题解:
2维权重dij, 先距离最短, 后改变最小。
在这个题中, 如果要改变最小, 则让更多的可用边放进来。
然后可以用pre存下关键边。
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 1e5 + 100; vector<pll> vc[N]; struct Node{ int o, u, d, p; bool operator < (const Node & x) const{ if(d == x.d) return p > x.p; return d > x.d; } }; int pre[N]; int vis[N]; priority_queue<Node> pq; void dij(){ pq.push({0, 1, 0, 0}); while(!pq.empty()){ Node t = pq.top(); pq.pop(); if(vis[t.u]) continue; vis[t.u] = 1; pre[t.u] = t.o; for(pll tmp : vc[t.u]){ if(vis[tmp.fi]) continue; pq.push({t.u, tmp.fi, t.d+1, t.p + !tmp.se}); } } } vector<Node> ans; int fpre[N]; int main(){ int n, m, u, v, op; scanf("%d%d", &n, &m); for(int i = 1; i <= m; ++i){ scanf("%d%d%d", &u, &v, &op); vc[u].pb(pll(v, op)); vc[v].pb(pll(u, op)); } dij(); int x = n; while(x){ fpre[x] = pre[x]; x = pre[x]; } for(int i = 1; i <= n; ++i){ for(pll &t : vc[i]){ if(t.fi < i){ int v = t.fi; if((fpre[i] == v || fpre[v] == i)){ if(!t.se) ans.pb({v, i, 1, 0}); } else if(t.se) ans.pb({v, i, 0, 0}); } } } printf("%d\n", ans.size()); for(Node & t : ans){ printf("%d %d %d\n", t.o, t.u, t.d); } return 0; }