POJ_3662_Telephone_Lines_(二分+最短路)
描述
http://poj.org/problem?id=3662
给一张图,要将1与n连起来.可以有k条边免费,其他边自费,付费的值为所有自费边中最大的值.求最小付费.
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5932 | Accepted: 2152 |
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
Source
分析
可以假定一个付费值m,所有<=m的都自己付费,>m的免费,然后使>m的边数a尽可能小,看是否可以使得a<=k.这里用二分即可.
统计a的最小值时,我们考虑:边分为免费边和自费边,要让免费边数量尽可能小,就把免费边赋值为1,自费边赋值为0,跑最短路,最后d[n]就是最少经过的免费边数量a.
另外,二分的标准如果是0~maxl的话就太大了,我们可以把每一条边的值存在f数组中,然后二分f[0]~f[p],这样会小很多.
注意:
1.如果自费最大费用(不用免费的了),还是不能成功,就输出-1.
Dijkstra:
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=,maxp=,INF=<<;
struct node
{
int to,w;
node(){}
node(int a,int b) : to(a),w(b){}
bool operator < (const node &a) const { return a.w>w; }
};
vector <node> g[maxn];
int n,p,k;
int d[maxn],f[maxp]; bool Dijkstra(int m)
{
for(int i=;i<=n;i++) d[i]=INF;
d[]=;
priority_queue <node> q;
q.push(node(,));
while(!q.empty())
{
int x=q.top().to;q.pop();
for(int i=;i<g[x].size();i++)
{
int y=g[x][i].to,dxy=g[x][i].w;
dxy=dxy>m ? : ;
if(d[y]>d[x]+dxy)
{
d[y]=d[x]+dxy;
q.push(node(y,d[y]));
}
}
}
return (d[n]<=k);
} void solve()
{
if(!Dijkstra(f[p])) { printf("-1\n"); return; }
int l=,r=p;
while(l<r)
{
int m=l+(r-l)/;
if(Dijkstra(f[m])) r=m;
else l=m+;
}
printf("%d\n",f[l]);
} void init()
{
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(node(b,c));
g[b].push_back(node(a,c));
f[i]=c;
}
sort(f+,f++p);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("phone.in","r",stdin);
freopen("phone.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
Spfa:
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=,maxp=,INF=<<;
struct node
{
int to,w;
node(){}
node(int a,int b) : to(a),w(b){}
};
vector <node> g[maxn];
int n,p,k;
int d[maxn],f[maxp];
bool vis[maxn]; bool Spfa(int m)
{
for(int i=;i<=n;i++) { d[i]=INF; vis[i]=false; }
d[]=; vis[]=true;
queue <int> q;
q.push();
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=false;
for(int i=;i<g[x].size();i++)
{
int y=g[x][i].to,dxy=g[x][i].w;
dxy=dxy>m ? : ;
if(d[y]>d[x]+dxy)
{
d[y]=d[x]+dxy;
if(!vis[y])
{
vis[y]=true;
q.push(y);
}
}
}
}
return (d[n]<=k);
} void solve()
{
if(!Spfa(f[p])) { printf("-1\n"); return; }
int l=,r=p;
while(l<r)
{
int m=l+(r-l)/;
if(Spfa(f[m])) r=m;
else l=m+;
}
printf("%d\n",f[l]);
} void init()
{
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(node(b,c));
g[b].push_back(node(a,c));
f[i]=c;
}
sort(f+,f++p);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("phone.in","r",stdin);
freopen("phone.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return ;
}
POJ_3662_Telephone_Lines_(二分+最短路)的更多相关文章
- BZOJ_1614_ [Usaco2007_Jan]_Telephone_Lines_架设电话线_(二分+最短路_Dijkstra/Spfa)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1614 分析 类似POJ_3662_Telephone_Lines_(二分+最短路) Dijks ...
- 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
// 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...
- P1462 通往奥格瑞玛的道路 (二分+最短路)
题目 P1462 通往奥格瑞玛的道路 给定\(n\)个点\(m\)条边,每个点上都有点权\(f[i]\),每条边上有边权,找一条道路,使边权和小于给定的数\(b\),并使最大点权最小. 解析 二分一下 ...
- 二分+最短路 UVALive - 4223
题目链接:https://vjudge.net/contest/244167#problem/E 这题做了好久都还是超时,看了博客才发现可以用二分+最短路(dijkstra和spfa都可以),也可以用 ...
- 2018.07.20 bzoj1614: Telephone Lines架设电话线(二分+最短路)
传送门 这题直接做显然gg" role="presentation" style="position: relative;">gggg,看这数据 ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举
2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举 ...
- Luogu P1951 收费站_NOI导刊2009提高(2) 二分 最短路
思路:二分+最短路 提交:1次 题解: 二分最后的答案. $ck()$: 对于每次的答案$md$跑$s,t$的最短路,但是不让$c[u]>md$的点去松弛别的边,即保证最短路不经过这个点.最后$ ...
- BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)
题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...
- 二分+最短路判定 BZOJ 2709: [Violet 1]迷宫花园
BZOJ 2709: [Violet 1]迷宫花园 Sample Input 5 ######### # # # # # # # #S# # ##### # # ## # # # ### ### ## ...
随机推荐
- powerbulider9.0在数据窗口中实现滚动到新添加行
powerbuilder9.0对数据窗口进行增加行操作,然后实现滚动到指定行时,应先滚动到指定行dw_1.scrolltorow( row),然后设置新添加的行为当前行dw_1.setrow( row ...
- 代码方式删除SVN
public static void delect(File s) { File b[] = null; if (s.exists()) {// 判读是否存在 if (s.isDirectory()) ...
- yaffs2文件系统
1 .yaffs2源码目录文件复制到需要移植的linux内核目录fs/下 同时替换掉源码文件中的Makefile文件跟Kconfig文件. 2.在内核中添加对yaffs2的支持. 3.在make me ...
- yum --rpm包安装
rpm -ivh package -i 表示安装install -v表示显示详细信息, -vv更详细些 -h表示显示安装进度 --force:表示强制安装 --nodeps:忽略依赖关系安装 --r ...
- centos 软件安装 删除
centos的软件安装大致可以分为两种类型: [centos]rpm文件安装,使用rpm指令 类似[ubuntu]deb文件安装,使用dpkg指令 [centos]yum安装 类似[ubuntu ...
- js中使用prototype扩展对象方法
//---------------------对象 //1. var HomeContrl = function(){ this.foo = 'bar'; //对象方法 this.intro = fu ...
- YII2数据库操作出现类似Database Exception – yii\db\Exception SQLSTATE
yii2安装后,连接数据库,必须要安装pdo_mysql扩展
- CentOS系统安全配置
http://down.51cto.com/data/318797 http://www.centos.bz/2011/07/centos-system-security-configure/ htt ...
- 由css属性:vertial-align想到的。。
我的笔记本:型号 acer4750G-2412g50mnkk 分辨率:1333*768,点距:0.25933mm; 12px下的font-size: 默认line-height减去font-size: ...
- Entity Framework Code First 映射继承关系
转载 http://www.th7.cn/Program/net/201301/122153.shtml Code First如何处理类之间的继承关系.Entity Framework Code Fi ...