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

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

Sample Output

  1. 4
  2.  
  3. 题解:二分搜索 在判断条件上使用Dijkstra求最少的花费 mid为所需的答案 那么长度大于mid即为需要免费的电线 这些电线的数量与k比较后确定答案的位置
  1. #include <cstdio>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6. const int inf = + ;
  7. const int maxn = + ;
  8. const int maxp = + ;
  9. const int maxl = + ;
  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] = ;
  25. que.push(P(, 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 = ; i < G[v].size(); i++) {
  33. pole e = G[v][i];
  34. //长度大于x的花费记为1
  35. int nd = d[v] + (e.l > x ? : );
  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-];
  43. }
  44. int main(int argc, char const *argv[]) {
  45. scanf("%d%d%d", &n, &p, &k);
  46. for (int i = ; 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 = , ub = maxl;
  56. while (ub > lb) {
  57. int mid = (lb + ub) / ;
  58. if (Dijkstra(, mid) > k) {
  59. lb = mid + ;
  60. } else {
  61. ub = mid;
  62. }
  63. }
  64. if (lb == maxl) {
  65. puts("-1");
  66. } else {
  67. printf("%d\n", ub);
  68. }
  69. return ;
  70. }

POJ 3662 Telephone Lines (二分 + 最短路)的更多相关文章

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

    查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...

  2. poj 3662 Telephone Lines(最短路+二分)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

  3. POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)

    题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...

  4. POJ 3662 Telephone Lines (二分+dijkstra)

    题意: 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人. 该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话 ...

  5. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

  6. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  7. poj 3662 Telephone Lines spfa算法灵活运用

    意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...

  8. poj 3662 Telephone Lines

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7115   Accepted: 2603 D ...

  9. POJ 3662 Telephone Lines (分层图)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 D ...

  10. poj 3662 Telephone Lines dijkstra+二分搜索

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 D ...

随机推荐

  1. DedeCMS后台500错误一种原因是不支持PHP5.3、5.4及以上版本

    我们在迁移网站的时候,可能会出现DedeCMS后台500错误,有可能是因为dedecms不支持PHP5.3.5.4及以上版本,这时我们要改动一些设置才能修复成功.跟着ytkah来修改配置文件吧.首先打 ...

  2. 显示日期的指令: date

    1.显示日期的指令: date (1)参数: (2)实例

  3. 初学git

    初学git,总结了一点东西,可能有理解和操作的不到位的地方,还有就是这个是我之前写在word上的,因为CSDN上不能直接上传,所以拷贝的过程中也可能有其他问题.发的的朋友还望指正... 1.找到“参与 ...

  4. Redis入门到高可用(三)——通用命令

    通用命令  参考 http://redisdoc.com/index.html 1. keys  #查看所有key 时间复杂度:O(N), N 为数据库中 key 的数量. 127.0.0.1:637 ...

  5. 删除 clean tomcat7:run

    1.在eclipse中运行的绿色箭头旁边有个下箭头,点击: 2.选择Run Configurations... 3.在Maven Builder下删除不想要的

  6. np.Linear algebra学习

    转自:https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.linalg.html 1.分解 //其中我觉得可以的就是svd奇异值分解吧 ...

  7. [LeetCode] 704. Binary Search_Easy tag: Binary Search

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  8. python简单的ftp程序

    服务器端 '''1.读取文件名2.检测文件是否存在3.打开文件4.检测文件大小5.发送文件大小给客户端6.等客户端确认7.开始边读边发数据8.发送md5'''import socket,os,time ...

  9. Node.js进击基础一(5-5http知识填坑)

    蚂蚁部落:谷歌浏览器network用法详解 http://www.softwhy.com/forum.php?mod=viewthread&tid=19119 按下f12->Networ ...

  10. lldb使用

    常用 si,ni ,ex, mem,di,reg,b,c,x 以下内容是lldb帮助文档中内容: apropos           -- List debugger commands related ...