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 ...
随机推荐
- 怎么修改TOMCAT的默认主页为你自己项目的主页
如果webapp下有一个abc的文件来下有一个index.html,想设置为首页怎么操作 方法: 修改tomcat/conf/web.xml文件.在web.xml文件中,有一段如下:<welco ...
- python基础入门--input标签、变量、数字类型、列表、字符串、字典、索引值、bool值、占位符格式输出
# 在python3 中: # nian=input('>>:') #请输入什么类型的值,都成字符串类型# print(type(nian)) # a = 2**64# print(typ ...
- Auth认证模块
1.auth简介 auth是什么? auth是django内置的用户认证系统,可以快速的实现:登录,注销,修改密码........ 2.autho的使用 1)先创建超级用户 python3 manag ...
- nginx命令行参数
通过控制台进入nginx目录后 1. 启动nginx start nginx 或 nginx.exe 2. 重启nginx nginx -s reload 3. 停止nginx nginx -s st ...
- (转)redis分布式锁-SETNX实现
Redis有一系列的命令,特点是以NX结尾,NX是Not eXists的缩写,如SETNX命令就应该理解为:SET if Not eXists.这系列的命令非常有用,这里讲使用SETNX来实现分布式锁 ...
- 17-Python3 循环语句
2018-11-21 18:23:56 print('pass语句') for letter in 'Runoob': if letter=='o': pass else: print(letter) ...
- [Java in NetBeans] Lesson 04. Class / Objects
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Class: Blueprint for an object. (e.g. dog is a class) Object: cust ...
- js模拟队列----小优先队列
队列:先进先出,后进后出 var Queue = (function(){ var item = new WeakMap(); class Queue{ constructor(){ item.set ...
- pd.concat/merge/join
pandas的拼接分为两种: 级联:pd.concat, pd.append 合并:pd.merge, pd.join 一.回顾numpy.concatenate 生成1个6*3的矩阵,一个2*3的矩 ...
- 一个简单的sel server 函数的自定义
创建自定义函数:use 数据库名gocreate function 函数名(@pno int)returns intasbegin declare @a int if not exists(se ...