>Link
ybtoj通信线路
>解题思路
求最大值最小,可以考虑二分答案
二分以后如何处理呢?我们将二分出来的mid作为标准值,边权大于mid的为1,小于等于的为0,这样跑一个spfa,如果1到n的最短路超过了k,说明路径上大于mid的数多于k,当前mid作为答案是不行的
>代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 1010
#define M 10010
using namespace std;
struct edge
{
int to, next, w;
} e[2 * M];
int n, m, k, h[N], cnt, l, r, mid, c[N], ans;
bool y[N];
void add (int u, int v, int w)
{
e[++cnt] = (edge){v, h[u], w}; h[u] = cnt;
e[++cnt] = (edge){u, h[v], w}; h[v] = cnt;
}
bool spfa (int x)
{
memset (c, 0x7f, sizeof (c));
queue<int> q;
q.push(1); y[1] = 1;
c[1] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
y[u] = 0;
for (int i = h[u]; i; i = e[i].next)
{
int v = e[i].to;
if (c[v] > c[u] + (e[i].w > x))
{
c[v] = c[u] + (e[i].w > x);
if (!y[v])
{
q.push(v);
y[v] = 1;
}
}
}
}
if (c[n] == c[0])
{
printf ("-1");
exit (0); //注意要判断是否有通路的情况
}
return c[n] <= k;
}
int main()
{
freopen ("phone.in", "r", stdin);
freopen ("phone.out", "w", stdout);
int u, v, w;
scanf ("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++)
{
scanf ("%d%d%d", &u, &v, &w);
add (u, v, w);
}
l = 0, r = 1000000;
while (l < r)
{
mid = (l + r) / 2;
if (spfa (mid)) r = mid;
else l = mid + 1;
}
printf ("%d", r);
return 0;
}