Solution to the problem of applying assembly lines
Attach source code Java version
1 Packagesecond;2 Public classSP {3 Static intx=100000;//no road between two points, path length is set to infinity4 Static intmax=100000;5 intdata[][]=New int[10] [10];//the full value of the edge6 Static int[]dist=New int[10];//record the value of the shortest path7 Static intpath[]=New int[10];//Record Shortest Path8 Public Static voidFpath (intA[][])//find the shortest Path value9 {Ten inti,j,k; OneDist[0]=0; A for(i=1;i<10;i++) - { -k=Max; the for(j=0;j<i;j++) - { - if(a[j][i]!=x) - if((Dist[j]+a[j][i]) <k) +k=dist[j]+A[j][i]; Dist[1]+a[1,4]=4+10=14 - }//dist[2]+a[2,4]=2+6=8 8<14 dist[4]=8 to the shortest path to node 4 +dist[i]=K; A } at } - Static intFroute (intA[][])//find the shortest path - { - intJ,b,k=1,i=9; -path[0]=10; - while(i>0) in { - for(j=i-1;j>=0;j--) to { + if(a[j][i]!=x) - { theb=dist[i]-A[j][i]; * if(b==Dist[j]) $ {Panax NotoginsengPath[k++]=j+1; -I=J; the Break; + } A } the + } - } $ returnK; $ } - - Public Static voidMain (string[] args) the { - inti,m;Wuyi int[] A ={ the{x,4,2,3, X,x,x,x,x,x},//a[j][i] represents the cost of point J to I -{x,x,x,x,10,9, x,x,x,x}, Wu{x,x,x,x,6,7,10, x,x,x}, -{x,x,x,x,x,3,8, x,x,x}, About{x,x,x,x,x,x,x,4,8, x}, ${x,x,x,x,x,x,x,9,6, x}, -{x,x,x,x,x,x,x,5,4, x}, -{x,x,x,x,x,x,x,x,x,8}, -{x,x,x,x,x,x,x,x,x,4}, A {x,x,x,x,x,x,x,x,x,x},}; + Fpath (A); theSystem.out.print (dist[9]); -System.out.println (""); $m=Froute (A); the for(i=m-1;i>0;i--) theSystem.out.print (path[i]+ "-"); theSystem.out.print (path[0]); the - } in the the}
Problem
1.public static void Main (string[] args) The format of the main function is changed directly can not run;
2. Specify how many int data[][]=new int[10][10] per row per column for the definition array;
3. Static array definition mode: static int[]dist=new int[10];
4.nullpointerexception Array length is not initialized;
Reference: http://www.cnblogs.com/lpshou/archive/2012/04/17/2453370.html
Solving the shortest path with DP