poj3662 Telephone Lines最短路二分
Posted wyboooo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3662 Telephone Lines最短路二分相关的知识,希望对你有一定的参考价值。
http://poj.org/problem?id=3662
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:9310 | Accepted: 3374 |
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<bits/stdc++.h> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstdlib> 6 #include<cstring> 7 #include<algorithm> 8 #include<queue> 9 #include<vector> 10 #include<set> 11 #include<climits> 12 using namespace std; 13 typedef long long LL; 14 #define N 100010 15 #define pi 3.1415926535 16 17 const int maxn = 1005; 18 const int maxp = 10005; 19 20 int n, p, k; 21 struct node{ 22 int v, w, nxt; 23 }e[maxp * 2]; 24 int tot = 0, head[maxn]; 25 LL dis[maxn]; 26 bool vis[maxn]; 27 28 void addedge(int u, int v, int w) 29 { 30 e[tot].v = v; 31 e[tot].w = w; 32 e[tot].nxt = head[u]; 33 head[u] = tot++; 34 e[tot].v = u; 35 e[tot].w = w; 36 e[tot].nxt = head[v]; 37 head[v] = tot++; 38 } 39 40 int dijkstra(int mid) 41 { 42 memset(dis, 0x3f, sizeof(dis)); 43 memset(vis, 0, sizeof(vis)); 44 dis[1] = 0; 45 priority_queue<pair<LL, int> >que; 46 que.push(make_pair(0, 1)); 47 while(que.size()){ 48 int x = que.top().second;que.pop(); 49 if(vis[x])continue; 50 vis[x] = true; 51 for(int i = head[x]; i != -1; i = e[i].nxt){ 52 int y = e[i].v, z = e[i].w; 53 if(z > mid)z = 1; 54 else z = 0; 55 if(dis[y] > dis[x] + z){ 56 dis[y] = dis[x] + z; 57 que.push(make_pair(-dis[y], y)); 58 } 59 } 60 } 61 return dis[n]; 62 } 63 64 int main() 65 { 66 while(scanf("%d%d%d", &n, &p, &k) != EOF){ 67 for(int i = 0; i < n; i++){ 68 head[i] = -1; 69 } 70 tot = 0; 71 72 int ed = -1; 73 for(int i = 0; i < p; i++){ 74 int u, v, w; 75 scanf("%d%d%d", &u, &v, &w); 76 ed = max(ed, w); 77 addedge(u, v, w); 78 } 79 80 //printf("%d ", dijkstra(0)); 81 if(dijkstra(0) == 1061109567)printf("-1 "); 82 else{ 83 int st = 0, ans; 84 while(st <= ed){ 85 int mid = (st + ed) / 2; 86 if(dijkstra(mid) <= k){ 87 ans = mid; 88 ed = mid - 1; 89 } 90 else{ 91 st = mid + 1; 92 } 93 } 94 printf("%d ", ans); 95 } 96 97 98 } 99 return 0; 100 }
以上是关于poj3662 Telephone Lines最短路二分的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3662 Telephone Lines (分层图)
POJ 3662 Telephone Lines (分层图做法)
poj 3662 Telephone Lines(最短路+二分)