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的更多相关文章

  1. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  2. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. poj 3662 Telephone Lines

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7115   Accepted: 2603 D ...

  4. POJ 3662 Telephone Lines (分层图)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 D ...

  5. poj 3662 Telephone Lines(最短路+二分)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

  6. poj 3662 Telephone Lines dijkstra+二分搜索

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 D ...

  7. poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  8. POJ 3662 Telephone Lines (二分 + 最短路)

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...

  9. POJ 3662 Telephone Lines(二分答案+SPFA)

    [题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...

随机推荐

  1. mybatis-trim标签说明

    trim标签使用1.trim 有四个属性 2.prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容(注意:是没有prefixOverrides,suffixOverrides ...

  2. day01 包 权限修饰符 static final

  3. 19 标签:xml或者html

    1       标签:xml或者html 1.1  使用XmlSlurper解析xml groovy处理xml非常容易.XmlSlurper 类用来处理xml.在处理xml方面,还有其他的处理方式,但 ...

  4. 执行脚本 提示 command not found

    问题现象: 初学shell,写了个脚本, 1.从windows 写好 脚本,然后部署到 linux 上. 2.chmod +x之后执行提示command not found,系统环境redhat9,用 ...

  5. JavaScript Allongé 第一呷 :基础函数 (2)

    啊!我想要有一个参数 到现在为止,我们已经了解了没有参数的函数.只说我们的函数没有任何参数,甚至还没说参数是什么.大多数程序员非常熟悉参数,中学数学就讨论这个了.所以你知道他们是什么,而我也知道你知道 ...

  6. ztr loves lucky numbers 傻逼的我来了个大模拟

    http://acm.hdu.edu.cn/showproblem.php?pid=5676 这题的正解因该是dfs的,但是有18个位,然后我一算,全排列的话,有18!个啊,那不是很大?但是有很多是相 ...

  7. Gym 101149I I - It's the Police

    http://codeforces.com/gym/101149/problem/I 考虑下面这个例子 4 3 1 2 1 3 1 4 应该是选 0 0 1 1这样是最优的,我们不选1号,因为如果选1 ...

  8. MySQL简单的确定瓶颈

    如果接到报警可能需要ssh看看瓶颈是什么,怎么下手 确定os层 确定磁盘是否够用的:df –h 再看看系统整体状态: top 哪些进程占用资源比较多,能杀就杀 系统的负载 vmstat看看wa值,r列 ...

  9. 绘制复杂的原理图元件和pcb封装库用于cadence(一)

    绘制TI公司的TPS53319电源芯片封装 由于产品设计需要大电流电源供电,选用TI公司TPS53319电源芯片通过cadence软件进行电路设计,但是TI公司所提供的封装格式为CAD File(.b ...

  10. 在CentOS 6.7 64位安装PHP的PDO_OCI扩展 Installing PDO_OCI extension on CentOS 6.7 64bit

    需求&背景 最近根据项目需求,要在php中远程连接Oracel 11g Express数据库,为了开发方便,决定采用pdo,也就是php的PDO_OCI扩展,但是php安装的时候并没有安装PD ...