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 71 72 73 74 75 76 77 78 79 80 81
| #include <iostream> #include <cstdio> #include <queue> #include <climits> #define INF INT_MAX using namespace std; const int N = 1e3 + 10, M = 2e4 + 10; int n, m, k, dis[N]; int idx = -1, hd[N], nxt[M], edg[M], wt[M], lth[M]; bool vis[N]; void spfa() { for (int i = 1; i <= n; i++) dis[i] = INF; dis[1] = 0; queue <int> q; q.push(1); vis[1] = true; while (!q.empty()) { int t = q.front(); q.pop(); vis[t] = false; for (int i = hd[t]; ~i; i = nxt[i]) if (dis[t] + wt[i] < dis[edg[i]]) { dis[edg[i]] = dis[t] + wt[i]; if (!vis[edg[i]]) { q.push(edg[i]); vis[edg[i]] = true; } } } } bool check(int x) { for (int i = 0; i <= idx; i++) wt[i] = lth[i] > x; spfa(); return dis[n] <= k; } void add(int x, int y, int z) { nxt[++idx] = hd[x]; hd[x] = idx; edg[idx] = y; lth[idx] = z; } int main() { int l = 0, r = 0, mid; cin >> n >> m >> k; for (int i = 1; i <= n; i++) hd[i] = -1; for (int i = 1, a, b, c; i <= m; i++) { cin >> a >> b >> c; r = max(r, c); add(a, b, c); add(b, a, c); } for (int i = 0; i <= idx; i++) wt[i] = 1; spfa(); if (dis[n] == INF) { cout << -1; return 0; } while (l < r) { mid = l + r >> 1; if (check(mid)) r = mid; else l = mid + 1; } cout << l; return 0; }
|