1. 如果没有负权边,本质上是对每个顶点做一次dijkstra,时间复杂度维nmlog n
2. 如果存在负权边,需要先设立0号点为超级原点,往每个点连长度是0的边,然后先用spfa跑一遍判断有没有负环,同时记录0号到任意店的距离
3. 然后将所有的边的边权更改为w[i]+d[i]-d[e[i]],然后对n个点跑dijkstra
4. 最后在计算任意两个点之间的距离时需要加上d[e[i]]-d[i],这样得到的就是任意点之间的距离
总体时间复杂度时nmlog n
// Problem: P5905 【模板】Johnson 全源最短路
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5905
// Memory Limit: 128 MB
// Time Limit: 1000 ms
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<set>
#include<unordered_map>
#include<ctime>
#include<cstdlib>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int> > PIII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}
int T,hackT;
int n,m,k;
int h[N],e[M],ne[M],w[M],idx;
bool st[3010];
int d[N];
int cnt[N];
int dist[3010];
void add(int a,int b,int c) {
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
bool spfa(int u) {
memset(d,0x3f,sizeof d);
d[u]=0;
queue<int> q;
q.push(u);
st[u]=true;
cnt[u]=-1;
while(q.size()) {
auto t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i]) {
int j=e[i];
if(d[j]>d[t]+w[i]) {
d[j]=d[t]+w[i];
cnt[j]=cnt[t]+1;
if(cnt[j]>=n) return false;
if(!st[j]) {
st[j]=true;
q.push(j);
}
}
}
}
return true;
}
void dijkstra(int u) {
memset(st,false,sizeof st);
memset(dist,0x3f,sizeof dist);
priority_queue<pair<ll,int>,vector<pair<ll,int> > ,greater<pair<ll,int> > > q;
dist[u]=0;
q.push({dist[u],u});
while(q.size()) {
auto it=q.top();
q.pop();
int ver=it.se;
if(st[ver]) continue;
st[ver]=true;
for(int i=h[ver];i!=-1;i=ne[i]) {
int j=e[i];
if(dist[j]>dist[ver]+w[i]) {
dist[j]=dist[ver]+w[i];
q.push({dist[j],j});
}
}
}
}
void solve() {
n=read(),m=read();
memset(h,-1,sizeof h);
while(m--) {
int a=read(),b=read(),c=read();
add(a,b,c);
}
for(int i=1;i<=n;i++) add(0,i,0);
if(!spfa(0)) {
printf("-1\n");
return ;
}
for(int i=1;i<=n;i++) {
for(int j=h[i];j!=-1;j=ne[j]) {
int a=i,b=e[j];
w[j]+=d[a]-d[b];
}
}
for(int i=1;i<=n;i++) {
dijkstra(i);
ll res=0;
for(int j=1;j<=n;j++) {
if(dist[j]==INF) res=res+(ll)j*1000000000;
else res=res+(ll)j*(dist[j]+d[j]-d[i]);
}
printf("%lld\n",res);
}
}
int main() {
// init();
// stin();
// scanf("%d",&T);
T=1;
while(T--) hackT++,solve();
return 0;
}