POJ 3662 Telephone Lines (二分 + 最短路)

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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and poleN is at the farm. Poles 1 and 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.

Input

* Line 1: Three space-separated integers: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, and Li

Output

* 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.

Sample Input

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

Sample Output

4

题解:二分搜索 在判断条件上使用Dijkstra求最少的花费 令mid为所需的答案 那么长度大于mid即为需要免费的电线 这些电线的数量与k比较后确定答案的位置
 1 #include <cstdio>
 2 #include <vector>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 const int inf = 1000 + 10;
 7 const int maxn = 1000 + 10;
 8 const int maxp = 10000 + 10;
 9 const int maxl = 1000000 + 10;
10 typedef pair<int, int> P;
11 struct pole {
12     int b, l;
13     pole() {}
14     pole(int b, int l) : b(b), l(l) {}
15 };
16 vector<pole> G[maxp];
17 int d[maxn];
18 int n, p, k;
19 //x长度内免费
20 //返回路线上大于k的边数即所需要免费的数量
21 int Dijkstra(int s, int x) {
22     priority_queue<P, vector<P>, greater<P> > que;
23     fill(d, d + n, inf);
24     d[s] = 0;
25     que.push(P(0, s));
26     while (!que.empty()) {
27         P p = que.top();
28         que.pop();
29         int v = p.second;
30         if (d[v] < p.first)
31             continue;
32         for (int i = 0; i < G[v].size(); i++) {
33             pole e = G[v][i];
34             //长度大于x的花费记为1
35             int nd = d[v] + (e.l > x ? 1 : 0);
36             if (d[e.b] > nd) {
37                 d[e.b] = nd;
38                 que.push(P(d[e.b], e.b));
39             }
40         }
41     }
42     return d[n-1];
43 }
44 int main(int argc, char const *argv[]) {
45     scanf("%d%d%d", &n, &p, &k);
46     for (int i = 0; i < p; i++) {
47         int a, b, l;
48         scanf("%d%d%d", &a, &b, &l);
49         a--, b--;
50         G[a].push_back(pole(b, l));
51         G[b].push_back(pole(a, l));
52     }
53     //二分搜索
54     //让长度大于mid的免费
55     int lb = 0, ub = maxl;
56     while (ub > lb) {
57         int mid = (lb + ub) / 2;
58         if (Dijkstra(0, mid) > k) {
59             lb = mid + 1;
60         } else {
61             ub = mid;
62         }
63     }
64     if (lb == maxl) {
65         puts("-1");
66     } else {
67         printf("%d
", ub);
68     }
69     return 0;
70 }
原文地址:https://www.cnblogs.com/wydxry/p/7274827.html