[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次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...
随机推荐
- 干货:排名前16的Java工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- Yac - PHP扩展
1:首先你要安装Git [root@localhost]# git clone https://github.com/laruence/yac 2:进入yac目录进行配置 [root@localhos ...
- 蓝牙硬件交互数据传输Demo
#import "ViewController.h" #import <CoreBluetooth/CoreBluetooth.h> @interface ViewCo ...
- Linux —— 常用命令集合
关机和重启命令 shutdown [选项] 时间 (添加&,把关机任务放在后台执行) 只有shutdown可以保存关机时资源 操作选项 重启: r 关机: h 取消一个关机任务: c 关机命令 ...
- 093 Restore IP Addresses 复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式.例如:给定 "25525511135",返回 ["255.255.11.135", " ...
- JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
contextmenu事件 该事件用以表示何时应该显示上下文菜单,以便开发者取消默认的上下文菜单,转而提供自定义的菜单. 因为该事件属于鼠标事件,所以其事件对象中包含与光标位置有关的所有属性.如: & ...
- 第八章 设计用户界面 之 给Web程序应用用户界面设计
1. 概述 本章内容包括: 使用CSS创建和应用样式.使用HTML构架用户界面的层次 以及 根据需求实现动态页面内容. 2. 主要内容 2.1 使用CSS创建和应用样式 Razor程序的模板是_Lay ...
- mongodb 正则
正则表达式常用来在所有语言中搜索字符串的任何模式或文字.MongoDB还提供了正则表达式功能的字符串模式使用正则表达式$regex操作符.MongoDB使用PCRE(Perl兼容正则表达式)为正则表达 ...
- babel-loader7和babel8版本的问题
根据官网https://www.npmjs.com/package/babel-loader要对应版本 一.babel7.X版本 1.要安装的包 第1套包:npm i babel-core babe ...
- Somethings about Floors题解
题目内容:一个楼梯有N级(N >=0), 每次走1级或2级, 从底走到顶一共有多少种走法? 输入要求:只有一行输入,并且只有一个数N(如果N > 20,则N = N%21,即保证N的范围控 ...