POJ 3662 (二分+SPFA
Posted mj-liylho
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3662 (二分+SPFA相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8856 | Accepted: 3211 |
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
1 #include"bits/stdc++.h" 2 #define db double 3 #define ll long long 4 #define vl vector<ll> 5 #define ci(x) scanf("%d",&x) 6 #define cd(x) scanf("%lf",&x) 7 #define cl(x) scanf("%lld",&x) 8 #define pi(x) printf("%d ",x) 9 #define pd(x) printf("%f ",x) 10 #define pl(x) printf("%lld ",x) 11 #define rep(i, n) for(int i=0;i<n;i++) 12 using namespace std; 13 const int N = 1e6 + 5; 14 const int mod = 1e9 + 7; 15 const int MOD = 998244353; 16 const db PI = acos(-1.0); 17 const db eps = 1e-10; 18 int R() 19 { 20 int f=1,x=0; 21 char e=getchar(); 22 while(e<‘0‘||e>‘9‘){if(e==‘-‘)f=-1;e=getchar();} 23 while(e>=‘0‘&&e<=‘9‘){x=x*10+e-‘0‘;e=getchar();} 24 return f*x; 25 } 26 27 const int inf=1128481603; 28 int n,m,k; 29 struct P{int v,dis,nxt;}E[50005]; 30 int head[N],tot; 31 int d[2005],vis[2005];//数组开太大会T 32 int l=0,r=1e6+10,mid; 33 34 void add(int u,int v,int dis) 35 { 36 E[++tot].nxt=head[u]; 37 E[tot].v=v; E[tot].dis=dis; 38 head[u]=tot; 39 } 40 41 void SPFA() 42 { 43 memset(d,67,sizeof(d)); d[1]=0; 44 queue<int> q; q.push(1); 45 memset(vis,0,sizeof(vis)); 46 while(!q.empty()) 47 { 48 int u=q.front(); 49 q.pop(); vis[u]=0; 50 for(int i=head[u];i;i=E[i].nxt) 51 { 52 int v=E[i].v,dis=(E[i].dis>mid); 53 if(d[v]>d[u]+dis) 54 { 55 d[v]=d[u]+dis; 56 if(!vis[v])q.push(v),vis[v]=1; 57 } 58 } 59 } 60 } 61 62 int main() 63 { 64 n=R();m=R();k=R(); 65 for(int i=1;i<=m;++i) 66 { 67 int u=R(),v=R(),dis=R(); 68 add(u,v,dis);add(v,u,dis); 69 } 70 int ans=-1; 71 while(l<=r) 72 { 73 mid=(l+r)>>1; 74 SPFA(); 75 if(d[n]==inf){ printf("-1"); return 0;} 76 if(d[n]<=k) ans=mid,r=mid-1; 77 else l=mid+1; 78 } 79 printf("%d",ans); 80 return 0; 81 }
以上是关于POJ 3662 (二分+SPFA的主要内容,如果未能解决你的问题,请参考以下文章
poj3662Telephone Lines——二分+最短路
POJ3662Telephone Lines(最短路+二分)