POJ3662 Telephone Lines 二分+最短路
来源:互联网 发布:js事件三个阶段 编辑:程序博客网 时间:2024/05/24 05:11
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 ofP (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 andBi, with lengthLi (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 andN 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 11 2 53 1 42 4 83 2 35 2 93 4 74 5 6
Sample Output
4
Source
#include<cstdio>#include<cstring>#define N 1005int n,m,k,num,adj[N],low[N],f[N],q[N];struct edge{int v,w,c,pre;}e[N*20];void insert(int u,int v,int w){e[num].v=v;e[num].c=w;e[num].pre=adj[u];adj[u]=num++;}int spfa(int x){int i,v,head=0,tail=0;memset(f,0,sizeof(f));memset(low,0x7f,sizeof(low));low[x]=0;q[++tail]=x;while(head!=tail){x=q[head=(head+1)%N];f[x]=0;for(i=adj[x];~i;i=e[i].pre)if(low[v=e[i].v]>low[x]+e[i].w){low[v]=low[x]+e[i].w;if(!f[v]){f[v]=1;q[tail=(tail+1)%N]=v;}}}return low[n]<=k;}int ok(int x){int i,j;for(i=1;i<=n;i++)for(j=adj[i];~j;j=e[j].pre)if(e[j].c<=x)e[j].w=0;elsee[j].w=1;return spfa(1);}int main(){int u,v,w;while(~scanf("%d%d%d",&n,&m,&k)){num=0;memset(adj,-1,sizeof(adj));int l=0,r=0,mid,ans=-1;while(m--){scanf("%d%d%d",&u,&v,&w);insert(u,v,w);insert(v,u,w);if(w>r)r=w;}while(l<=r){mid=(l+r)/2;if(ok(mid)){ans=mid;r=mid-1;}elsel=mid+1;}printf("%d\n",ans);}}
- POJ3662 Telephone Lines 二分+最短路
- POJ3662 Telephone Lines [二分 最短路]
- POJ3662 Telephone Lines 二分+最短路
- Poj3662 Telephone Lines (限制最短路)
- POJ - 3662 Telephone Lines (二分 + 最短路)
- POJ3662 二分+最短路
- POJ3662--Telephone Lines
- POJ3662-Telephone Lines
- POJ3662-Telephone Lines
- poj3662 Telephone Lines
- Poj3662 Usaco2008JanSilver Telephone Lines
- Poj 3662 Telephone Lines(最短路+二分)
- *【bzoj 1614】Telephone Lines架设电话线(二分+最短路)
- poj3662,二分 + 最短路,好题啊
- BZOJ 1614: [Usaco2007 Jan]Telephone Lines架设电话线 二分答案 最短路
- bzoj 1614: [Usaco2007 Jan]Telephone Lines架设电话线 二分+最短路
- POJ3662,洛谷1948 [USACO08JAN]电话线Telephone Lines
- POJ Telephone Lines 二分+dijkstra
- trie树--详解
- XP系统下全新安装windows 7
- SQLServer 约束有?
- Scite调试lua脚本新问题及解决
- 用ASP.NET做一个自己的新闻发布RSS
- POJ3662 Telephone Lines 二分+最短路
- linux启动
- 洗牌的学问
- C语言运算符优先级和结合性表
- (复习笔记之数据库)数据库流程控制语句
- 线程和进程间的相似处与区别
- 深入C++的new和traits
- java多线程应用——生产者消费者
- 表达式求值(c++实现)