Problem: 2680 (choose the best route) Judge Status: Accepted <br/> runid: 1154926 language: G ++ Author: zhouxc <br/> code render status: rendered by hdoj g ++ code Rander version 0.01 beta <br/> # include "stdio. H "<br/> # include" string. H "<br/> # define M 100000000 <br/> typedef struct {</P> <p> int Dist; <br/> bool known; <br/>} tableentery; <br/> tableentery table [1002]; </P> <p> int n, m, S, W, station; <B R/> int A, B, C, flag, result, map [1002] [1002]; <br/> bool started [1002]; </P> <p> void inittable () <br/> {</P> <p> for (INT I = 1; I <= N; I ++) <br/>{</P> <p> table [I]. dist = m; <br/> table [I]. known = false; <br/>}</P> <p> void Dijkstra () <br/>{</P> <p> inittable (); <br/> flag = 0; <br/> table [s]. dist = 0; <br/> bool succes = true; <br/> If (started [s] = true) <br/>{< br/> result = 0; <br/> succes = false; <br/>}< br/> wh Ile (succes) <br/>{</P> <p> int record, min = m; <br/> for (INT I = 1; I <= N; I ++) <br/>{< br/> If (! Table [I]. known & min> table [I]. dist) <br/>{< br/> min = table [I]. dist; <br/> record = I; <br/> // printf ("* % d/N", table [I]. dist); <br/>}< br/> // printf ("% d/n", record, min ); <br/> If (min = m) <br/> break; <br/> table [Record]. known = true; <br/> If (started [Record] = true) <br/> {<br/> // printf ("% d/n ", table [Record]. dist); <br/> flag = 1; <br/> result = table [Record]. dist; <br/> succes = false; <br/> Break; <br/>}< br/> for (INT I = 1; I <= N; I ++) <br/>{</P> <p> If (! Table [I]. known & map [Record] [I] & table [I]. dist> map [Record] [I] + Table [Record]. dist) <br/> table [I]. dist = map [Record] [I] + Table [Record]. dist; // printf ("% d/N", I, table [I]. dist); </P> <p >}< br/>}</P> <p> int main () <br/>{</P> <p> while (scanf ("% d", & N, & M, & S )! = EOF) <br/>{< br/> memset (started, false, sizeof (started); <br/> memset (MAP, 0, sizeof (MAP )); <br/> for (INT I = 1; I <= m; I ++) <br/> {<br/> scanf ("% d ", & A, & B, & C); <br/> If (Map [B] [a] = 0 | map [B] [a]> C) <br/> map [B] [a] = C; <br/>}< br/> scanf ("% d", & W ); <br/> for (INT I = 1; I <= W; I ++) <br/> {<br/> scanf ("% d", & station ); <br/> started [station] = true; <br/>}< br/> Dijkstra (); <br/> If (FLAG) <br/> printf ("% d/N ", Result); <br/> else <br/> printf ("-1/N"); </P> <p >}</P> <p> return 0; <br/>}</P> <p> # include "stdio. H "<br/> # include" string. H "<br/> # define M 100000000 <br/> typedef struct {</P> <p> int Dist; <br/> bool known; <br/>} tableentery; <br/> tableentery table [1002]; </P> <p> int n, m, S, W, station; <br/> int A, B, C, flag, result, map [1002] [1002]; <br/> bool started [1002]; </P> <p> void inittable () <br/>{</P> <p> for (INT I = 1; I <= N; I ++) <br/>{</P> <p> table [I]. dist = m; <br/> table [I]. known = false; <br/>}</P> <p> void Dijkstra () <br/>{</P> <p> inittable (); <br/> flag = 0; <br/> table [s]. dist = 0; <br/> bool succes = true; <br/> If (started [s] = true) <br/>{< br/> result = 0; <br/> succes = false; <br/>}< br/> while (succes) <br/>{</P> <p> int record, min = m; <br/> for (INT I = 1; I <= N; I ++) <br/>{< br/> If (! Table [I]. known & min> table [I]. dist) <br/>{< br/> min = table [I]. dist; <br/> record = I; <br/> // printf ("* % d/N", table [I]. dist); <br/>}< br/> // printf ("% d/n", record, min ); <br/> If (min = m) <br/> break; <br/> table [Record]. known = true; <br/> If (started [Record] = true) <br/> {<br/> // printf ("% d/n ", table [Record]. dist); <br/> flag = 1; <br/> result = table [Record]. dist; <br/> succes = false; <br/> Break; <br/>}< br/> for (INT I = 1; I <= N; I ++) <br/>{</P> <p> If (! Table [I]. known & map [Record] [I] & table [I]. dist> map [Record] [I] + Table [Record]. dist) <br/> table [I]. dist = map [Record] [I] + Table [Record]. dist; // printf ("% d/N", I, table [I]. dist); </P> <p >}< br/>}</P> <p> int main () <br/>{</P> <p> while (scanf ("% d", & N, & M, & S )! = EOF) <br/>{< br/> memset (started, false, sizeof (started); <br/> memset (MAP, 0, sizeof (MAP )); <br/> for (INT I = 1; I <= m; I ++) <br/> {<br/> scanf ("% d ", & A, & B, & C); <br/> If (Map [B] [a] = 0 | map [B] [a]> C) <br/> map [B] [a] = C; <br/>}< br/> scanf ("% d", & W ); <br/> for (INT I = 1; I <= W; I ++) <br/> {<br/> scanf ("% d", & station ); <br/> started [station] = true; <br/>}< br/> Dijkstra (); <br/> If (FLAG) <br/> printf ("% d/N", result); <br/> else <br/> printf ("-1/N "); </P> <p >}</P> <p> return 0; <br/>}</P> <p>