题解:
题意大致为给定1e5的无向图,在m条边中有点边是好的有的是坏的,若要用坏的就要修,多余的好的要炸。问从1到n的最短路中修和炸的次数最少是几次
首先大致框架就是一个bfs,之后我们可以利用类似求最短路松弛的原理,对每个点求最短路到此点需要修路的最小值(因为我们发现路只有两种情况,因此只要保证修的路越少,则用之前能用的路就会越多,炸的也就越少,这样总和才最少)这样回溯把路径打上标记,最后比较原路输出即可
代码:
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=3e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//逆元
int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
bool mark[N],ans[N];
int dis[N],pre[N],p[N];
int n,res;
void bfs(){
for(int i=1;i<=n;i++) {
dis[i]=INF;
p[i]=0;
}
queue<int>q;
q.push(1);
dis[1]=0;
res=0;
bool flag=false;
while(!q.empty()){
int ti=q.size();
while(ti--){
int x=q.front();
q.pop();
for(int i=head[x];i;i=NEXT[i]){
int y=ver[i];
// cout<<dis[y]<<' '<<INF<<endl;
if(y==n) flag=true;
if(p[y]==0) q.push(y);
if(p[y]!=p[x]+1&&p[y]!=0) continue;
p[y]=p[x]+1;
if(mark[i/2]){
if(dis[y]>dis[x]){
dis[y]=dis[x];
pre[y]=i;
}
}
else {
if(dis[y]>dis[x]+1){
dis[y]=dis[x]+1;
pre[y]=i;
}
}
}
}
res++;
if(flag) return;
}
}
int main(){
//freopen("1.txt","r",stdin);
//ios::sync_with_stdio(false);
int m;
int inp;
int u,v;
tot=1;
int num=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&inp);
link(u,v);
link(v,u);
mark[i]=inp;
if(inp==1) num++;
}
bfs();
int tmp=n;
while(tmp!=1){
ans[pre[tmp]/2]=true;
tmp=ver[pre[tmp]^1];
}
printf("%d\n",num-(res-dis[n])+dis[n]);
for(int i=1;i<=m;i++){
// cout<<ans[i]<<' '<<mark[i]<<endl;
if(mark[i]==1&&ans[i]==0) printf("%d %d 0\n",ver[i*2+1],ver[i*2]);
else if(mark[i]==0&&ans[i]==1) printf("%d %d 1\n",ver[i*2+1],ver[i*2]);
}
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}