首页 > 代码库 > POJ 3662 Telephone Lines (分层图做法)

POJ 3662 Telephone Lines (分层图做法)

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

用分层图很好去处理费用问题。
设d[i][j] 表示到i使用j个优惠的答案。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <vector>
 5 #include <queue>
 6 using namespace std;
 7 int n, p, k, vis[1010][1010], d[1010][1010];
 8 typedef pair<int,int> P;
 9 struct Nod {
10     int v, c, w;
11     bool operator < (const Nod &a) const {
12         return w > a.w;
13     }
14 };
15 vector<P> G[1010];
16 void spfa(int s) {
17     memset(d,-1,sizeof(d));memset(vis,0,sizeof(vis));
18     d[s][0] = 0;
19     priority_queue<Nod> que;
20     que.push((Nod){s,0,0});
21     while(!que.empty()) {
22         int u = que.top().v, c = que.top().c; que.pop();
23         if(vis[u][c])continue;
24         vis[u][c] = 1;
25         for(int i = 0; i < G[u].size(); i ++) {
26             int v = G[u][i].first, w = G[u][i].second;
27             if(!vis[v][c] && (d[v][c] == -1 || d[v][c] > max(d[u][c],w))) {
28                 d[v][c] = max(d[u][c], w);
29                 que.push((Nod){v,c,d[v][c]});
30             }
31             if(c < k) {
32                 if(!vis[v][c+1] && (d[v][c+1] == -1 || d[v][c+1] > d[u][c])) {
33                     d[v][c+1] = d[u][c];
34                     que.push((Nod){v,c+1,d[v][c+1]});
35                 }
36             }
37         }
38     }
39 }
40 int main() {
41     ios::sync_with_stdio(false);
42     cin>>n>>p>>k;
43     int s = 1, t = n;
44     for(int i = 1; i <= p; i ++) {
45         int u, v, w;
46         cin>>u>>v>>w;
47         G[u].push_back(P(v,w));
48         G[v].push_back(P(u,w));
49     }
50     spfa(s);
51     int ans = 10000010;
52     for(int i = 0; i <= k; i ++) ans = min(ans,d[t][i]);
53     printf("%d\n",ans);
54     return 0;
55 }

 

POJ 3662 Telephone Lines (分层图做法)