Telephone Lines POJ - 3662(spfa+二分)
2018-03-22 13:46
579 查看
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)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)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)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)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
评价:好题,spfa的变式原因,因为答案存在单调行(假设答案存在),而且必然是从已知边里选择,关键怎么判断边的可行,只要把边大于mid的地阿亮看作长度为1,反正变为0,然后做spfa,如果num
的值小于等于k,则合法。
代码
There are N(1≤N≤1,000)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)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)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)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
评价:好题,spfa的变式原因,因为答案存在单调行(假设答案存在),而且必然是从已知边里选择,关键怎么判断边的可行,只要把边大于mid的地阿亮看作长度为1,反正变为0,然后做spfa,如果num
的值小于等于k,则合法。
代码
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<queue> #include<cstring> #define maxx 1005 #define maxn 10005 #define INF 0x3f3f3f3f using namespace std; int n,m,k; int head[maxx],_next[maxn<<1],to[maxn<<1],w[maxn<<1]; int num[maxx]; char inQ[maxx]; int cnt; void addEdge(int x,int y,int r) { to[++cnt]=y,w[cnt]=r,_next[cnt]=head[x],head[x]=cnt; to[++cnt]=x,w[cnt]=r,_next[cnt]=head[y],head[y]=cnt; } int b[maxn]; bool spfa(int x) { queue<int> que; memset(num,INF,sizeof(num)); num[1]=0; que.push(1); inQ[1]=true; while(que.size()) { int now=que.front(); que.pop(); inQ[now]=false; for(int i=head[now];i;i=_next[i]) { int L=w[i]>x?1:0; int v=to[i]; if(num[v]>num[now]+L) { num[v]=num[now]+L; if(!inQ[v]) { que.push(v); inQ[v]=true; } } } } return num <=k; } int main() { cin>>n>>m>>k; int x,y,w; cnt=0; for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&w); addEdge(x,y,w); b[i]=w; } if(spfa(0)) cout<<0<<endl; else { sort(b,b+m); //cout<<"haha"<<spfa(4)<<endl; int l=0,r=m-1; while(l<=r) { int mid=(l+r)>>1; if(spfa(b[mid])) r=mid-1; else l=mid+1; } if(l==m) cout<<-1<<endl; else cout<<b[l]<<endl; } return 0; }
相关文章推荐
- POJ 3662 Telephone Lines(二分答案+SPFA)
- POJ 3662 Telephone Lines 电话线(二分答案+SPFA)
- Poj 3662 Telephone Lines【二分+SPFA】
- POJ3662 Telephone Lines 二分+最短路
- POJ 3662 Telephone Lines 二分答案+djk
- 【POJ】3662 - Telephone Lines 二分+最短路
- POJ_3662_Telephone_Lines_(二分+最短路)
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
- POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)
- poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)
- POJ 3662 Telephone Lines (二分+dijkstra)
- POJ3662 Telephone Lines [二分 最短路]
- POJ - 3662 Telephone Lines (二分 + 最短路)
- bzoj 1614: [Usaco2007 Jan]Telephone Lines架设电话线【二分+spfa】
- POJ Telephone Lines 二分+dijkstra
- A - Telephone Lines POJ - 3662 第k+1条边尽量小
- POJ 3662 Telephone Lines(Dijkstra的邻接表存储)(可以作为模板题参考)
- 【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线 二分+SPFA
- poj 3662 Telephone Lines 图论
- bzoj 1614: [Usaco2007 Jan]Telephone Lines架设电话线 二分+spfa