http://poj.org/problem?id=3662

Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:9310   Accepted: 3374

Description

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 pole N 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

Source

 
题意:
【感觉题意描述很不清啊,一会长度一会段的】
有$n$个点,$p$条边,每条边有一个权值(花费)。将第$1$个点和第$n$个点连通,并且可以有$k$条边是免费的。剩下的不免费的边的最大值作为最终的花费。求最终花费的最小值。
思路:
刚开始题意理解错了。以为是有$k$长度的是免费的,边的那个权值是长度。想了半天搞不懂。
后来发现其实就是求一个使路径上第$k+1$大的边权尽量小的路径。【虽然还是不会】
因为当我们支付的钱变多时,合法的路径一定包含了花费更少的路径。答案具有单调性。
所以我们可以二分答案。【注意想题目答案的单调性尝试二分】
这时候问题就变成了,把价格超过$mid$的边花费看成是$1$,不超过的边花费看成是$0$,然后求$1~N$的最短路是否不超过$k$就可以了。
要注意考虑$1$和$N$不连通的情况,输出是$-1$
 
虐狗宝典上还有一个思路是用dp,但是我不是很会写。
用$D[x,p]$表示从$1$号节点到基站$x$,途中已经指定了$p$条电缆免费时,经过的路径上最贵的电缆的花费最小是多少(选择一条$1$到$x$的路径,使路径上第$p+1$大的边权尽量小)。若有一条从$x$到$y$长度是$z$的无向边,用$max(D[x,p],z)$更新$D[y,p]$的最小值,用$D[x,p]$更新$D[y, p+1]$的最小值。前者表示不在电缆$(x,y,z)$上使用免费升级服务,后者表示使用。用迭代的思想,借助SPFA进行动态规划,直至所有状态收敛。
还可以把图中节点拓展到二维,用二元组$(x,p)$表示一个节点,从$(x,p)$到$(y,p)$有长度为$z$的边,从$(x,p)$到$(y,p+1)$有长度为0的边。问题就变成了$N*K$个点,$P*K$条边的广义单源最短路问题。
  1. #include<iostream>
  2. //#include<bits/stdc++.h>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<cstring>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<vector>
  10. #include<set>
  11. #include<climits>
  12. using namespace std;
  13. typedef long long LL;
  14. #define N 100010
  15. #define pi 3.1415926535
  16.  
  17. const int maxn = ;
  18. const int maxp = ;
  19.  
  20. int n, p, k;
  21. struct node{
  22. int v, w, nxt;
  23. }e[maxp * ];
  24. int tot = , head[maxn];
  25. LL dis[maxn];
  26. bool vis[maxn];
  27.  
  28. void addedge(int u, int v, int w)
  29. {
  30. e[tot].v = v;
  31. e[tot].w = w;
  32. e[tot].nxt = head[u];
  33. head[u] = tot++;
  34. e[tot].v = u;
  35. e[tot].w = w;
  36. e[tot].nxt = head[v];
  37. head[v] = tot++;
  38. }
  39.  
  40. int dijkstra(int mid)
  41. {
  42. memset(dis, 0x3f, sizeof(dis));
  43. memset(vis, , sizeof(vis));
  44. dis[] = ;
  45. priority_queue<pair<LL, int> >que;
  46. que.push(make_pair(, ));
  47. while(que.size()){
  48. int x = que.top().second;que.pop();
  49. if(vis[x])continue;
  50. vis[x] = true;
  51. for(int i = head[x]; i != -; i = e[i].nxt){
  52. int y = e[i].v, z = e[i].w;
  53. if(z > mid)z = ;
  54. else z = ;
  55. if(dis[y] > dis[x] + z){
  56. dis[y] = dis[x] + z;
  57. que.push(make_pair(-dis[y], y));
  58. }
  59. }
  60. }
  61. return dis[n];
  62. }
  63.  
  64. int main()
  65. {
  66. while(scanf("%d%d%d", &n, &p, &k) != EOF){
  67. for(int i = ; i < n; i++){
  68. head[i] = -;
  69. }
  70. tot = ;
  71.  
  72. int ed = -;
  73. for(int i = ; i < p; i++){
  74. int u, v, w;
  75. scanf("%d%d%d", &u, &v, &w);
  76. ed = max(ed, w);
  77. addedge(u, v, w);
  78. }
  79.  
  80. //printf("%d\n", dijkstra(0));
  81. if(dijkstra() == )printf("-1\n");
  82. else{
  83. int st = , ans;
  84. while(st <= ed){
  85. int mid = (st + ed) / ;
  86. if(dijkstra(mid) <= k){
  87. ans = mid;
  88. ed = mid - ;
  89. }
  90. else{
  91. st = mid + ;
  92. }
  93. }
  94. printf("%d\n", ans);
  95. }
  96.  
  97. }
  98. return ;
  99. }

poj3662 Telephone Lines【最短路】【二分】的更多相关文章

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

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

  2. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...

  3. POJ3662 Telephone Lines (dijkstra+二分)

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...

  4. POJ3662 Telephone Lines( dijkstral + 二分 )

    POJ3662 Telephone Lines 题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的, 求这m-k条边中花费最大的一条边的最 ...

  5. Luogu P1948 [USACO08JAN]电话线Telephone Lines(最短路+dp)

    P1948 [USACO08JAN]电话线Telephone Lines 题意 题目描述 Farmer John wants to set up a telephone line at his far ...

  6. poj-3662 Telephone Lines 二分答案+最短路

    链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...

  7. [Usaco2007 Jan]Telephone Lines架设电话线[二分答案+最短路思想]

    Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...

  8. BZOJ1614:[USACO]Telephone Lines架设电话线(二分,最短路)

    Description FarmerJohn打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司 支付一定的费用.FJ的农场周围分布着N(1<=N< ...

  9. BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)

    题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...

随机推荐

  1. 九个PHP很有用的功能

    1. 函数的任意数目的参数 你可能知道PHP允许你定义一个默认参数的函数.但你可能并不知道PHP还允许你定义一个完全任意的参数的函数 下面是一个示例向你展示了默认参数的函数: // 两个默认参数的函数 ...

  2. 如何打造千万级Feed流系统

    from:https://www.cnblogs.com/taozi32/p/9711413.html 在互联网领域,尤其现在的移动互联网时代,Feed流产品是非常常见的,比如我们每天都会用到的朋友圈 ...

  3. 如何用一个for循环打印出一个二维数组

    思路分析: 二维数组在内存中默认是按照行存储的,比如一个二维数组{{1,2,3,},{4,5,6}},它在内存中存储的顺序就是1.2.3.4.5.6,也就是说,对于这6个数组元素,按照从0到5给它们编 ...

  4. Java实现匿名内部类的简单应用

    在查看数码相片时,通常会使用一款图片查看软件,该软件应该能遍历文件夹下的所有图片并进行显示.编写程序,实现一个图片查看软件,它可以支持6张图片,通过单击不同的按钮就可以查看不同的图片. 思路分析:就是 ...

  5. 利用OpenSSL库对Socket传输进行安全加密(RSA+AES)

    轉自:http://blog.chinaunix.net/uid-9543173-id-3921143.html 利用OpenSSL库对Socket传输进行安全加密(RSA+AES) 1. 利用RSA ...

  6. backbone学习笔记:模型(Model)(1)基础知识

    backbone为复杂Javascript应用程序提供MVC(Model View Controller)框架,框架里最基本的是Model(模型),它用来处理数据,对数据进行验证,完成后台数据与前台数 ...

  7. NFS 常见报错

    问题:客户端挂载共享目录后,不管是root用户还是普通用户,创建新文件时属主属组都为nobody解决方法:这种情况会出现在 centos6 或 NFS 4版本中,只要在挂载的时候加上 -o nfsve ...

  8. VS无法导航到插入点F12失败

    关闭VS 开启控制台并导航到Visual安装文件夹,例如C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\ID ...

  9. 关于vc工程包含多个lib库老是提示无法打开问题

    在一个VC项目中,我要包含五个lib库,我在连接器->常规->附加库目录中输入了正确的库包含路径,然后再连接器->输入->附加依赖项中输入:ws2_32.lib;wsock32 ...

  10. GNU Readline库函数的应用示例

    说明 GNU Readline是一个跨平台开源程序库,提供交互式的文本编辑功能.应用程序借助该库函数,允许用户编辑键入的命令行,并提供自动补全和命令历史等功能.Bash(Bourne Again Sh ...