28行那里
#include<bits/stdc++.h>
#define up(i,x,y) for (int (i)=(x);(i)<=(y);i++)
#define down(i,x,y) for (int (i)=(x);(i)>=(y);i--)
#define ll long long
#define elif else if
#define inf 1000000000
using namespace std;
struct node{
int to,w;
friend bool operator<(node a,node b){
return a.w>b.w;
}
};
vector<node> t[3005];
int n,m,u,v,w,cnt[3005]={},h[3005]={},dis[3005];
bool vis[3005];
bool spfa(){
queue<int>q;
cnt[0]=vis[0]=1;
q.push(0);
int p;
while(not q.empty()){
p=q.front();
q.pop();
vis[p]=0;
for (node x:t[p]){
// 错在这里 vector[0]遍历的时候冲出去了
if (h[x.to]>h[p]+x.w){
h[x.to]=h[p]+x.w;
if (not vis[x.to]){
q.push(x.to);
vis[x.to]=1;
if ((++cnt[x.to])>n) return 1;
}
}
}
}
}
priority_queue<node> q;
void dij(int start){
up(i,1,n) dis[i]=inf,vis[i]=0;
dis[start]=0;
q.push({start,0});
int p;
while (not q.empty()){
p=q.top().to;
q.pop();
if (vis[p]) continue;
vis[p]=1;
for (node x:t[p]){
if (vis[x.to]) continue;
if (dis[x.to]>dis[p]+x.w){
dis[x.to]=dis[p]+x.w;
q.push({x.to,dis[x.to]});
}
}
}
}
int main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
cin>>n>>m;
up(i,1,n) t[0].push_back({i,0});
up(i,1,m){
cin>>u>>v>>w;
t[u].push_back({v,w});
}
if (spfa()){
cout<<-1;
return 0;
}
up(i,1,n) for(node &x:t[i]) x.w+=h[i]-h[x.to];
up(i,1,n){
dij(i);
ll ans=0;
up(j,1,n)
ans+=(dis[j]==inf?j*inf:j*dis[j]+h[j]-h[i]);
cout<<ans;
}
}