P1948 [USACO08JAN]电话线Telephone Lines
感觉这个题极其眼熟,做过吗?
#include<bits/stdc++.h> using namespace std; const int bow=1010; const int INF=0x3f3f3f3f; int n,m,k,a,b,c,ne,head[bow],dis[1001],vist[1001]; struct node{int nxt,to,dis;}eg[100100]; void adde(int u,int v,int val){eg[++ne].nxt=head[u];eg[ne].to=v;eg[ne].dis=val;head[u]=ne;} int check(int w){ queue<int>q; for(int i=1;i<=n;i++)dis[i]=INF; q.push(1);dis[1]=0;vist[1]=1; while(!q.empty()){ int u=q.front(); q.pop();vist[u]=0; for(int i=head[u];i;i=eg[i].nxt) { int l=0; if(eg[i].dis>w)l=1; if(dis[eg[i].to]>dis[u]+l){ dis[eg[i].to]=dis[u]+l; if(!vist[eg[i].to]){vist[eg[i].to]=1;q.push(eg[i].to);} } } } if(dis[n]>k)return 0; else return 1; } int main() { cin>>n>>m>>k; while(m--) { cin>>a>>b>>c; adde(a,b,c); adde(b,a,c); } int l=0,r=1000005,ans=INF; while(l<=r) { int mid=(l+r)/2; if(check(mid)){ans=mid,r=mid-1;} else l=mid+1; } if(ans>=1000005)ans=-1; cout<<ans; }