题目链接:http://codeforces.com/contest/507/problem/E
#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cstring> #define N 100010 using namespace std; struct edge{ int u; int v; int w; bool z; int next; }e[N*2]; int last[N]; int d[N]; int s; int path[N]; int workNum[N]; bool vis[N]; void spfa(int s) { queue<int> q; memset(workNum,0,sizeof(workNum)); memset(d,0x3f,sizeof(d)); d[s]=0; memset(vis,0,sizeof(vis)); q.push(s); vis[s]=1; path[s]=-1; while(!q.empty()) { int x=q.front(); q.pop(); vis[x]=0; for(int k=last[x];k!=-1;k=e[k].next) { int u=e[k].u,v=e[k].v; int dist=d[u]+e[k].w; if(d[v]>dist||(d[v]==dist && workNum[u]+e[k].z>workNum[v])) { path[v]=u; d[v]=dist; workNum[v]=workNum[u]+e[k].z; if(!vis[v]) { vis[v]=1; q.push(v); } } } } } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { memset(last,-1,sizeof(last)); int eidx=0; int workSum=0; for(int i=0;i<m;i++) { int u,v,z; scanf("%d%d%d",&u,&v,&z); e[eidx].u=u,e[eidx].v=v,e[eidx].z=z,e[eidx].w=1,e[eidx].next=last[u],last[u]=eidx++; e[eidx].u=v,e[eidx].v=u,e[eidx].z=z,e[eidx].w=1,e[eidx].next=last[v],last[v]=eidx++; workSum+=z; } spfa(1); int ans=d[n]-workNum[n]+workSum-workNum[n]; cout<<ans<<endl; memset(vis,0,sizeof(vis)); int pre=n; while(path[pre]!=-1) { vis[pre]=1; pre=path[pre]; vis[pre]=1; } for(int i=0;i<eidx;i+=2) { int u=e[i].u,v=e[i].v; if(e[i].z && !(vis[v] && vis[u])) printf("%d %d 0\n",u,v); else if((path[v]==u||path[u]==v) && !e[i].z && vis[v] && vis[u]) printf("%d %d 1\n",u,v); } } return 0; }