POJ 3662 Telephone Lines (二分 + 最短路)
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 poleN 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 题解:二分搜索 在判断条件上使用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 (二分 + 最短路)的更多相关文章
- POJ 3662 Telephone Lines(二分+最短路)
查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...
- poj 3662 Telephone Lines(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
- POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)
题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...
- POJ 3662 Telephone Lines (二分+dijkstra)
题意: 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人. 该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话 ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
- poj 3662 Telephone Lines
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7115 Accepted: 2603 D ...
- POJ 3662 Telephone Lines (分层图)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6785 Accepted: 2498 D ...
- poj 3662 Telephone Lines dijkstra+二分搜索
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accepted: 2071 D ...
随机推荐
- redgate的mysql架构比较和数据比较工具
redgate的mysql架构比较和数据比较工具 最近线上数据需要进行架构比较,比较两个服务器上的mysql实例上数据库的架构 数据比较可以用percona的pt-table-checksum和pt- ...
- 报错解决——OSError: libdarknet.so: cannot open shared object file: No such file or directory
在python目录下打开终端,输入 python darknet.py 结果报错 Traceback (most recent call last): File “darknet.py”, line ...
- Python日期与字符串互转
import datetime #str -> date detester = '2017-01-01' date = datetime.datetime.strptime(detester,' ...
- c# 文件或者文件夹改名的最简单方法
使用cmd命令 ren Process.Start( "cmd", "/C " + "ren f:\\first c code.txt zhou.tx ...
- [vue]模拟移动端三级路由: router-link位置体现router的灵活性
小结 router-link可以随便放 router-view显示的是父组件的直接子组件的内容 想研究下移动三级路由的逻辑, 即 router-link和router-view 点首页--点新闻资讯( ...
- 使用Python监控Linux系统
一.Python编写的监控工具 一.多功能系统资源统计工具dstat 1.dstat介绍 dstat是一个用Python语言实现的多功能系统资源统计工具,用来取代Linux下的vmstat.iosta ...
- 对于jquery实现原理的浅谈
关键词:prototype(原型).它能让javascript的方法(也可看成:类)能够动态地追加方法(猜测:目的是为了代码实现引入“类的思想”) 废话少说,代码见真义. <html> & ...
- 【LeetCode每天一题】Group Anagrams(变位词组)
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- mysql数据库字段类型详解
MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型.本节首先给出可用类型的一个概述,并且总结每个列类型的存储需求,然后提供每个类中的类型性质的更详细的描述. ...
- 乐固加固后windows下实现给apk签名
遇到了这样一个问题:我们已经在centos下签名生成好的apk,拿到腾讯乐固上加固以后,签名没有了,就需要重新签名,我乐滋滋的想,既然原来是在centos下签名的,那再去centos上签名一次就好了, ...