Você está na página 1de 3

public class CheapestPath {

// the same price of the edges


// network size: n*n, // n - number of rows (columns)
int n;
Node [][] mN;
public CheapestPath(int n){
this.n = n;
mN = new Node[n][n];
}
// number of paths in matrix with equal paths
public static int numberOfEqualPaths(int n){
int mat[][] = new int[n][n];
for (int i=0; i<n; i++){
mat[i][0] = 1;
mat[0][i] = 1;
}
for (int i=1; i<n; i++){
for (int j=i; j<n; j++){
mat[i][j] = mat[i-1][j]+mat[i][j-1];
mat[j][i] = mat[i][j];
}
}
MyLibrary.printIntMatrix(mat);
return mat[n-1][n-1];
}
// inner class Node
private class Node{
int y, x, entry;
public Node(int x, int y){
this.x = x;
this.y = y;
this.entry = 0;
}
}// inner class Node
public void initMatOfNodes(){
// the 1-st row
mN[0][0] = new Node(10,1);
mN[0][1] = new Node(2,4);
mN[0][2] = new Node(12,5);
mN[0][3] = new Node(9,1);
mN[0][4] = new Node(1,2);
mN[0][5] = new Node(0,5);
// the 2-nd row
mN[1][0] = new Node(3,10);
mN[1][1] = new Node(4,4);
mN[1][2] = new Node(2,2);
mN[1][3] = new Node(8,5);
mN[1][4] = new Node(4,6);
mN[1][5] = new Node(0,7);
// the 3-d row
mN[2][0] = new Node(2,5);
mN[2][1] = new Node(8,1);
mN[2][2] = new Node(8,5);
mN[2][3] = new Node(13,4);
mN[2][4] = new Node(8,5);
mN[2][5] = new Node(0,5);
// the 4-th row
mN[3][0] = new Node(2,3);
mN[3][1] = new Node(7,10);
mN[3][2] = new Node(4,8);
mN[3][3] = new Node(3,2);//check it!
mN[3][4] = new Node(4,2);
mN[3][5] = new Node(0,3);
// the 5-th row
mN[4][0] = new Node(1,10);
mN[4][1] = new Node(2,4);
mN[4][2] = new Node(9,7);
mN[4][3] = new Node(5,11);
mN[4][4] = new Node(1,3);
mN[4][5] = new Node(0,4);
// the 6-th row
mN[5][0] = new Node(8,0);
mN[5][1] = new Node(5,0);
mN[5][2] = new Node(15,0);
mN[5][3] = new Node(4,0);
mN[5][4] = new Node(10,0);
mN[5][5] = new Node(0,0);
}
// number of cheapest paths
public int numberOfPaths(){
mN[0][0].entry = 0;
for (int i=1; i<n; i++){
mN[0][i].entry = mN[0][i-1].entry+ mN[0][i-1].x;
mN[i][0].entry = mN[i-1][0].y+ mN[i-1][0].entry;
}
for (int i=1; i<n; i++){
for (int j=1; j<n; j++){
int x = mN[i-1][j].entry+mN[i-1][j].y;
int y = mN[i][j-1].entry+mN[i][j-1].x;
mN[i][j].entry = Math.min(x,y);
}
}
printPrice();
return mN[n-1][n-1].entry;
}
// Print matrix of prices
public void printPrice(){
for (int i=0; i<mN.length; i++){
for (int j=0; j<mN.length; j++){
System.out.print(mN[i][j].entry+" ");
}
System.out.println();
}
System.out.println("----------------------");
}
// one of the cheapest paths
public void printPath(){
String p = "("+n+","+n+")";
int i = n-1, j = n-1;
while (i>0 || j>0){
int x = mN[i-1][j].entry+mN[i-1][j].y;
if (mN[i][j].entry == x) i--;
else j--;
p = p + "->("+(i+1)+","+(j+1)+")";
}
System.out.println("\none of the cheapest paths \n"+p);
}
public static void main(String[] args) {
int n = 6;
System.out.println("number of paths (with equal prices):
"+numberOfEqualPaths(n));
CheapestPath cp = new CheapestPath(n);
cp.initMatOfNodes();
System.out.println("number of cheapest paths: "+cp.numberOfPaths());
cp.printPath();
}
}
/// the results number of paths wits equal prices
// 1, 1, 1, 1, 1, 1,
// 1, 2, 3, 4, 5, 6,
// 1, 3, 6, 10, 15, 21,
// 1, 4, 10, 20, 35, 56,
// 1, 5, 15, 35, 70, 126,
// 1, 6, 21, 56, 126, 252,
// number of paths (with equal prices): 252

// matrix of prices
//0 10 12 24 33 34
//1 4 8 10 18 22
//11 8 10 15 24 29
//16 9 15 19 22 26
//19 19 21 21 24 25
//29 23 28 32 27 29
//----------------------
//number of cheapest paths: 29

// one of the cheapest paths


//(6,6)->(5,6)->(5,5)->(4,5)->(4,4)->(3,4)->(2,4)->(2,3)->(2,2)->(2,1)->(1,1)

Você também pode gostar