[POJ] 3362 Telephone Lines
Telephone Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7978 Accepted: 2885
Description
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
Source
USACO 2008 January Silver
求一条从1到n的路,删去k条最大的边后,剩余的第k-1大边最小
(可以DAG DP 回头补充)
考虑二分答案,枚举k-1条边长度,大于的置为1,小于的置为0,跑最短路,检查答案
#include<iostream>
#include<queue>
using namespace std;
const int MAXN=20000;
const int INF=1<<28;
struct Edge{
int next,to,w;
}e[MAXN];
int ecnt,head[MAXN];
inline void add(int x,int y,int w){
e[++ecnt].next = head[x];
e[ecnt].to = y;
e[ecnt].w = w;
head[x]=ecnt;
}
int n,m,st,k;
int mid;
bool inq[MAXN];
int dis[MAXN];
void spfa(){
queue<int> Q;
for(int i=1;i<=n;i++) inq[i]=0,dis[i]=INF;
Q.push(1); inq[1]=1; dis[1]=0;
while(!Q.empty()){
int top=Q.front();
Q.pop() ;inq[top]=0;
for(int i=head[top];i;i=e[i].next){
int v=e[i].to ;
int w=e[i].w > mid;
if(dis[v]>dis[top]+w){
dis[v]=dis[top]+w;
if(!inq[v]) Q.push(v);
}
}
}
}
int main(){
cin.sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>k;
int l=0,r=-INF;
for(int i=1;i<=m;i++){
int x,y,w;
cin>>x>>y>>w;
add(x,y,w);
add(y,x,w);
l=min(l,w);
r=max(r,w);
}
int tmp=++r;
while(l<r){
mid=(l+r)>>1;
spfa();
if(dis[n]<=k){
r=mid;
}else{
l=mid+1;
}
}
if(l>=tmp){
cout<<-1<<endl;
return 0;
}
cout<<l<<endl;
return 0;
}
[POJ] 3362 Telephone Lines的更多相关文章
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- 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(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
- poj 3662 Telephone Lines dijkstra+二分搜索
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accepted: 2071 D ...
- poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)
Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...
- POJ 3662 Telephone Lines (二分 + 最短路)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...
- POJ 3662 Telephone Lines(二分答案+SPFA)
[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...
随机推荐
- python 之 函数 迭代器
5.9 迭代器 5.91 可迭代对象和迭代器对象 1.什么是迭代?:迭代是一个重复的过程,并且每次重复都是基于上一次的结果而来 2.要想了解迭代器到底是什么?必须先了解一个概念,即什么是可迭代的对象? ...
- Spring security + oauth2.0 + redis + mybatis plus 搭建微服务
上个星期一个朋友请求帮忙,让我搭建一个分布式授权中心的微服务,之前我也没搭建过,在网上撸了几天前辈们写的技术博客,搞出个模型,分享给大家: 前辈们博客地址: OAuth2.0 原理:https://b ...
- Java反编译工具JD-GUI以及Eclipse的反编译插件
什么是反编译 高级语言源程序经过编译变成可执行文件,反编译就是逆过程.但是通常不能把可执行文件变成高级语言源代码,只能转换成汇编程序. 反编译是一个复杂的过程,所以越是高级语言,就越难于反编译,但目前 ...
- spring+mybits 整合所需jar包的下载路径(亲测有效)
1.spring jar包:http://repo.springsource.org/libs-release-local/org/springframework/spring/5.0.0.RELEA ...
- 洛谷P2599||bzoj1413 [ZJOI2009]取石子游戏
bzoj1413 洛谷P2599 根本不会啊... 看题解吧 #include<cstdio> #include<algorithm> #include<cstring& ...
- VMware下OSSIM 4.1.0的下载、安装和初步使用(图文详解)
不多说,直接上干货! 为什么,我写了一篇OSSIM 5.2.0的,还要再来写OSSIM 4.1.0呢,是因为,OSSIM 5.2.0所需内存较大,8G甚至16G,但是,肯定性能和里面集成组件越高级.也 ...
- Unity AssetBundle笔记
1.入门: Resources:表示U3D自动将资源打成一个AssetBundle包,所有放在Resources下的文件夹都会打成一个AssetBundle包,资源非常大,Resources文件夹在真 ...
- volatile双重锁实现单例
双重锁实现单例时遭到质疑,既是:双重锁也无法保证单例模式! 原因是:指令会重排序,普通的变量仅仅会保证该方法在执行时,所有依赖的赋值结果是正确的,但不会保证执行顺序! 为什么会重排序:指令重排序是指c ...
- vue-cli脚手架(框架)
一.创建vue项目 npm install vue-cli -g #-g全局 (sudo)npm install vue-cli -g #mac笔记本 vue-init webpack myvue # ...
- CF1093D Beautiful Graph
思路: 题目倒是没啥好说的,就是注意memset的效率问题.如果循环多次调用memset去初始化一个比较大的数组,那就会很费时间.就是因为这个被hack了.:( 实现: #include <bi ...