Poj 1502 MPI maelstrom (single-source shortest path | Dijkstra implementation)

Source: Internet
Author: User

// Single-source shortest path. The final answer should be the maximum value from the starting point to the shortest path of other nodes. <Br/> # include <iostream> <br/> # include <queue> <br/> using namespace STD; <br/> const int INF = 50000000; <br/> int G [105] [105]; <br/> bool flag [105]; <br/> int dis [105]; <br/> int N; <br/> int Dijkstra (INT st) <br/>{< br/> int res = inf * (-1); <br/> for (INT I = 0; I <n; I ++) dis [I] = (I = sT? 0: INF); // initialization <br/> memset (flag, 0, sizeof (FLAG); </P> <p> for (INT I = 0; I <n; I ++) <br/>{< br/> int X, _ min = inf; <br/> for (INT y = 0; y <N; + + Y) <br/>{< br/> If (! Flag [y] & dis [y] <_ min) // find the minimum value in the unlabeled node <br/> _ min = dis [x = y]; <br/>}< br/> flag [x] = 1; // label <br/> for (INT y = 0; y <n; ++ y) <br/> {<br/> dis [y] = min (DIS [Y], DIS [x] + G [x] [Y]); // perform the relaxation operation on all other points starting from the label node <br/>}< br/> for (INT I = ST + 1; I <N; ++ I) <br/> If (DIS [I]> res) <br/> res = dis [I]; // find the maximum value in the single-source shortest path <br/> return res; <br/>}< br/> int main () <br/>{< br/> freopen ("in.txt", "r", stdin); <br/> char d [100]; <br /> Scanf ("% d", & N); <br/> memset (G, 0, sizeof (g); <br/> for (INT I = 1; I <n; ++ I) <br/> {<br/> for (Int J = 0; j <I; j ++) <br/>{< br/> scanf ("% s", d); <br/> If (d [0]! = 'X') <br/>{< br/> G [I] [J] = atoi (d ); <br/> G [J] [I] = atoi (d ); // use the atoi function to convert char * to int <br/>}< br/> else <br/>{< br/> G [I] [J] = inf; <br/> G [J] [I] = inf; <br/>}< br/> printf ("% d/N", Dijkstra (0 )); <br/> return 0; <br/>} 

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.