1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <bits/stdc++.h> #define pii pair<int, int> #define FAST_IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std;
const int INF = 0x3f3f3f3f; const int Maxn = 1e3+5; struct node{ int to, w; }; vector <node> V[Maxn]; int n, m, k; int Dis[Maxn], Vis[Maxn]; priority_queue <pii, vector<pii>, greater<pii> > q;
int Dij(int mid) { memset(Vis, 0, sizeof(Vis)); memset(Dis, INF, sizeof(Dis)); Dis[1] = 0; q.push(pii(0,1)); while(q.size()) { int x = q.top().second; q.pop(); if(Vis[x]) continue; Vis[x] = 1; for(int i = 0; i < V[x].size(); i++) { int y = V[x][i].to, w = (V[x][i].w >= mid); if(Dis[y] > Dis[x] + w){ Dis[y] = Dis[x] + w; q.push(pii(Dis[y],y)); } } } return Dis[n]; } int main() { FAST_IO; while(cin >> n >> m >> k) { int x, y, z; int Max = 0; for(int i = 0; i <= n; i++) V[i].clear(); for(int i = 0; i < m; i++) { cin >> x >> y >> z; Max = max(Max, z); V[x].push_back((node){y, z}); V[y].push_back((node){x, z}); } int ans = 0, l = 0, r = Max; while(l <= r) { int mid = (l+r) / 2; if(Dij(mid) <= k){ r = mid - 1; } else { ans = mid; l = mid + 1; } } if(l > Max) cout << -1 << endl; else cout << ans << endl; } return 0; }
|