HDU 2544 Shortest circuit

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.