最后一个点没过,到底咋回事啊,大佬帮看看
// 题目: 【模板】差分约束算法
// 链接: https://www.luogu.com.cn/problem/P5960
// 难度: 普及+/提高
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
const int MAX_NUM = 1e4 + 5;
struct edge {
int u;
int to, next;
ll dis;
} g[MAX_NUM];
int head[MAX_NUM], color[MAX_NUM], n, m, k = 0;
ll dist[MAX_NUM];
bool cut[MAX_NUM];
bool spfa(int s) {
dist[s] = 0;
cut[s] = true;
queue<int> q;
q.push(s);
while (!q.empty()) {
auto u = q.front();
q.pop();
if (++color[u] >= n) {
return false;
}
cut[u] = false;
for (int i = head[u]; i; i = g[i].next) {
auto v = g[i];
if (dist[v.to] > dist[u] + v.dis) {
dist[v.to] = dist[u] + v.dis;
if (!cut[v.to]) {
cut[v.to] = true;
q.push(v.to);
}
}
}
}
return true;
}
void add(int u, int v, ll w) {
g[++k].to = v;
g[k].u = u;
g[k].dis = w;
g[k].next = head[u];
head[u] = k;
}
void best_coder() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v;
ll w;
cin >> u >> v >> w;
add(v, u, w);
}
for (int i = 1; i <= n; ++i) {
add(0, i, 0);
dist[i] = INF;
}
if (!spfa(0)) {
cout << "NO";
return;
}
for (int i = 1; i <= n; ++i) {
cout << dist[i] << " ";
}
}
void happy_coder() {
}
int main() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// 小码匠
best_coder();
// 最优解
// happy_coder();
return 0;
}