Codeforces D. Minimum maximum on the Path ###K //K
题目链接:https://codeforces.ml/edu/course/2/lesson/6/3/practice/contest/285083/problem/D
题意:给定有向图,每条边上有一个数 ,问在走过的步数不超过d的情况下 让所有边上的数的最大数最小是多少
思路:二分check mid 如果边上的数大于mid的就不能走这条边, 用pre[now]=pre[u] 记录路径,
因为边权为1 跑一下bfs找最短路即可, 注意要check成功才能更新路径
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn =1e5+10; 6 const int mod=1e9+7; 7 int n,m,d; 8 vector<pair<int,int>>E[maxn]; 9 int vis[maxn]; 10 int ans[maxn]; 11 vector<int>cnt; 12 int f=0; 13 struct ac 14 { 15 int p,step; 16 }; 17 18 int check(int x) 19 { 20 memset(vis,0,sizeof(vis)); 21 22 queue<ac>q; 23 q.push({1,0}); 24 vis[1]=1; 25 while(!q.empty()) 26 { 27 ac u=q.front(); 28 q.pop(); 29 if(u.step>d) 30 return 0; 31 if(u.p==n) 32 { 33 cnt.clear(); 34 int x=n; 35 while(ans[x]!=-1) 36 { 37 cnt.pb(x); 38 x=ans[x]; 39 } 40 reverse(cnt.begin(),cnt.end()); 41 f=1; 42 return 1; 43 } 44 for(auto v:E[u.p]) 45 { 46 if(vis[v.first]||v.second>x) 47 continue; 48 ans[v.first]=u.p; 49 vis[v.first]=1; 50 q.push({v.first,u.step+1}); 51 } 52 } 53 return 0; 54 } 55 56 57 int main() 58 { 59 ios::sync_with_stdio(false); 60 cin.tie(0); 61 cin>>n>>m>>d; 62 for(int i=1;i<=m;i++) 63 { 64 int u,v,w; 65 cin>>u>>v>>w; 66 E[u].pb({v,w}); 67 } 68 int l=0,r=1e9; 69 ans[0]=-1; 70 while(l<=r) 71 { 72 int mid=(l+r)/2; 73 if(check(mid)) 74 { 75 r=mid-1; 76 } 77 else 78 l=mid+1; 79 } 80 if(f==0) 81 { 82 cout<<-1<<'\n'; 83 return 0; 84 } 85 86 cout<<cnt.size()-1<<'\n'; 87 for(auto &v:cnt) 88 { 89 cout<<v<<" "; 90 } 91 92 93 94 95 96 97 }

浙公网安备 33010602011771号