Telephone Lines POJ - 3662 (二分+spfa)

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


题意:n个基站,p条双向边,免费k条路,剩余的路的花费为剩余最大的那条路的权值,求最小花费, 即是求从1到n的路径,使得k+1大的边权尽量小的路径
思路:二分枚举第k+1条路的权值,跑最短路,把所有权值大于该值的边权当作1,其余当作0,如果求出最短路值不超过k,那么继续二分,找到临界值。

另外还有dp做法(留坑
 1 #include<deque>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 
 6 using namespace std;
 7 
 8 int n,p,k;
 9 const int maxn = 1005;
10 int head[maxn];
11 int cnt;
12 
13 struct Node
14 {
15     int x,y,val;
16     int next;
17     Node(int x=0,int y=0,int val=0,int next = 0):x(x),y(y),val(val),next(next){}
18 }node[maxn<<5];
19 
20 void add(int x,int y,int val)
21 {
22     node[++cnt].x = x;
23     node[cnt].y = y;
24     node[cnt].val = val;
25     node[cnt].next = head[x];
26     head[x] = cnt;
27 }
28 
29 bool vis[maxn];
30 int dist[maxn];
31 bool bfs(int mid)
32 {
33     deque<int>que;
34     while(!que.empty())que.pop_back();
35     que.push_back(1);
36     memset(vis,0,sizeof(vis));
37     memset(dist,0x3f,sizeof(dist));
38     dist[1] = 0;
39     vis[1] = 1;
40     while(!que.empty())
41     {
42         int s = que.front();
43         que.pop_front();
44         if(s == n)return dist[n] <= k;
45         for(int i=head[s];i;i=node[i].next)
46         {
47             int to = node[i].y;
48             int val = node[i].val;
49             if(val <= mid)
50             {
51                 dist[to] = min(dist[to],dist[s]);
52                 if(!vis[to])que.push_front(to);
53             }
54             else
55             {
56                 dist[to] = min(dist[to],dist[s]+1);
57                 if(!vis[to])que.push_back(to);
58             }
59             vis[to] = 1;
60         }
61 
62     }
63     return 0;
64 }
65 int cal(int r)
66 {
67     int l = 0;
68     int ans = -1;
69     while(l <= r)
70     {
71         int mid = (l+r)>>1;
72         if(bfs(mid))r = mid - 1,ans = mid;
73         else l = mid + 1;
74     }
75     return ans;
76 }
77 
78 int main()
79 {
80     scanf("%d%d%d",&n,&p,&k);
81     int maxx = cnt = 0;
82     for(int i=1;i<=p;i++)
83     {
84         int u,v,val;
85         scanf("%d%d%d",&u,&v,&val);
86         add(u,v,val);
87         add(v,u,val);
88         maxx = max(maxx,val);
89     }
90     int ans = cal(maxx);
91     printf("%d\n",ans);
92 }
View Code

 

转载于:https://www.cnblogs.com/iwannabe/p/10676330.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值