萌新刚学 OI,差分约束板子全输出 NO 求调

P1993 小 K 的农场

```cpp #include <bits/stdc++.h> #define int long long using namespace std; int n, m, tot, head[100005], ans[100005], flag[100005], sum[100005]; queue <int> q; struct jc{ int to, val, net; }e[100005]; void add(int x, int y, int z) { e[++tot].to = y; e[tot].val = z; e[tot].net = head[x]; head[x] = tot; } bool spfa(int s) { memset(flag, 0, sizeof(flag)); memset(ans, 0x7F, sizeof(ans)); ans[s] = 0; sum[s] = 1; flag[s] = 1; q.push(s); while(!q.empty()) { int x = q.front(); q.pop(); flag[x] = 0; for (int i = head[x]; i; i = e[i].net) { int y = e[i].to; if (ans[y] > ans[x] + e[i].val) { ans[y] = ans[x] + e[i].val; if(!flag[y]) { q.push(y); flag[y] = 1; sum[y]++; if (sum[y] > n + 1) return 0; } } } } return 1; } signed main() { cin >> n >> m; for (int i = 1; i <= n; i++) add(0, i, 0); for (int i = 1; i <= m; i++) { int op, x, y, z; cin >> op >> x >> y; if (op == 1) { cin >> z; add(x, y, -z); } if (op == 2) { cin >> z; add(y, x, z); } else { add(x, y, 0); add(y, x, 0); } } if (spfa(0)) cout << "Yes" << '\n'; else cout << "No" << '\n'; return 0; } ```
by kaceqwq @ 2023-01-29 10:07:34


@[kaceqwq](/user/527992) `if (op == 2)` -> `else if (op == 2)`
by AZN_0975 @ 2023-01-29 10:21:23


@[AZN_0975](/user/476985) 感谢,我是sb
by kaceqwq @ 2023-01-29 10:22:53


@[kaceqwq](/user/527992) ```cpp #include <bits/stdc++.h> #define int long long using namespace std; int n, m, tot, head[100005], ans[100005], flag[100005], sum[100005]; queue <int> q; struct jc{ int to, val, net; }e[100005]; void add(int x, int y, int z) { e[++tot].to = y; e[tot].val = z; e[tot].net = head[x]; head[x] = tot; } bool spfa(int s) { memset(flag, 0, sizeof(flag)); memset(ans, 0x3F, sizeof(ans)); ans[s] = 0; flag[s] = 1; q.push(s); while(!q.empty()) { int x = q.front(); q.pop(); flag[x] = 0; for (int i = head[x]; i; i = e[i].net) { int y = e[i].to; if (ans[y] > ans[x] + e[i].val) { ans[y] = ans[x] + e[i].val; sum[y]++; if (sum[y] > n) return 0; if(!flag[y]) { q.push(y); flag[y] = 1; } } } } return 1; } signed main() { cin >> n >> m; for (int i = 1; i <= n; i++) add(0, i, 0); for (int i = 1; i <= m; i++) { int op, x, y, z; cin >> op >> x >> y; if (op == 1) { cin >> z; add(x, y, -z); } else if (op == 2) { cin >> z; add(y, x, z); } else { add(x, y, 0); add(y, x, 0); } } if (spfa(0)) cout << "Yes" << '\n'; else cout << "No" << '\n'; return 0; } ```
by Texas_the_Omertosa @ 2023-01-29 10:25:12


|