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. 关闭掉eclipse启动的自动更新功能(提高打开eclipse的速度)

  2. ES6 数组去重 方法用了filter或者 indexOf Array.from

  3. 常见的Xshell运行命令

    最近接触到了Xshell这个软件,使用这个软件我们来进行连接Linux系统,进去之后我们可能会两眼一抹黑,小编就带大家来学些常见的shell命令. 首先我们要跟大家从最简单的聊起,我们进入Xshell ...

  4. json 添加 和删除两种方法

    <script> var test = { name: "name", age: "12" }; var countrys = { "ne ...

  5. How To: set udev rule for setting the disk permission on ASM disks when using multipath on Linux 6.x

    在RHEL6.4上安装11gR2的RAC时,使用了MULTIPATH来聚合绑定多路径的磁盘,并且修改磁盘的权限,赋予grid:asmadmin用户和组. 此时,在安装时可以发现磁盘,日志如下 INFO ...

  6. Codeforces Round #468 Div. 2题解

    A. Friends Meeting time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. [luogu2154 SDOI2009] 虔诚的墓主人(树状数组+组合数)

    传送门 Solution 显然每个点的权值可以由当前点上下左右的树的数量用组合数\(O(1)\)求出,但这样枚举会T 那么我们考虑一段连续区间,对于一行中两个常青树中间的部分左右树的数量一定,我们可用 ...

  8. SLF4J和Logback和Log4j和Logging的区别与联系

    本文转载自:一个著名的日志系统是怎么设计出来的?(作者:刘欣) 前言 Java帝国在诞生之初就提供了集合.线程.IO.网络等常用功能,从C和C++领地那里吸引了大量程序员过来加盟,但是却有意无意地忽略 ...

  9. 20180530利用Maxwell组件实时监听Mysql的binlog日志

    转自:https://blog.csdn.net/qq_30921461/article/details/78320750 http://kafka.apache.org/quickstart htt ...

  10. 【ACM】 hdu_1090_A+BII_201307261100

    A+B for Input-Output Practice (II)Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276 ...