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. 2019牛客多校第四场A meeting 思维

    meeting 题意 一个树上有若干点上有人,找出一个集合点,使得所有人都到达这个点的时间最短(无碰撞) 思路 就是找树的直径,找直径的时候记得要找有人的点 #include<bits/stdc ...

  2. SRAM速度提升思路及方法

    SRAM总体分为两大部分,一部分是存储阵列,另一部分是外围辅助电路.提高SRAM工作速度从这两大方面着手. ·存储阵列 对于存储阵列,首先可以通过降低工艺节点,以达到提高器件本身速度,从而提高整体SR ...

  3. Django中的check指令和sqlmigrate指令

    官方文档的解释如下: Django 有一个自动执行数据库迁移并同步管理你的数据库结构的命令 - 这个命令是 migrate,我们马上就会接触它 - 但是首先,让我们看看迁移命令会执行哪些 SQL 语句 ...

  4. jquery获取select多选框选中的文本值

    $("#select option:selected").text();

  5. 如和针对CPU时间百分比,Mem使用bytes,以及Network RecvBytes/SendBytes指标性能压测数据可视化

    设计思路:通过jmeter5.1压测获取cpu,Mem,Network的压测指标数据利用pandas+openpyxl进行数据可视化: 涉及添加jar包:下载地址:https://files.cnbl ...

  6. CSS-透明背景色兼容

    IE 不支持透明背景色 使用fileter div{ backgournd: #666; filter:alpha(opacity=50); -moz-opacity:0.5; opacity:0.5 ...

  7. Form DataGridView绑定BindingSource的几种方式

    本文链接:https://blog.csdn.net/qq_15138169/article/details/83341076 在WinForm的开发中,ListView和DataGridView应用 ...

  8. 2019沈阳网赛树形dp

    https://nanti.jisuanke.com/t/41403 2019沈阳网络赛D题 树形dp.一棵树,求任意两个点的距离之和.u-v和v-u算两次.两点之间的距离分为三类,模3等于0,1,2 ...

  9. 吴裕雄 python 机器学习——人工神经网络与原始感知机模型

    import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D from ...

  10. promise封装ajax

    promise的含义(本身不是异步,是封装异步操作的容器,统一异步的标准) promise对象的特点:对象的状态不受外界影响:一旦状态改变,就不会再变,任何时候都可以得到这个结果. function ...