[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次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...
随机推荐
- LeetCode.897-递增搜索树(Increasing Order Search Tree)
这是悦乐书的第346次更新,第370篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第211题(顺位题号是897).给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节 ...
- 5本自然语言处理书单-附pdf
文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 自然语言处理(英语:Natural Language Processing,缩写作 NLP)是人工智能 ...
- D - Bomb
//反向62 #include <iostream> #include <algorithm> #include <string> #include <cst ...
- python模块之hmac
# hmac模块使用步骤: # hmac模块模块的使用步骤与hashlib模块的使用步骤基本一致,只是在第1步获取hmac对象时,只能使用hmac.new()函数, # 因为hmac模块没有提供与具体 ...
- MDX之Case When用法
with member [Measures].[终端销售数量总计] as sum(ytd([日期].[年月].CurrentMember),[Measures].[终端销售数量]) member [M ...
- centos7安装mysql5.7 使用yum
https://blog.csdn.net/z13615480737/article/details/78906598 使用yum,比较简单,不用考虑版本依赖问题
- Oracle如何创建表空间
create user frame identified by tiger; grant create session to frame; grant create table to frame; g ...
- HTML中实现Table表头点击升序/降序排序
题目:如下图,请实现表格信息的排序功能,当点击表头的属性区域,将表格信息进行排序切换功能,即第一次点击为降序排序,再一次点击进行升序排序. 姓名 力量 敏捷 智力 德鲁伊王 17 24 13 月之骑士 ...
- Json的详细用法
参考博客:https://www.cnblogs.com/haiyan123/p/7829080.html 1.json(Javascript Obiect Notation,JS对象标记)是一种 ...
- js图片预加载以及延迟加载
当我们需要做图片轮播的时候,如果让图片提前下载到本地,用浏览器缓存起来,我们可以用Image对象: function preLoadImg(){ var img=new Image(); img.sr ...