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

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 Nis 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

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

/*
* @Author: Lyucheng
* @Date: 2017-07-24 11:08:55
* @Last Modified by: Lyucheng
* @Last Modified time: 2017-07-24 17:36:49
*/
/*
题意:题意很迷,有n个点,p条边,现在要修一条从1到n的线路,你可以将其中的k条线权值变为0,并且你修的线路中
最长的边,一定要是最小的 思路:二分答案,将大于mid的边权值为1,小于等于的为0,找出最少耗费的1,如果小于k那么就可以通过
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> #define MAXN 1005
#define INF 0x3f3f3f3f
#define MAXR 1000000 using namespace std; int n,p,k;
int u,v,w;
struct HeapNode //Dijkstra算法用到的优先队列的节点
{
int d,u;
HeapNode(int d,int u):d(d),u(u){}
bool operator < (const HeapNode &rhs)const
{
return d > rhs.d;
}
}; struct Edge //边
{
int from,to,dist;
Edge(int f,int t,int d):from(f),to(t),dist(d){}
}; struct Dijkstra
{
int n,m; //点数和边数,编号都从0开始
vector<Edge> edges; //边列表
vector<int> G[MAXN];//每个节点出发的边编号(从0开始编号)
bool done[MAXN]; //是否已永久标号
int d[MAXN]; //s到各个点的距离
int p[MAXN]; //p[i]为从起点s到i的最短路中的最后一条边的编号 void init(int n)
{
this->n=n;
for(int i=;i<n;i++) G[i].clear();//清空邻接表
edges.clear(); //清空边列表
} void AddEdge(int from,int to,int dist)
{//如果是无向图,每条无向边调用两次AddEdge
edges.push_back(Edge(from,to,dist) );
m = edges.size();
G[from].push_back(m-);
} bool dijkstra(int s,int pos)//求s到所有点的距离
{
priority_queue<HeapNode> Q;
for(int i=;i<n;i++) d[i]=INF;
d[s]=;
memset(done,,sizeof(done));
Q.push(HeapNode(,s) ); while(!Q.empty())
{
HeapNode x=Q.top(); Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]= true; for(int i=;i<G[u].size();i++)
{
Edge& e= edges[G[u][i]];
int new_dist=e.dist>pos?:;
if(d[e.to]> d[u]+new_dist)
{
d[e.to] = d[u]+new_dist;
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to],e.to) );
}
}
}
if(d[n-]>k) return false;
else return true;
}
}DJ; int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(scanf("%d%d%d",&n,&p,&k)!=EOF){
DJ.init(n);
for(int i=;i<p;i++){
scanf("%d%d%d",&u,&v,&w);
DJ.AddEdge(u-,v-,w);
DJ.AddEdge(v-,u-,w);
}
int res=-;
int l=,r=MAXR,m;
while(l<=r){
m=(l+r)/;
if(DJ.dijkstra(,m)==true){//如果这个长度可以完成的话
r=m-;
res=m;
}else{//如果不可以完成的话
l=m+;
}
}
printf("%d\n",res);
}
return ;
}

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【Dijkstra最短路+二分求解】

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

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

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

  4. POJ 3662 Telephone Lines (分层图)

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

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

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

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

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

  7. poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  8. POJ 3662 Telephone Lines(二分答案+SPFA)

    [题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...

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

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

随机推荐

  1. Day3 文件操作和函数

    一 文件操作 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 1.1打开文件读取内容 print(open("sounds","r", ...

  2. Java 数组扩容

    在添加数据到达数组的上限的时候数组进行扩容: public void resizeArrayCaptcity(){ if(size>=arr.length){ Emp [] arr2=new   ...

  3. U方法

    U方法用于完成对URL地址的组装,特点在于可以自动根据当前的URL模式和设置生成对应的URL地址,格式为:U('地址','参数','伪静态','是否跳转','显示域名');在模板中使用U方法而不是固定 ...

  4. Codeforces 858A. k-rounding 数论

    题目: 题意:输入n和k,找到一个最小的数,满足末尾有至少k个0和是n的倍数. 最小的情况 ans = n,最大的情况 ans = n*pow(10,k). 令 k = pow(10,k); 我们发现 ...

  5. GCD XOR uvalive6657

    GCD XORGiven an integer N, nd how many pairs (A; B) are there such that: gcd(A; B) = A xor B where1 ...

  6. Python数据分析(二): Numpy技巧 (3/4)

    numpy.pandas.matplotlib(+seaborn)是python数据分析/机器学习的基本工具. numpy的内容特别丰富,我这里只能介绍一下比较常见的方法和属性.   昨天晚上发了第一 ...

  7. python之路第五篇之递归(进阶篇:续:经典例子剖析)

    递归 在函数内部,可以调用其他函数; 如果一个函数在内部调用自身本身,这个函数就是递归函数. 例如,我们来计算阶乘: n! = 1 x 2 x 3 x ... x n, 用函数f1(n)表示,可以看出 ...

  8. 即时通信系统Openfire分析之五:会话管理

    什么是会话? A拨了B的电话 电话接通 A问道:Are you OK? B回复:I have a bug! A挂了电话 这整个过程就是会话. 会话(Session)是一个客户与服务器之间的不中断的请求 ...

  9. 【转】python数据格式化之pprint

    pprint – 美观打印 作用:美观打印数据结构 pprint 包含一个“美观打印机”,用于生成数据结构的一个美观视图.格式化工具会生成数据结构的一些表示,不仅可以由解释器正确地解析,而且便于人类阅 ...

  10. Jmeter脚本录制方法(二)——手工编写脚本(jmeter与fiddler结合使用)

    jmeter脚本录制方法可以分三种,前几天写的一篇文章中,已介绍了前两种,今天来说下第三种,手工编写脚本,建议使用这一种方法,虽然写的过程有点繁琐,但调试脚本比前两者方式都要便捷. 首先来看下三种方式 ...