POJ 3662: Telephone Lines
POJ 3662: Telephone Lines
Problem Statement
Telephone Lines
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8989 | Accepted: 3263 |
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.
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
* 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
Problem Analysis
Notice it said "K lengths of wire for free." This does not mean the cable company is providing a cable of length K; nor does it mean the cable company is providing cables with a collective length of K. It means that the cable company is providing K cables for free.Which would be easier, directly computing it (through doing a shortest path, weighted by the K+1th edge when sorted by edge length on path) or guess and check (checking also by doing a shortest path, weighted by number of edges above the guess)? I think guess and check would be easier. For the record, I tried directly computing it first, and failed.
Obviously the only feasible way to do guess-and-check is binary search. But what do we binary search? Normally we search for the answer directly, so let's search for the maximum edge length.
We still have to do a shortest path to verify our guess, but this time the shortest path is much easier. We use a priority queue, making the "smallest" pop first. What is defined as the smallest? Let's use "least amount of edges above our guess" as our comparison.
The shortest path algorithm is a normal Dijkstra's except it returns if the "distance" to N is less than or equal to K. The reality is that it's not "distance", it's weight - the "least amount of edges above our guess".
If the result of Dijkstra's is TRUE for a guess, then the answer is less than or equal to the guess; if not, then the answer is greater than the guess.
My Code
#include <iostream> #include <queue> #include <vector> #include <cstring> #define MAXN 1005 using namespace std; struct state { int a, b; state(int a, int b) : a(a), b(b) { } const bool operator<(const state other) const { return b > other.b; } }; priority_queue<state> que; vector<state> elist[MAXN]; int N, P, K; int res[MAXN]; bool dij(int longest) { memset(res, 127, sizeof res); que.push(state(0, 0)); res[0] = 0; while(!que.empty()) { state x = que.top(); que.pop(); if(res[x.b] < x.a) continue; for(int i=0; i<(signed)elist[x.b].size(); i++) { state e = elist[x.b][i]; int upd = res[x.b] + ((e.b > longest) ? 1 : 0); if(res[e.a] > upd) { que.push(state(upd, e.a)); res[e.a] = upd; } } } if(res[N-1] == res[1004]) throw -1; // Dijkstra's never even touched node N-1 return res[N-1] <= K; } int bs(int ul) { int ll = 0; while(ll < ul) { int mid = (ll + ul)/2; try { if(dij(mid)) ul = mid; else ll = mid+1; } catch (const int i) { return i; } } return ul; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> P >> K; int ul = -1; for(int i=0; i<P; i++) { int s, e, l; cin >> s >> e >> l; s--; e--; elist[s].push_back(state(e, l)); elist[e].push_back(state(s, l)); ul = ((ul < l) ? l : ul); } cout << bs(ul) << endl; }
Comments
Post a Comment