Poj1502 spfa Shortest Path

Source: Internet
Author: User
1 // accepted 320 KB 16 MS 2 // There are n vertices, Edge Weight expressed with a 3 // give the lower Triangle Matrix, calculate the maximum value of 4 # include <cstdio> 5 # include <cstring> 6 # include <iostream> 7 # include <queue> 8 # include <cmath> 9 # include <algorithm> 10 using namespace STD; 11/** 12 * This is a documentation comment Block 13 * if you can't stick to it one day, think about why you're here! 14 * @ authr songt 15 */16 const int imax_n = 105; 17 const int imax_e = imax_n * imax_n; 18 const int INF = 100000000; 19 struct node 20 {21 int U, v, C; 22 node (INT u = 0, int V = 0, int C = 0): U (u), V (V), C (c) 23 {24 25} 26} p [imax_e]; 27 int head [imax_n]; 28 int next [imax_e]; 29 bool vis [imax_n]; 30 int dis [imax_n]; 31 int CNT [imax_n]; 32 int N; 33 int e = 0; 34 void Init () 35 {36 memset (Head,-1, sizeof (he AD); 37 memset (next,-1, sizeof (next); 38 e = 0; 39} 40 void addedge (int u, int V, int C) 41 {42 p [e] = node (u, v, c); 43 next [e] = head [u]; 44 head [u] = e ++; 45} 46 bool relax (int u, int V, int c) 47 {48 if (DIS [v]> dis [u] + C) 49 {50 dis [v] = dis [u] + C; 51 return true; 52} 53 return false; 54} 55 queue <int> q; 56 bool spfa (int src) 57 {58 While (! Q. empty () Q. pop (); 59 memset (VIS, 0, sizeof (VIS); 60 memset (CNT, 0, sizeof (CNT); 61 for (INT I = 1; I <= N; I ++) 62 dis [I] = inf; 63 dis [SRC] = 0; 64 Q. push (SRC); 65 vis [SRC] = 1; 66 CNT [SRC] ++; 67 while (! Q. empty () 68 {69 int pre = Q. front (); 70 Q. pop (); 71 vis [pre] = 0; 72 for (INT I = head [pre]; I + 1; I = next [I]) 73 {74 if (relax (PRE, P [I]. v, p [I]. c )&&! Vis [p [I]. v]) 75 {76 if (++ CNT [p [I]. v])> = N) return false; 77 vis [p [I]. v] = 1; 78 Q. push (P [I]. v); 79} 80} 81} 82 return true; 83} 84 char s [10]; 85 int main () 86 {87 while (scanf ("% d ", & N )! = EOF) 88 {89 Init (); 90 for (INT I = 2; I <= N; I ++) 91 {92 for (Int J = 1; j <I; j ++) 93 {94 scanf ("% s", S); 95 If (s [0] = 'X') continue; 96 int c = atoi (s); 97 // printf ("c = % d \ n", c); 98 addedge (I, j, C ); 99 addedge (J, I, C); 100} 101} 102 spfa (1); 103 int ans = 0; 104 for (INT I = 1; I <= N; I ++) 105 if (DIS [I]> ans) ans = dis [I]; 106 printf ("% d \ n", ANS); 107} 108 return 0; 109}
View code

 

Poj1502 spfa Shortest Path

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.