问题 E: Telephone Lines
时间限制: 1 Sec 内存限制: 128 MB
提交: 28 解决: 10
[提交] [状态] [命题人:admin]
题目描述
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.
There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.
The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.
As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.
Determine the minimum amount that Farmer John must pay.
输入
* Line 1: Three space-separated integers: N, P, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li
输出
* Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.
样例输入
复制样例数据
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
样例输出
4
题意:找最短路从1到n,有k条路是免费,使剩余路中最长的那条最短(一开始理解成k距离是免费的...)
二分查找答案
对每一个mid,将距离>mid 的赋值1,<=mid 的赋值0,跑spfa,如果dis [ n ]>k,说明该路上除k条路外最大的距离不是mid ,不可行,小于则可行
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
const int inf = 0x3f3f3f3f;
int vis[maxn], dis[maxn];
int n, m, k, tot;
int edge[maxn], val[maxn], nex[maxn], hea[maxn];
struct node {
int u, v, d;
} s[maxn];
void add(int x, int y, int c) {
edge[++tot] = y;
val[tot] = c;
nex[tot] = hea[x];
hea[x] = tot;
}
bool spfa() {
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[1] = 0;
vis[1] = 1;
queue<int> q;
q.push(1);
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
for (int i = hea[x]; i; i = nex[i]) {
int y = edge[i], c = val[i];
if (dis[y] > dis[x] + c) {
dis[y] = dis[x] + c;
if (!vis[y]) q.push(y), vis[y] = 1;
}
}
}
if (dis[n] > k) return 0;
return 1;
}
bool check(int c) {
memset(hea, -1, sizeof(hea));
tot = 0;
for (int i = 1; i <= m; i++) {
if (s[i].d > c) {
add(s[i].u, s[i].v, 1);
add(s[i].v, s[i].u, 1);
} else {
add(s[i].u, s[i].v, 0);
add(s[i].v, s[i].u, 0);
}
}
if (spfa()) return 1;
return 0;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &s[i].u, &s[i].v, &s[i].d);
}
int l = 0, r = inf, ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid - 1;
ans = mid;
} else l = mid + 1;
}
printf("%d\n", ans);
return 0;
}