选择二分答案求解,注意无向边!!!!!
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
int maxn;
int n, m, k, t;
struct node{
int to, dist;
};
int sum[100010];
vector<node> g[100010];
bool go(int x){
memset(sum, 0, sizeof(sum));
queue<int> q;
q.push(1);
while(!q.empty()){
int u = q.front();
q.pop();
if(u == t) return true;
if(sum[u] == k) continue;
int siz = g[u].size();
for(int i = 0; i < siz; i++){
int v = g[u][i].to;
if(!sum[v] && g[u][i].dist <= x){
sum[v] = sum[u] + 1;
q.push(v);
}
}
}
return false;
}
int main(){
cin >> n >> m >> k >> t;
for(int i = 1, u, v, w; i <= m; i++){
cin >> u >> v >> w;
maxn = max(w, maxn);
g[u].push_back({v, w});
g[v].push_back({u, w});
}
int l = 0, r = maxn, mid;
while(l < r){
mid = (l+r) / 2;
if(go(mid)) r = mid;
else l = mid+1;
}
//cout << mid << " " << l << " " << r;
cout << r << endl;
return 0;
}