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

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

Source

这题意有毒  题意是
n个农场  每个农场通电话需要w[i]长度的电话线  其中k根电话线免费 
费用是这样定义的  除去这k根免费的电话线 剩下的最长的电话线作为花费的费用 问求最少花费的费用是多少 就是求第k+1大线的最小
我们二分答案  x;
用dijkstra算法  假如 边值>x  边值=1  否则等于0
所以我们只有统计这这条路径大于x的个数  如果大于k个 证明x还可以变大
否则x变小
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
struct node{
int to,next,w;
bool operator<(const node &a)const{
return w>a.w;
}
}edge[N*];
int t;
int head[N];
int vis[N],dis[N];
int n;
void init(){
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
t=;
for(int i=;i<N;i++)dis[i]=INF;
}
void add(int u,int v,int w){
edge[t].to=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
int Dijkstra(int x,int y){
memset(dis,INF,sizeof(dis));
dis[x]=;
node t1,t2;
t1.to=x;
priority_queue<node>q;
q.push(t1);
memset(vis,,sizeof(vis));
while(!q.empty()){
//cout<<2<<endl;
t1=q.top();
q.pop();
int u=t1.to;
if(vis[u]==)continue;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
int tt=;
if(edge[i].w>=y)tt=;
if(vis[v]==&&dis[v]>dis[u]+tt){
dis[v]=dis[u]+tt;
t2.to=v;
t2.w=dis[v];
q.push(t2);
}
}
}
return dis[n];
}
int main(){
int k,p;
while(scanf("%d%d%d",&n,&p,&k)!=EOF){
init();
int maxx=;
for(int i=;i<p;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
maxx=max(maxx,w);
}
int low=;
int high=maxx;
int ans=;
while(low<=high){
int mid=(low+high)>>;
//cout<<Dijkstra(1,mid)<<endl;
if(Dijkstra(,mid)<=k){
high=mid-;
}
else{
ans=mid;
low=mid+;
} }
if(low>maxx){cout<<-<<endl;continue;}
cout<<ans<<endl;
//for(int i=1;i<=n;i++)cout<<dis[i]<<endl;
}
}
 

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. Git的使用及托管代码到GitHub

    首先Git是一个开源的分布式版本控制工具,用git创建代码仓库. 仓库(Repository)是用于保存版本管理所需信息的地方,本地代码 提交到 代码仓库中,如果需要还可以 再推送到 远程仓库中. 所 ...

  2. Ajax——瀑布流

    基本概念 1.宽度是一致的,高度上参差不齐 2.新增内容优先放置在最矮的地方 核心难点 1.用一个数组存储每列的高度值 2.新值添加到值最小索引上,每次替换更新数组 插件使用 1.$.fn.exten ...

  3. Ajax——异步基础知识(一)

    基础概念 1.异步请求可以做到偷偷向服务器发送请求,而页面却不刷新 2.get异步请求传递参数是通过url追加键值对的方式 3.post异步请求比较特殊,需要设置请求的类型 User-Agent:浏览 ...

  4. 回顾Google IO 2016 -Keynote【图解】

    Google IO大会倒计时进行中~~ 两名演奏者在使用高空“古筝”. 最后5秒倒计时~~~~全场轰动~ 倒计时结束,IO大会正式开始.屏幕中,一个人把纯白的唱片放入唱片机中. 然后欢快的音乐响起,台 ...

  5. cordova插件分类

    1.android自动更新功能所需插件 cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.gi ...

  6. Centos7安装gitlab服务器

    1.先按照官方教程 https://about.gitlab.com/downloads/#centos7 大概内容如下: 1. Install and configure the necessary ...

  7. zoom,zoom与haslayout的关系,zoom与transform: scale( )的区别

    1.zoom:(缩放)

  8. mybatis批量操作(foreach)

    foreach可以在SQL语句中通过拼接的方式进行集合迭代.foreach元素的属性主要有collection,item,index,separator,open,close. item属性:表示循环 ...

  9. (C/C++学习)13.C语言字符串处理函数(一)

    说明:字符串处理的函数很多,本文将例举经常遇到的一些函数加以说明. 一.字符串的输入输出 头文件:<stdio.h> 1.利用标准输出函数 printf() 来输出,将格式设置为 s% . ...

  10. [HDU5807] Keep In Touch

    \(Keep\ In\ Touch\):保持联络 \(Informatik\ verbindet\ dich\ und\ mich.\) 信息将你我连结? 发现这个方程很容易列出来. \(f[i][j ...