POJ3259简单SPFA判负权回路
提交了好几次终于AC了,开始没仔细看题,后来才看见a bidirectional path between S and E that requires T seconds to traverse这句话!!原来路是双向的,虫洞是单向的,还有注意Two fields might be connected by more than one path. 这句话形同虚设,不用考虑这条边再次赋新值的取舍,直接再来一条新边就ok了。最近写图论比较多,不知道用vector好还是指针好= =还是请教一下老师比较好
View Code
1 #include <iostream> 2 #include <queue> 3 #include <vector> 4 using namespace std; 5 int n, m, p, w; 6 int b[100001], f[100001], e[100001]; 7 int main() 8 { 9 int i, x, y, z, t, tt; 10 queue<int> que; 11 vector<pair<int, int> > a[100001]; 12 vector<pair<int, int> > :: iterator it; 13 scanf("%d", &w); 14 while (w--) 15 { 16 scanf("%d%d%d", &n, &m, &p); 17 memset(a, 0, sizeof(a)); 18 memset(b, 0, sizeof(b)); 19 memset(e, 0, sizeof(e)); 20 memset(f, 8, sizeof(f)); 21 for (i = 1; i <= m; i++) 22 { 23 cin >> x >> y >> z; 24 a[x].push_back(make_pair(y, z)); 25 a[y].push_back(make_pair(x, z)); 26 } 27 for (i = 1; i <= p; i++) 28 { 29 cin >> x >> y >> z; 30 a[x].push_back(make_pair(y, -z)); 31 } 32 que.push(1); 33 b[1] = 1; 34 f[1] = 0; 35 while (!que.empty()) 36 { 37 t = que.front(); 38 e[t]++; 39 if (e[t] > n) 40 { 41 b[0] = 1; 42 break; 43 } 44 for (it = a[t].begin(); it < a[t].end(); it++) 45 { 46 i = it -> first; 47 if (f[i] > f[t] + it -> second) 48 { 49 f[i] = f[t] + it -> second; 50 if (b[i] == 0) 51 { 52 b[i] = 1; 53 que.push(i); 54 } 55 } 56 } 57 b[t] = 0; 58 que.pop(); 59 } 60 if (b[0] == 1) 61 printf("YES\n"); 62 else 63 printf("NO\n"); 64 } 65 system("pause"); 66 return 0; 67 }


浙公网安备 33010602011771号