Description
is the shortest circuit.
Algorithm
Dijkstra
I did a bit of theoretical time complexity O (v^2) with the most water-not-optimized.
V is the top point
And then think about the fact that the v^2 of the first assignment inf is not worth it.
Did a hascost optimization, space change time, but not much faster
Or do you want to learn the priority queue optimization?
Code First edition
ImportJava.util.Arrays;ImportJava.util.Scanner; Public class Main { Public Static void Main(string[] args) {Scanner cin =NewScanner (system.in); for(;;) {intn = cin.nextint ();intm = Cin.nextint ();if(n = =0) Break;int[] Cost =New intN [n]; for(inti =0; I < n; i++) {Arrays.fill (cost[i], Integer.max_value/2); Cost[i][i] =0; } for(inti =0; I < m; i++) {intA = Cin.nextint ()-1;intb = Cin.nextint ()-1;intc = Cin.nextint (); COST[A][B] = c; Cost[b][a] = c; }int[] D =New int[n]; Arrays.fill (d, Integer.max_value/2);Boolean[] used =New Boolean[n]; d[0] =0; for(;;) {intv =-1; for(intU =0; U < n; u++) {if(!used[u] && (v = =-1|| D[u] < d[v])) v = u; }if(v = =-1) Break; USED[V] =true; for(intU =0; U < n; u++) {D[u] = Math.min (D[u], d[v] + cost[v][u]); }} System.out.println (D[n-1]); } }}
Second Edition
ImportJava.util.Arrays;ImportJava.util.Scanner; Public class Main { Public Static void Main(string[] args) {Final intINF = Integer.max_value/2; Scanner cin =NewScanner (system.in); for(;;) {intn = cin.nextint ();intm = Cin.nextint ();if(n = =0) Break;int[] Cost =New intN [n];Boolean[] Hascost =New BooleanN [n]; for(inti =0; I < m; i++) {intA = Cin.nextint ()-1;intb = Cin.nextint ()-1;intc = Cin.nextint (); COST[A][B] = c; Cost[b][a] = c; HASCOST[A][B] =true; Hascost[b][a] =true; }int[] D =New int[n]; Arrays.fill (d, INF);Boolean[] used =New Boolean[n]; d[0] =0; for(;;) {intv =-1; for(intU =0; U < n; u++) {if(!used[u] && (v = =-1|| D[u] < d[v])) v = u; }if(v = =-1) Break; USED[V] =true; for(intU =0; U < n; u++) {intW = d[v] + cost[v][u];if(!hascost[v][u]) w = INF; D[u] = Math.min (D[u], W); }} System.out.println (D[n-1]); } }}
HDU 2544 Shortest circuit