POJ 3662
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4591 | Accepted: 1693 |
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 <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 7 using namespace std; 8 9 #define maxnode 10005 10 #define edge 100005 11 #define INF (1 << 30) 12 13 14 15 struct node { 16 int d,u; 17 bool operator < (const node& rhs) const { 18 return d > rhs.d; 19 } 20 }; 21 int n,p,k,l,r,_max; 22 int v[edge * 2],w[edge * 2],next[edge * 2],first[maxnode]; 23 int d[maxnode]; 24 bool done[maxnode]; 25 26 int dijkstra(int s,int L) { 27 priority_queue<node> q; 28 for(int i = 1; i <= n; ++i) d[i] = INF; 29 d[s] = 0; 30 31 node t; 32 t.d = 0,t.u = s; 33 q.push(t); 34 memset(done,0,sizeof(done)); 35 36 37 while(!q.empty()) { 38 node x = q.top(); q.pop(); 39 int u = x.u; 40 if(done[u]) continue; 41 done[u] = 1; 42 for(int e = first[u]; e != -1; e = next[e]) { 43 if(d[ v[e] ] > d[u] + (w[e] > L) ) { 44 d[ v[e] ] = d[u] + (w[e] > L); 45 t.d = d[ v[e] ]; 46 t.u = v[e]; 47 q.push(t); 48 } 49 } 50 } 51 52 return d[n] <= k; 53 } 54 55 56 void addedge(int a,int b,int id) { 57 int e = first[a]; 58 next[id] = e; 59 first[a] = id; 60 } 61 62 void solve() { 63 64 65 l = 0; 66 while(l < r) { 67 int mid = (l + r) >> 1; 68 if(dijkstra(1,mid)) r = mid; 69 else l = mid + 1; 70 } 71 72 if(l == _max + 1) printf("-1\n"); 73 else 74 printf("%d\n",r); 75 76 77 78 } 79 80 int main() 81 { 82 //freopen("sw.in","r",stdin); 83 84 scanf("%d%d%d",&n,&p,&k); 85 for(int i = 1; i <= n; ++i) first[i] = -1; 86 l = INF,_max = 0; 87 for(int i = 0; i < 2 * p; i += 2) { 88 int a; 89 scanf("%d%d%d",&a,&v[i],&w[i]); 90 _max = max(_max,w[i]); 91 v[i + 1] = a; 92 w[i + 1] = w[i]; 93 addedge(a,v[i],i); 94 addedge(v[i],a,i + 1); 95 } 96 97 98 r = _max + 1; 99 solve(); 100 101 102 103 104 return 0; 105 }