Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
第一次用spfa#include<iostream> #include<string> #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<queue> #include<stack> #include<set> using namespace std; int n; int f; const int MAX = 0x3f3f3f3f; const int NUM = 510; int map[NUM][NUM] ; int dis[NUM]; int vis[NUM]; int p[NUM]; int c[NUM]; int spfa() { memset(vis, 0, sizeof(vis)); memset(c, 0, sizeof(vis)); queue<int> q; dis[1] = 0; q.push(1); vis[1] = 1; c[1] = 1; while (!q.empty()) { int x; x = q.front(); q.pop(); vis[x] = 0; for (int k = 1; k <= n; k++) { if (dis[x] + ::map[x][k] < dis[k]&&k!=x) { dis[k] = dis[x] + ::map[x][k]; if (!vis[k]) { vis[k] = 1; c[k]++; q.push(k); if (c[k] > 1000) return 1; } } } } return 0; } int main() { cin >> f; for (int i = 0; i < f; i++) { int m, w; for (int i = 0; i < NUM; i++) for (int j = 0; j < NUM; j++) ::map[i][j] = MAX; for (int i = 0; i < NUM; i++) ::dis[i] = MAX; cin >> n >> m >> w; for (int i = 1; i <= m; i++) { int s, e, t; cin >> s >> e >> t; if (::map[s][e] == 0 || t < ::map[s][e]) ::map[s][e] = t; if (::map[e][s] == 0 || t < ::map[e][s]) ::map[e][s] = t; } for (int i = 1; i <= w; i++) { int s, e, t; cin >> s >> e >> t; if (::map[s][e] == 0 || -t < ::map[s][e]) ::map[s][e] = -t; } if (spfa()) printf("YES\n"); else printf("NO\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator