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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
int n,p,k;
int d[];
int head[],ver[],edge[],Next[];//开两倍存储双向边
bool v[];
int tot=;
priority_queue<pair<int,int> >q;
void add(int x,int y,int z)
{
ver[++tot]=y;
edge[tot]=z;
Next[tot]=head[x];
head[x]=tot;
}
int dijkstra(int mid)//花费小于等于mid记为0 否则记为1 //只需要为一条路买单 //dij返回的是大于mid的数
{
int cnt=;
memset(d,0x3f,sizeof(d));
memset(v,,sizeof(v));
d[]=;
q.push(make_pair(,));
int i,j;
while(q.size())
{
int x=q.top().second;
q.pop();
if(v[x])continue;
v[x]=;
for(i=head[x];i;i=Next[i])
{
int y=ver[i],z=edge[i];
int z1;
if(z<=mid)z1=;
else z1=;
if(d[y]>d[x]+z1)// 注意 这里要求的最短路并不是原来费用的最短路
{
d[y]=d[x]+z1;
q.push(make_pair(-d[y],y));
}
}
}
return d[n];
}
bool check(long long mid)
{
if(dijkstra(mid)<=k)
{
return true;
}
else return false;
}
int main()
{
int i;
memset(v,,sizeof(v));
scanf("%d%d%d",&n,&p,&k);
for(i=;i<=p;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
long long l=,r=,mid;
while(l<r)
{
mid=(l+r)>>;
if(check(mid))//钱数够了缩小
{
r=mid;
}
else//钱数不够扩大
{
l=mid+;
}
}
if(r>)cout<<-<<endl;
else if(r<)cout<<<<endl;
else cout<<l<<endl;
return ;
}

POJ3662 Telephone Lines (dijkstra+二分)的更多相关文章

  1. POJ3662 Telephone Lines( dijkstral + 二分 )

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

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

    题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...

  3. poj3662 Telephone Lines【最短路】【二分】

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

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

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

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

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

  6. POJ3662 [USACO08JAN]Telephone Lines (二分答案/分层图求最短路)

    这道题目有两种解法: 1.将每个点视为一个二元组(x,p),表示从起点到x有p条路径免费,相当于构建了一张分层图,N*k个节点,P*k条边.在这张图上用优先队列优化的SPFA算法求解,注意这里的d数组 ...

  7. 【POJ3662】Telephone Lines dij + 二分答案

    题目大意:给定一个 N 个顶点,M 条边的无向图,求一条从 1 号节点到 N 号节点之间的路径,使得第 K+1 大的边权最小,若 1 与 N 不连通,输出 -1. 最小化最大值一类的问题,采用二分答案 ...

  8. POJ-3662 Telephone Lines 二分+双端队列

    题目传送门 题意:有n个点, p条路,每条道路有个花费Li, 然后现在要建一条1-n的路线,然后可以选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. 转:unittest的几种运行方式

    #unittest-test.py import unittestfrom demo import RunMainimport HtmlTestRunner class TestMethod(unit ...

  2. java基础(六)之继承初探

    什么是继承? 一个类得到了另一个类当中的成员变量和成员方法.java只支持单继承.一个子类只允许继承一个父类,一个父类可以被多个子类继承. 比如下面的一个例子, 先创建一个Person类 class ...

  3. MySQL性能优化---优化方案

    1.对查询进行优化,应尽量避免全表查询,首先考虑在where及order by涉及的列上建立索引: 2.应尽量避免where子句中对字段进行null值判断,否则将导致引擎放弃使用索引而进行全表扫描: ...

  4. JavaScript arguments对象浅析

    arguments对象 概念 用法 属性 arguments对象 概念 arguments 是一个类数组对象.代表传给一个function的参数列表. 用法 function a() { consol ...

  5. 如何处理python异常

    1.python异常有那些? window的机器如果安装了python,则直接可以在idle中查看,打开idle,按F1即可打开帮助文档,按如下路径即可查看,也可以去python官网查看这里不说明了百 ...

  6. 存储过程(Stored Procedure)

    存储过程中 IN,OUT,INOUT类型参数的区别 IN:输入参数:表示该参数的值必须在调用存储过程时指定赋值,在存储过程中修改该参数的值不能被返回,为默认值 OUT:在存储过程内部,该值的默认值为N ...

  7. 【音乐欣赏】《JINGO JUNGLE》 - MYTH & ROID

    歌名:JINGO JUNGLE 作者:MYTH & ROID [00:19.82]Don’t go away 逃げ場はない [00:27.12]Oh why? You’re crying 嗤え ...

  8. C语言报错:“gets”: 找不到标识符。解决方法

    C语言报错:“gets”: 找不到标识符. 把“gets”改成“gets_s”即可.

  9. jsTree获取选中节点和选中指定节点

    jstree获取当前选中的checkbox和获取选中节点的所有节点 首先初始化一个带有复选框的 jstree $('#demo_tree').jstree({ "core" : { ...

  10. 【转载】巴塞尔问题(Basel Problem)的多种解法

    如何计算 \(\displaystyle \zeta \left ( 2 \right )=\frac{1}{1^{2}}+\frac{1}{2^{2}}+\frac{1}{3^{2}}+\cdots ...