1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<queue> using namespace std; typedef long long ll; const int N = 2e4 + 10; const int M = 1e6 + 10; const int inf = 0x3f3f3f3f; int t, n, m, flag; int dist[N], vis[N], cnt[N]; int head[N], edge[M * 2], nxt[M * 2], w[M * 2], tot; void add(int x, int y, int z) { edge[++tot] = y; nxt[tot] = head[x]; w[tot] = z; head[x] = tot; } void spfa(int s) { queue<int>q; memset(vis, 0, sizeof(vis)); memset(cnt, 0, sizeof(cnt)); memset(dist, 0x3f, sizeof(dist)); dist[s] = 0; q.push(s); while (q.size()) { int x = q.front(); q.pop(); cnt[x]++; if (cnt[x] > n) { flag = 1; return ; } vis[x] = 0; for (int i = head[x]; ~i; i = nxt[i]) { int y = edge[i], z = w[i]; if (dist[y] > dist[x] + z) { dist[y] = dist[x] + z; if (!vis[y]) { vis[y] = 1; q.push(y); } } } } } int main() { scanf("%d", &t); while (t--) { memset(head, -1, sizeof(head)); tot = 0; flag = 0; scanf("%d%d", &n, &m); while (m--) { int x, y, z; scanf("%d%d%d", &x, &y, &z); add(x - 1, y, z); add(y, x - 1, -z); } for (int i = 1; i <= n; i++) { add(n + 1, i, 0); } spfa(n + 1); if (!flag) { puts("AC"); } else { puts("WA"); } } return 0; }
|