假定一个解可行,然后二分判断,判断给出的值是不是可以到达终点,我们使用bfs搜一下
#include<bits/stdc++.h>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define LL long long
#define pb push_back
#define gcd __gcd
#define For(i,j,k) for(int i=(j);i<k;i++)
#define lowbit(i) (i&(-i))
#define _(x) printf("%d\n",x)
const int maxn = 1e4+10;
const int inf = 1 << 28;
int n,m,k,t;
vector<pair<int,int> > G[maxn];
int d[maxn];
bool bfs(int s,int val){
queue<int> q;
fill(d,d+maxn,inf);
q.push(s);
d[s] = 0;
while(!q.empty()){
int u = q.front();q.pop();
if(d[u]>k)return false;
if(u==t)return true;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].first;
int w = G[u][i].second;
if(w<=val){
if(d[v]>d[u]+1){
d[v] = d[u]+1;
q.push(v);
}
}
}
}
return false;
}
int main(){
while(~scanf("%d%d%d%d",&n,&m,&k,&t)){
for(int i=0;i<maxn;i++)G[i].clear();
for(int i=0;i<m;i++){
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
G[x].pb(make_pair(y,w));
G[y].pb(make_pair(x,w));
}
int l = 0,r = inf;
while(l<r){
int mid = l+r>>1;
if(bfs(1,mid))r=mid;
else l=mid+1;
}
// printf("l = %d, r = %d\n",l,r);
printf("%d\n",r);
}
return 0;
}