poj3662 Telephone Lines 二分答案+最短路

链接:洛谷

           POJ

题目描述

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.

多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。

第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。

电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.

请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?

扫描二维码关注公众号,回复: 6892218 查看本文章

输入格式

输入文件的第一行包含三个数字n,p,k;

第二行到第p+1行,每行分别都为三个整数ai,bi,li。

输出格式

一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.

输入输出样例

输入

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

输出

 4

说明

There are 5 poles. Pole 1 cannot be connected directly to poles 4 or 5. Pole 5 cannot be connected directly to poles 1 or 3. All other pairs can be connected. The phone company will provide one free cable.
If pole 1 is connected to pole 3, pole 3 to pole 2, and pole 2 to pole 5 then Farmer John requires cables of length 4, 3, and 9. The phone company will provide the cable of length 9, so the longest cable needed has length 4.

题意:

给定一个无向有权图,权值代表修建电线需要的花费,公司给报销K条线,这个人需要付款剩下的线路中花费最大的那个线路。

在加权无向图上求出一条从1号结点到N号结点的路径,使路径上第K+1大的边权尽量小

思考:

为什么可以这样概括呢?因为题意中的答案要最小,我们贪心肯定要使k次免费的资格用完,那么最划算的方案肯定是拿最长的k条路使之免费,然后付第k+1长路的长度的钱。。。这样的贪心思路显然是正确的。

题解:(先粘一个比较好的题解链接https://www.luogu.org/problemnew/solution/P1948

最短路问题,只不过这时候的最短路不是让求最短的距离了,而是给定一个花费X,是否有一条通路能够满足,恰好有K+1条路满足>=X,这个X就是可行解。二分去求最小的可行解就可以了!

所以这个时候d[]数组放最短距离已经没有任何意义了,而是放到达终点>=X的边数最少有多少条。

具体点如下:

我们可以二分最大的花费mid,mid属于[l,r],(l为最小花费,r为最大花费), 然后将大于等于mid的边看做权值为1的边,将小于mid的边看做权值为零的边,然后找到从点1到点n的最短路,若最短路的长度大于k,则要连接的对数大于k对,在[mid,r]中继续二份查找;若最短路的长度小于k,则要连接的对数比k小,在[l,mid]中继续二份查找,最终,l即为所求。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 const int INF=0x3f3f3f3f;
 9 using namespace std;
10 const int maxlen=1000000;
11 int D[1005];//“最短距离”,记录大于等于mid的有多少条边!
12 
13 struct Edge{
14     int to;
15     int cost;
16 };
17 vector<Edge> G[1005];//18 // pair<int,int> 用来存 最短路径,顶点编号
19 int n,p,k;
20 
21 //Dijkstra算法。收进来不属于集合的点的时候,不是更新最短路径了,而是更新到达此点共需要>=mid的边多少条!
22 bool judge(int mid)
23 {
24     priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >qe;
25     fill(D,D+n+2,INF);//初始化数组,记得长度为n+2 
26     D[1]=0;//起点 
27     qe.push(make_pair(0,1));
28     while(qe.size())
29     {
30         pair<int,int> P=qe.top();
31         qe.pop();
32         int u=P.second;
33         if(D[u]<P.first)
34             continue;
35         for(int i=0;i<G[u].size();i++)
36         {
37             Edge e=G[u][i];
38             int dd;//记录大于等于mid的。
39             if(e.cost>=mid)
40                 dd=D[u]+1;
41             else
42                 dd=D[u];
43             if(D[e.to]>dd)
44             {
45                 D[e.to]=dd;
46                 qe.push(make_pair(D[e.to],e.to));
47             }
48         }
49     }
50     return D[n]>=k+1;
51     //k+1这个边界是最后的可行解!
52     // 因为>=mid有K+1个说明mid右边刚好有K个免费的,此时的mid就是最优解。
53     //所以满足这个条件的mid即为答案。
54 }
55 
56 int main()
57 {
58     scanf("%d %d %d",&n,&p,&k);
59     for(int i=1;i<=p;i++)
60     {
61         int a,b,c;
62         scanf("%d %d %d",&a,&b,&c);
63         G[a].push_back(Edge{b,c});
64         G[b].push_back(Edge{a,c});
65     }
66     int l=0,r=maxlen+2;//注意r=maxlen+2,否则不会输出-1 
67     while(r-l>1)
68     {
69         int mid=(r+l)/2;
70         if(judge(mid))
71             l=mid;
72         else
73             r=mid;
74     }
75     if(l>maxlen)
76         printf("-1\n");
77     else
78         printf("%d\n",l);
79     return 0;
80 }

还有一种dp方法(摘自上方题解链接)

考虑用f[i][j]f[i][j]表示从1到点i,用了j条免费线的最小花费。

这样的状态空间是1000*1000的,可以存下来。

对于一条边e,u->v,我们有如下转移

​ 不用免费线:f[v][k] = min(f[v][k],max(f[u][k],len[e]))

​ 用免费线:f[v][k+1]=min(f[v][k+1],f[u][k])

考虑给的不是一个DAG图,没有一个确定的顺序来dp,所以我们用spfa来转移这些状态。把每一个状态想成一个点,按上面的转移来连边,然后跑最短路就求出了每个状态的最小值。当然我们实际在做的时候不用真的建出这样1e6个点的图,只需要松弛的时候分类就行了,具体看代码。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<vector>
 7 #include<cstring>
 8 #include<cmath>
 9 #include<set>
10 using namespace std;
11 
12 const int maxn = 1005, maxm = 20005;
13 int n,p,k;
14 int h[maxn],nxt[maxm],to[maxm],len[maxm],tot;
15 int dis[maxn][maxn];
16 bool vis[maxn][maxn];
17 struct Node{
18     int u,k;//存当前点和用过的免费线
19     Node(int x,int y){
20         u = x; k = y;
21     }
22     void operator=(const Node b){
23         u = b.u; k = b.k;
24     }
25     void is(int x,int y){
26         u = x; k = y;
27     }
28 };
29 
30 inline void spfa(){
31     memset(dis,0x2f,sizeof(dis));
32     queue<Node> q;
33     Node a(1,0);//边界条件,f[1][0] = 0
34     q.push(a); vis[1][0] = 1; dis[1][0] = 0;
35     while(!q.empty()){
36         a = q.front(); q.pop();
37         int u =a.u, uk = a.k; //取出当前点和当前用了的免费线数量
38         vis[u][uk] = 0;
39         for(int i=h[u],v;i;i=nxt[i]){
40             v = to[i];
41             //不用免费线
42             if(max(dis[u][uk],len[i]) < dis[v][uk]){
43                 dis[v][uk] = max(dis[u][uk],len[i]);
44                 if(!vis[v][uk]){
45                     a.is(v,uk);
46                     q.push(a); vis[v][uk] = 1;
47                 }
48             }
49             //用免费线
50             if(uk < k && dis[u][uk] < dis[v][uk+1]){
51                 dis[v][uk+1] = dis[u][uk];
52                 if(!vis[v][uk+1]){
53                     a.is(v,uk+1);
54                     q.push(a); vis[v][uk+1] = 1;
55                 }
56             }
57         }
58     }
59 }
60 
61 int main(){
62     cin>>n>>p>>k;
63     for(int i=1;i<=p;i++){
64         int u,v,w; cin>>u>>v>>w;
65         to[++tot] = v; nxt[tot] = h[u]; len[tot] = w; h[u] = tot;
66         to[++tot] = u; nxt[tot] = h[v]; len[tot] = w; h[v] = tot;
67     }
68     spfa();
69     //判断无解 791621423是开始赋的INF
70     if(dis[n][k] == 791621423) cout<<-1<<endl;
71     else cout<<dis[n][k]<<endl;
72     return 0;
73 }

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11261745.html