传送门
Luogu
解题思路
考虑二分,每次把大于二分值的边的权设为1,小于等于的设为0,如果最短路<=k则可行,记得判无解
细节注意事项
- 咕咕咕
参考代码
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cctype> #include <cmath> #include <ctime> #include <queue> #define rg register using namespace std; template < typename T > inline void read(T& s) { s = 0; int f = 0; char c = getchar(); while (!isdigit(c)) f |= (c == '-'), c = getchar(); while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar(); s = f ? -s : s; } const int _ = 1002; const int __ = 10002; int tot, head[_], nxt[__ << 1], ver[__ << 1], w[__ << 1]; inline void Add_edge(int u, int v, int d) { nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; } int n, m, k, exi[_], dis[_]; inline bool check(int mid) { static queue < int > Q; memset(exi + 1, 0, sizeof (int) * n); memset(dis + 1, 0x3f, sizeof (int) * n); dis[1] = 0, exi[1] = 1, Q.push(1); while (!Q.empty()) { int u = Q.front(); Q.pop(), exi[u] = 0; for (rg int i = head[u]; i; i = nxt[i]) { int v = ver[i], d = (w[i] > mid); if (dis[v] > dis[u] + d) { dis[v] = dis[u] + d; if (!exi[v]) Q.push(v); } } } return dis[n] <= k; } int main() { #ifndef ONLINE_JUDGE freopen("in.in", "r", stdin); #endif read(n), read(m), read(k); for (rg int u, v, d; m--; ) read(u), read(v), read(d), Add_edge(u, v, d), Add_edge(v, u, d); int l = 0, r = 1e6 + 1; while (l < r) { int mid = (l + r) >> 1; if (check(mid)) r = mid; else l = mid + 1; } if (r == (int) 1e6 + 1) puts("-1"); else printf("%d\n", r); return 0; }
完结撒花 \(qwq\)