#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int g[1010][1010];
int n,m,k;
bool st[N];
int dist[N];
struct Edge{
int a,b,c;
}edge[N];
bool check(int x){
memset(g,0x3f,sizeof g);
for(int i=1;i<=n;i++)g[i][i] = 0;
for(int i=1;i<=m;i++){
int a =edge[i].a,b = edge[i].b,c = edge[i].c;
if(c<=x)g[a][b] = g[b][a] = 0;
else g[a][b] = g[b][a] = 1;
}
memset(dist,0x3f,sizeof dist);
dist[1] = 0;
memset(st,0,sizeof st);
for(int i=1;i<=n;i++){
int t = -1;
for(int j=1;j<=n;j++)
if(!st[j]&&(t==-1||dist[j]<dist[t]))t = j;
st[t] = 1;
for(int j=1;j<=n;j++)
dist[j] = min(dist[j],dist[t]+g[t][j]);
}
return dist[n]<=k;
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=m;i++){
int a,b,c;cin>>a>>b>>c;
edge[i] = {a,b,c};
}
int l = -1,r = 1000010 ;
while(l+1!=r){
int mid = l+r>>1;
if(check(mid))r = mid;
else l = mid;
}
if(r!=1000010)cout<<r;
else cout<<-1;
}
二分+dijkstra
最新推荐文章于 2024-08-29 22:25:44 发布