Poj3662 telephone lines binary + Shortest Path

Source: Internet
Author: User

 

Telephone Lines
Time Limit:1000 ms   Memory Limit:65536 K
Total Submissions:3468   Accepted:1245

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 areN(1 ≤N≤ 1,000) forlorn telephone poles conveniently numbered 1 ..NThat are scattered around Farmer John's property; no cables connect any them. A totalP(1 ≤
P≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.

TheI-Th cable can connect the two distinct polesAIAndBi, 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 poleNIs
The farm. Poles 1 andNNeed 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
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, AndK
* Lines 2 ..P+ 1: LineI+ 1 contains the three space-separated integers:AI,
Bi, AndLi

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

USACO 2008 January Silver binary + Shortest Path: Calculate a path from 1 to n to minimize the side of k + 1. If the original edge weight is less than or equal to the value of the new Edge Weight of the mid is 0, otherwise the new edge weight is 1. Find the shortest path. If the value is less than or equal to k, the condition is met. Code:
#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);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.