1. 程式人生 > >poj3662 Telephone Lines【最短路】【二分】

poj3662 Telephone Lines【最短路】【二分】

http://poj.org/problem?id=3662

Telephone Lines
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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 
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: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

USACO 2008 January Silver   題意: 【感覺題意描述很不清啊,一會長度一會段的】 有$n$個點,$p$條邊,每條邊有一個權值(花費)。將第$1$個點和第$n$個點連通,並且可以有$k$條邊是免費的。剩下的不免費的邊的最大值作為最終的花費。求最終花費的最小值。 思路: 剛開始題意理解錯了。以為是有$k$長度的是免費的,邊的那個權值是長度。想了半天搞不懂。 後來發現其實就是求一個使路徑上第$k+1$大的邊權儘量小的路徑。【雖然還是不會】 因為當我們支付的錢變多時,合法的路徑一定包含了花費更少的路徑。答案具有單調性。 所以我們可以二分答案。 【注意想題目答案的單調性嘗試二分】 這時候問題就變成了,把價格超過$mid$的邊花費看成是$1$,不超過的邊花費看成是$0$,然後求$1~N$的最短路是否不超過$k$就可以了。 要注意考慮$1$和$N$不連通的情況,輸出是$-1$   虐狗寶典上還有一個思路是用dp,但是我不是很會寫。 用$D[x,p]$表示從$1$號節點到基站$x$,途中已經指定了$p$條電纜免費時,經過的路徑上最貴的電纜的花費最小是多少(選擇一條$1$到$x$的路徑,使路徑上第$p+1$大的邊權儘量小)。若有一條從$x$到$y$長度是$z$的無向邊,用$max(D[x,p],z)$更新$D[y,p]$的最小值,用$D[x,p]$更新$D[y, p+1]$的最小值。前者表示不在電纜$(x,y,z)$上使用免費升級服務,後者表示使用。用迭代的思想,藉助SPFA進行動態規劃,直至所有狀態收斂。 還可以把圖中節點拓展到二維,用二元組$(x,p)$表示一個節點,從$(x,p)$到$(y,p)$有長度為$z$的邊,從$(x,p)$到$(y,p+1)$有長度為0的邊。問題就變成了$N*K$個點,$P*K$條邊的廣義單源最短路問題。
  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\n", dijkstra(0));
 81         if(dijkstra(0) == 1061109567)printf("-1\n");
 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\n", ans);
 95         }
 96 
 97 
 98     }
 99     return 0;
100 }