题意:


思路:
https://ac.nowcoder.com/discuss/151522?type=101&order=0&pos=1&page=0&channel=666&source_id=discuss_tag



%E7%9A%84%E8%BE%B9%E6%9D%83%E8%B5%8B%E4%B8%BAdist%5Bu%5D%20-%20dist%5Bv%5D%20%2B%20w(w%E6%98%AF%E5%8E%9F%E6%9D%A5%E7%9A%84%E8%BE%B9%E6%9D%83)&preview=true)

%E5%BA%94%E6%BB%A1%E8%B6%B3%E4%B8%89%E8%A7%92%E5%BD%A2%E4%B8%8D%E7%AD%89%E5%BC%8Fdist%5Bu%5D%2Bw%20%3E%3D%20dist%5Bv%5D%2C%E7%A7%BB%E9%A1%B9%E5%8F%AF%E5%BE%97&preview=true)



%20%3D%20w(u%2Cx_1)%20%2B%20w(x_1%2Cx_2)%20%2B%20w(x_2%2Cx_3)%20%2B%20....w(x_m%2Cv)&preview=true)
%20%2B%20dist%5Bu%5D%20-%20dist%5Bx_1%5D%20%2B%20w(x_1%2Cx_2)%20%2B%20dist%5Bx_1%5D%20-%20dist%5Bx_2%5D%2B....%2Bw(x_m%2Cv)%2Bdist%5Bx_m%5D-dist%5Bv%5D&preview=true)

%20%2B%20w(x_1%2Cx_2)%20%2B%20w(x_2%2Cx_3)%20%2B%20....w(x_m%2Cv)%20%2B%20dist%5Bu%5D%20-%20dist%5Bv%5D&preview=true)


#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
const int M = 1e6 + 10;
const int N = 1e3 + 10;
typedef long long ll;
const ll inf = 1e18;
struct Edge{
int u,to,w,nex;
}e[M];
int head[N],idx;
int n,m;
void add_edge(int u,int v,int w){
e[idx].u = u;
e[idx].to = v;
e[idx].w = w;
e[idx].nex = head[u];
head[u] = idx++;
}
ll dist[N];
bool in[N];
void spfa(int S){
for(int i = 0;i <= n;i++){
dist[i] = inf,in[i] = 0;
}
queue<int> q;
dist[S] = 0;
q.push(S);
while(q.size()){
int u = q.front();q.pop();
in[u] = 0;
for(int i = head[u];~i;i = e[i].nex){
int v = e[i].to,w = e[i].w;
if(dist[v] > dist[u] + w){
dist[v] = dist[u] + w;
if(!in[v]){
q.push(v);
in[v] = 1;
}
}
}
}
}
int main(){
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i = 1,u,v,w;i <= m;i++){
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
}
for(int i = 1;i <= n;i++) add_edge(0,i,0);
spfa(0);
for(int i = 0,u,v,w;i < m;i++){
u = e[i].u,v = e[i].to,w = e[i].w;
printf("%d %d %lld\n",u,v,dist[u] - dist[v] + w);
}
return 0;
}