Telephone Lines

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7214   Accepted: 2638

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 ≤ 1000) forlorn telephone warehouse gap numbered 1.. N that are scattered around Farmer John’s property; No cables connect any of 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. I'm sorry, but I'm sorryCopy the code

Sample Output

4
Copy the code

Source

USACO 2008 January Silver

Title link: poj.org/problem?id=…

The question: A total of N pole, P for poles can be connected, connected with lines between poles can communicate with each other, now want to make poles 1 and pole N can communicate with each other, and the wire company K of the wire can be free to use, when using the number of wire more than K, beyond the wire charge, The total charge is the length of the longest wire after removing the K free wires. Now need to reduce the cost as much as possible, ask what is the minimum cost \

Then, according to the value of bisection, the whole graph is treated with 0,1: set all the edges larger than the value of bisection to 1, set all the edges smaller than the value of bisection to 0, and then find the shortest path from 1 to n. But if the number of edges from 1 to n is less than or equal to k, the answer is 0

Here is the AC code:

 

1 #include <iostream> 2 #include<algorithm> 3 #include<queue> 4 #include<stack> 5 #include<cmath> 6 #include<string.h> 7  #include<stdio.h> 8 #include<stdlib.h> 9 #include <set> 10 #include <map> 11 using namespace std; 12 inline int read() 13 { 14 int x=0,f=1; 15 char ch=getchar(); 16 while(ch<'0'||ch>'9') 17 { 18 if(ch=='-') 19 f=-1; 20 ch=getchar(); 21 } 22 while(ch>='0'&&ch<='9') 23 { 24 x=x*10+ch-'0'; 25 ch=getchar(); 26 } 27 return x*f; 28 } 29 inline void write(int x) 30 { 31 if(x<0) 32 { 33 putchar('-'); 34 x=-x; 35 } 36 if(x>9) 37 write(x/10); 38 putchar(x%10+'0'); 39 } 40 struct Node 41 { 42 int id,dis; 43}; 44 bool operator<(const Node &a,const Node &b) 45 { 46 return a.dis>b.dis; 47 } 48 typedef struct 49 { 50 int v,next,cost; 51 }Edge; 52 Edge e[50050]; 53 int head[1010]; 54 int d[1010]; 55 int n,p,k; 56 int cnt; 57 inline bool BFS() 58 { 59 int vis[1010]; 60 memset(vis,false,sizeof(vis)); 61 queue<int>Q; 62 Q.push(1); 63 vis[1]=1; 64 while(! Q.empty()) 65 { 66 int u=Q.front(); 67 Q.pop(); 68 for(int i=head[u]; i! = 1; i=e[i].next) 69 { 70 int v=e[i].v; 71 if(vis[v]) 72 continue; 73 vis[v]=1; 74 if(v==n) 75 return 1; 76 Q.push(v); 77 } 78 } 79 return 0; 80 } 81 inline int Dijkstra(int ans) 82 { 83 priority_queue<Node>q; 84 Node temp,pp; 85 bool vis[1010]; 86 for(int i=1; i<=n; i++) 87 { 88 d[i]=1e+8; 89 vis[i]=0; 90 } 91 d[1]=0; 92 temp.id=1; 93 temp.dis=0; 94 q.push(temp); 95 while(! q.empty()) 96 { 97 pp=q.top(); 98 q.pop(); 99 int u=pp.id; 100 if(vis[u]) 101 continue; 102 vis[u]=1; 103 for(int i=head[u]; i! = 1; i=e[i].next) 104 { 105 int v=e[i].v; 106 int t=e[i].cost>ans? true:false; 107 if(! vis[v]) 108 { 109 if(d[v]>d[u]+t) 110 { 111 d[v]=d[u]+t; 112 pp.id=v; 113 pp.dis=d[v]; 114 q.push(pp); 115 } 116 } 117 } 118 } 119 return d[n]; 120 } 121 int main() 122 { 123 int fr,to,cost; 124 while(scanf("%d%d%d",&n,&p,&k)! =EOF) 125 { 126 cnt=1; 127 memset(e,0,sizeof(e)); 128 memset(head,-1,sizeof(head)); 129 for(int i=0; i<p; i++) 130 { 131 fr=read(); 132 to=read(); 133 cost=read(); 134 e[cnt].v=to; 135 e[cnt].next=head[fr]; 136 e[cnt].cost=cost; 137 head[fr]=cnt; 138 cnt++; 139 e[cnt].v=fr; 140 e[cnt].next=head[to]; 141 e[cnt].cost=cost; 142 head[to]=cnt; 143 cnt++; 144 } 145 if(! BFS()) 146 { 147 puts("-1"); 148 continue; 149 } 150 int l=0,r=1000010,mid; 151 while(l<=r) 152 { 153 mid=(l+r)/2; 154 if(Dijkstra(mid)<=k) 155 r=mid-1; 156 else 157 l=mid+1; 158 } 159 write(r+1); 160 printf("\n"); 161 } 162 return 0; 163}Copy the code