https://www.luogu.com.cn/problem/P5905
思路
先跑spfa找到“势能”以及是否有闭环,然后每个点Dijkstra找最短路。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
const int M = 60006;
const int INF = 1e9;
struct edge {
long long v, w, next;
} e[M];
struct node {
int id;
long long w;
const bool operator < (const node &a) const {
return a.w < w;
}
};
int cnt, n, m;
int head[M], inq[M], vis[M], num[N];
long long dis[M], ndis[M];
void add(int u, int v, int w) {
cnt++;
e[cnt].v = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt;