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

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

Input

* 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

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个农场  每个农场通电话需要w[i]长度的电话线  其中k根电话线免费 
费用是这样定义的  除去这k根免费的电话线 剩下的最长的电话线作为花费的费用 问求最少花费的费用是多少 就是求第k+1大线的最小
我们二分答案  x;
用dijkstra算法  假如 边值>x  边值=1  否则等于0
所以我们只有统计这这条路径大于x的个数  如果大于k个 证明x还可以变大
否则x变小
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cctype>
  5. #include<cmath>
  6. #include<queue>
  7. #include<cstring>
  8. #include<map>
  9. #include<stack>
  10. #include<set>
  11. #include<vector>
  12. #include<algorithm>
  13. #include<string.h>
  14. typedef long long ll;
  15. typedef unsigned long long LL;
  16. using namespace std;
  17. const int INF=0x3f3f3f3f;
  18. const double eps=0.0000000001;
  19. const int N=+;
  20. struct node{
  21. int to,next,w;
  22. bool operator<(const node &a)const{
  23. return w>a.w;
  24. }
  25. }edge[N*];
  26. int t;
  27. int head[N];
  28. int vis[N],dis[N];
  29. int n;
  30. void init(){
  31. memset(vis,,sizeof(vis));
  32. memset(head,-,sizeof(head));
  33. t=;
  34. for(int i=;i<N;i++)dis[i]=INF;
  35. }
  36. void add(int u,int v,int w){
  37. edge[t].to=v;
  38. edge[t].w=w;
  39. edge[t].next=head[u];
  40. head[u]=t++;
  41. }
  42. int Dijkstra(int x,int y){
  43. memset(dis,INF,sizeof(dis));
  44. dis[x]=;
  45. node t1,t2;
  46. t1.to=x;
  47. priority_queue<node>q;
  48. q.push(t1);
  49. memset(vis,,sizeof(vis));
  50. while(!q.empty()){
  51. //cout<<2<<endl;
  52. t1=q.top();
  53. q.pop();
  54. int u=t1.to;
  55. if(vis[u]==)continue;
  56. vis[u]=;
  57. for(int i=head[u];i!=-;i=edge[i].next){
  58. int v=edge[i].to;
  59. int tt=;
  60. if(edge[i].w>=y)tt=;
  61. if(vis[v]==&&dis[v]>dis[u]+tt){
  62. dis[v]=dis[u]+tt;
  63. t2.to=v;
  64. t2.w=dis[v];
  65. q.push(t2);
  66. }
  67. }
  68. }
  69. return dis[n];
  70. }
  71. int main(){
  72. int k,p;
  73. while(scanf("%d%d%d",&n,&p,&k)!=EOF){
  74. init();
  75. int maxx=;
  76. for(int i=;i<p;i++){
  77. int u,v,w;
  78. scanf("%d%d%d",&u,&v,&w);
  79. add(u,v,w);
  80. add(v,u,w);
  81. maxx=max(maxx,w);
  82. }
  83. int low=;
  84. int high=maxx;
  85. int ans=;
  86. while(low<=high){
  87. int mid=(low+high)>>;
  88. //cout<<Dijkstra(1,mid)<<endl;
  89. if(Dijkstra(,mid)<=k){
  90. high=mid-;
  91. }
  92. else{
  93. ans=mid;
  94. low=mid+;
  95. }
  96.  
  97. }
  98. if(low>maxx){cout<<-<<endl;continue;}
  99. cout<<ans<<endl;
  100. //for(int i=1;i<=n;i++)cout<<dis[i]<<endl;
  101. }
  102. }
 

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

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

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

  2. POJ - 3662 Telephone Lines (dijstra+二分)

    题意:有N个独立点,其中有P对可用电缆相连的点,要使点1与点N连通,在K条电缆免费的情况下,问剩下的电缆中,长度最大的电缆可能的最小值为多少. 分析: 1.二分临界线(符合的情况的点在右边),找可能的 ...

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

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

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

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

  5. poj 3662 Telephone Lines

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

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

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

  7. POJ 3662 Telephone Lines (分层图)

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

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

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

  9. POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】

    <题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...

随机推荐

  1. java攻城师之路--复习java web之request_respone

    Servlet技术 两条主线1.HTTP协议 2.Servlet生命周期 init() 方法中参数 ServletConfig 对象使用通过ServletConfig 获得 ServletContex ...

  2. mysql常用命令介绍

    mysql适用于在Internet上存取数据,支持多种平台 1.主键:唯一标识表中每行的这个列,没有主键更新或删除表中的特定行很困难. 2.连接mysql可以用Navicat 要读取数据库中的内容先要 ...

  3. RHEL7配置中文输入法-智能拼音

    RHEL7配置中文输入法-智能拼音 RHEL7.x(CentOS7.x)系统相对之前的6.x系统变化较大,虽然安装时选择了中文环境,但是进入系统后,在控制台及编辑器中仍无法切换输入法进行中文输入. 原 ...

  4. 【sqli-labs】 less49 GET -Error based -String -Blind -Order By Clause(GET型基于盲注的字符型Order By从句注入)

    都是order by的注入,作者连图片都懒得改了... 注意和整型的区别,前引号用提交的引号闭合,后引号用#注释 http://192.168.136.128/sqli-labs-master/Les ...

  5. nagios 安装pnp4nagios插件

    Naigos install pnp4nagios 绘图插件 原文地址:http://www.cnblogs.com/caoguo/p/5022230.html [root@Cagios ~]# yu ...

  6. C# SetWindowsHookEx

    [DllImport("user32.dll")] static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookPr ...

  7. Xftp 5 和 Xshell 5 基本使用方法

    软件介绍: (1)Xshell: 一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的 TELNET 协议.Xshell通过互联网可以连接到远程的服 ...

  8. JavaFX桌面应用开发-鼠标事件和键盘事件

    鼠标相关事件的操作初始代码 package application; import javafx.application.Application;import javafx.event.ActionE ...

  9. Android之手机振动和振铃

    一.振动的实现1.使用振动所需的权限 <uses-permission android:name="android.permission.VIBRATE" />2.相关 ...

  10. chrony配置介绍

    rhel7 文档https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Adminis ...