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 poleN is 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

题解:二分搜索 在判断条件上使用Dijkstra求最少的花费 令mid为所需的答案 那么长度大于mid即为需要免费的电线 这些电线的数量与k比较后确定答案的位置 
 #include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int inf = + ;
const int maxn = + ;
const int maxp = + ;
const int maxl = + ;
typedef pair<int, int> P;
struct pole {
int b, l;
pole() {}
pole(int b, int l) : b(b), l(l) {}
};
vector<pole> G[maxp];
int d[maxn];
int n, p, k;
//x长度内免费
//返回路线上大于k的边数即所需要免费的数量
int Dijkstra(int s, int x) {
priority_queue<P, vector<P>, greater<P> > que;
fill(d, d + n, inf);
d[s] = ;
que.push(P(, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
for (int i = ; i < G[v].size(); i++) {
pole e = G[v][i];
//长度大于x的花费记为1
int nd = d[v] + (e.l > x ? : );
if (d[e.b] > nd) {
d[e.b] = nd;
que.push(P(d[e.b], e.b));
}
}
}
return d[n-];
}
int main(int argc, char const *argv[]) {
scanf("%d%d%d", &n, &p, &k);
for (int i = ; i < p; i++) {
int a, b, l;
scanf("%d%d%d", &a, &b, &l);
a--, b--;
G[a].push_back(pole(b, l));
G[b].push_back(pole(a, l));
}
//二分搜索
//让长度大于mid的免费
int lb = , ub = maxl;
while (ub > lb) {
int mid = (lb + ub) / ;
if (Dijkstra(, mid) > k) {
lb = mid + ;
} else {
ub = mid;
}
}
if (lb == maxl) {
puts("-1");
} else {
printf("%d\n", ub);
}
return ;
}

POJ 3662 Telephone Lines (二分 + 最短路)的更多相关文章

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

    查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...

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

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

  3. POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)

    题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...

  4. POJ 3662 Telephone Lines (二分+dijkstra)

    题意: 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人. 该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话 ...

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

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

  6. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

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

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

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

  8. poj 3662 Telephone Lines

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

  9. POJ 3662 Telephone Lines (分层图)

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

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

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

随机推荐

  1. 20180824 SSRS Line Chart 绘制

    (很多时候我都会只记录遇到的问题点,很少详细的写整个过程) 1. 安装ReportBulider 客户端,不需要填写server url ,可以先放空,后面再维护. 安装包官网可以下载,是免费的,现在 ...

  2. 萌新接触前端的第一课——HTML

    HTML web服务本质(好吧这个先不用知道也可以) import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOC ...

  3. 云服务器--linux系统操作命令以及安装ngnix记录,以及手动部署本地文件

    1.控制台登陆服务器,需要首先知道服务器ip和密码,,命令是 ssh root@1.1.1.1(服务器IP),然后输入密码登入服务器 2.查看linux 版本的系统命令是 cat /etc/redha ...

  4. Mysql表中唯一编号的分配机制

    最近遇到一个问题:高并发环境下,如何避免MYSQL一张表里的某些列不要重复. 同其他博友一样 https://blog.csdn.net/jacketinsysu/article/details/51 ...

  5. Scala对class/object反射

    近期有需求,要根据解析字符串,根据字符串的内容去调用方法.想到的基本就是使用反射.但是基本上能找到的资料,全没有讲scala的反射.只有零星点点的讲解.大部分都是用scala的语法写java反射最后翻 ...

  6. 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  7. 2019年UI和UX设计趋势:大概是最全的汇总了!

    朋友们,很快,2018就要跟我们说

  8. QT linux

    一. 如果你是服务器版的CentOS,我建议你安装一下图形界面 1. 首先安装X window: yum groupinstall 'X WindowSystem'2. 然后安装GNOME: yum ...

  9. android hook native函数

    大概2年前写的代码,今天突然要用到,找了半天,这里记录下 用到的库: https://pan.baidu.com/s/1htuUQX2 #include <jni.h> #include ...

  10. 图像中的stride含义

    这个不是卷积中的步长stride 是另外一个含义, stride = 每个像素所占字节数 * width input stride为我们正常进行卷积时候设置的stride值,output stride ...