社区讨论
吸氧8ms,无氧TLE,?
P1993小 K 的农场参与者 3已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @lo7wew1i
- 此快照首次捕获于
- 2023/10/27 08:52 2 年前
- 此快照最后确认于
- 2023/10/27 08:52 2 年前
RT
CPP#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 5010;
struct Edge {
int to, next, w;
} G[maxn * 2];
int head[maxn], cnt;
void add(int u, int v, int w) {
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
G[cnt].w = w;
}
bool inq[maxn];
int num[maxn], dis[maxn];
bool spfa(int n) {
memset(dis, 0x3f, sizeof(dis));
dis[0] = 0;
queue<int> q;
q.push(0);
inq[0] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
if (++num[u] > n) return false;
for (int i = head[u]; i; i = G[i].next) {
int v = G[i].to;
if (dis[v] > dis[u] + G[i].w) {
dis[v] = dis[u] + G[i].w;
if (!inq[v]) {
q.push(v);
inq[v] = true;
}
}
}
inq[u] = false;
}
return true;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
int op, a, b, c;
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &op, &a, &b);
if (op == 3) {
add(a, b, 0);
add(b, a, 0);
} else {
scanf("%d", &c);
if (op == 1) add(a, b, -c);
else if (op == 2) add(b, a, c);
}
}
for (int i = 1; i <= n; i++) add(0, i, 0);
puts(spfa(n) ? "Yes" : "No");
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...