poj-3662 Telephone Lines 二分答案+最短路
链接:洛谷
题目描述
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.
请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?
输入格式
输入文件的第一行包含三个数字n,p,k;
第二行到第p+1行,每行分别都为三个整数ai,bi,li。
输出格式
一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.
输入输出样例
输入
输出
说明
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即为所求。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <queue>
const int INF=0x3f3f3f3f;
using namespace std;
const int maxlen=;
int D[];//“最短距离”,记录大于等于mid的有多少条边! struct Edge{
int to;
int cost;
};
vector<Edge> G[];//边
// pair<int,int> 用来存 最短路径,顶点编号
int n,p,k; //Dijkstra算法。收进来不属于集合的点的时候,不是更新最短路径了,而是更新到达此点共需要>=mid的边多少条!
bool judge(int mid)
{
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >qe;
fill(D,D+n+,INF);//初始化数组,记得长度为n+2
D[]=;//起点
qe.push(make_pair(,));
while(qe.size())
{
pair<int,int> P=qe.top();
qe.pop();
int u=P.second;
if(D[u]<P.first)
continue;
for(int i=;i<G[u].size();i++)
{
Edge e=G[u][i];
int dd;//记录大于等于mid的。
if(e.cost>=mid)
dd=D[u]+;
else
dd=D[u];
if(D[e.to]>dd)
{
D[e.to]=dd;
qe.push(make_pair(D[e.to],e.to));
}
}
}
return D[n]>=k+;
//k+1这个边界是最后的可行解!
// 因为>=mid有K+1个说明mid右边刚好有K个免费的,此时的mid就是最优解。
//所以满足这个条件的mid即为答案。
} int main()
{
scanf("%d %d %d",&n,&p,&k);
for(int i=;i<=p;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
G[a].push_back(Edge{b,c});
G[b].push_back(Edge{a,c});
}
int l=,r=maxlen+;//注意r=maxlen+2,否则不会输出-1
while(r-l>)
{
int mid=(r+l)/;
if(judge(mid))
l=mid;
else
r=mid;
}
if(l>maxlen)
printf("-1\n");
else
printf("%d\n",l);
return ;
}
还有一种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个点的图,只需要松弛的时候分类就行了,具体看代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#include<set>
using namespace std; const int maxn = , maxm = ;
int n,p,k;
int h[maxn],nxt[maxm],to[maxm],len[maxm],tot;
int dis[maxn][maxn];
bool vis[maxn][maxn];
struct Node{
int u,k;//存当前点和用过的免费线
Node(int x,int y){
u = x; k = y;
}
void operator=(const Node b){
u = b.u; k = b.k;
}
void is(int x,int y){
u = x; k = y;
}
}; inline void spfa(){
memset(dis,0x2f,sizeof(dis));
queue<Node> q;
Node a(,);//边界条件,f[1][0] = 0
q.push(a); vis[][] = ; dis[][] = ;
while(!q.empty()){
a = q.front(); q.pop();
int u =a.u, uk = a.k; //取出当前点和当前用了的免费线数量
vis[u][uk] = ;
for(int i=h[u],v;i;i=nxt[i]){
v = to[i];
//不用免费线
if(max(dis[u][uk],len[i]) < dis[v][uk]){
dis[v][uk] = max(dis[u][uk],len[i]);
if(!vis[v][uk]){
a.is(v,uk);
q.push(a); vis[v][uk] = ;
}
}
//用免费线
if(uk < k && dis[u][uk] < dis[v][uk+]){
dis[v][uk+] = dis[u][uk];
if(!vis[v][uk+]){
a.is(v,uk+);
q.push(a); vis[v][uk+] = ;
}
}
}
}
} int main(){
cin>>n>>p>>k;
for(int i=;i<=p;i++){
int u,v,w; cin>>u>>v>>w;
to[++tot] = v; nxt[tot] = h[u]; len[tot] = w; h[u] = tot;
to[++tot] = u; nxt[tot] = h[v]; len[tot] = w; h[v] = tot;
}
spfa();
//判断无解 791621423是开始赋的INF
if(dis[n][k] == ) cout<<-<<endl;
else cout<<dis[n][k]<<endl;
return ;
}
poj-3662 Telephone Lines 二分答案+最短路的更多相关文章
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- POJ 3662 Telephone Lines(二分+最短路)
查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...
- POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)
题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...
- POJ 3662 Telephone Lines (二分+dijkstra)
题意: 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人. 该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话 ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】
<题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...
- POJ 3662 Telephone Lines(二分答案+SPFA)
[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...
- poj 3662 Telephone Lines(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
- POJ - 3662 Telephone Lines (Dijkstra+二分)
题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...
随机推荐
- mybatis的一对多和多对一的连接查询
实体类: package com.entity; import java.util.List; public class Dept { private Integer deptId; private ...
- POJ - 3660 Cow Contest(flod)
题意:有N头牛,M个关系,每个关系A B表示编号为A的牛比编号为B的牛强,问若想将N头牛按能力排名,有多少头牛的名次是确定的. 分析: 1.a[u][v]=1表示牛u比牛v强,flod扫一遍,可以将所 ...
- java 简单的冒泡
import java.util.Arrays; public class mao { public static void main(String[] args) { int [] array={1 ...
- PL/SQL 连接oracle步骤
下面就将PL/SQL的配置说明一下. 一.安装Oracle客户端,让后配置 安装目录下面的C:\ORACLE\instantclient_11_2\NETWORK\ADMIN 的 tnsname ...
- 翻译——3_Gaussian Process Regression
使用不同的机器学习方法进行预测 续上篇2_Linear Regression and Support Vector Regression 高斯过程回归 %matplotlib inline impor ...
- IDEA常用技巧以及快捷键总结
一.常用快捷键 快捷键 描述 Ctrl+o 复写父类方法 Alt+7 查看类所有方法实现 Ctrl+Alt+H 方法调用链
- 运行roscore出现unable to contact my own server无法启动小海龟的部分故障问题解决
运行roscore后,出现下图这种情况(unable to contact my own server) 原因是找不到http://后面那些,ping不到域名或IP. 参考http://www.ros ...
- Django框架(八):视图(一) URLconf、视图
1. 视图 视图的功能就是接收请求,进行处理,与M和T进行交互,返回应答. 返回html内容HttpResponse,也可能重定向redirect,还可以返回json数据. 1.1 URLconf 1 ...
- share团队冲刺5
团队冲刺第五天 昨天:按钮操作,自定义按钮及各种原件样式 今天:设置按钮按下效果,界面布局 问题:无
- dp--背包--开心的金明
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今天 ...