[USACO08JAN]电话线Telephone Lines
二分+01BFS
#include<bits/stdc++.h> using namespace std; #define Max(x,y) (x)>(y)?(x):(y) #define Min(x,y) (x)>(y)?(y):(x) #define ll long long #define rg register const int N=1000+5,M=10000+5,inf=0x3f3f3f3f,P=99999997; int n,m,k,mx; template <class t>void rd(t &x){ x=0;int w=0;char ch=0; while(!isdigit(ch)) w|=ch=='-',ch=getchar(); while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); x=w?-x:x; } int head[N],tot=0; struct edge{int v,w,nxt;}e[M<<1]; void add(int u,int v,int w){ e[++tot]=(edge){v,w,head[u]},head[u]=tot; } int dis[N]; bool vis[N]; int check(int x){ memset(dis,0,sizeof(dis)); memset(vis,0,sizeof(vis)); deque<int>q; q.push_back(1),dis[1]=0,vis[1]=1; int flag=0; while(!q.empty()){ int u=q.front(); q.pop_front(); for(int i=head[u],v,w;i;i=e[i].nxt){ v=e[i].v,w=e[i].w; if(!vis[v]||dis[v]>=dis[u]+1){ if(w<=x) vis[v]=1,q.push_front(v),dis[v]=dis[u]; else vis[v]=1,q.push_back(v),dis[v]=dis[u]+1; if(v==n) flag=1; } } } if(!flag) return 2; if(dis[n]>k) return 0; else return 1; } int main(){ // freopen("in2.txt","r",stdin); rd(n),rd(m),rd(k); for(int i=1,u,v,w;i<=m;++i) rd(u),rd(v),rd(w),add(u,v,w),add(v,u,w),mx=Max(mx,w); int l=1,r=mx; if(check(inf)==2) {puts("-1");return 0;} while(l<r){ int mid=l+r>>1; if(check(mid)) r=mid; else l=mid+1; } if(check(0)) puts("0"); else if(l!=1) printf("%d",l); else puts("-1"); return 0; }