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# # ##### # # ## # # # ### ### ## ...
随机推荐
- Android中使用ViewPager实现广告条
我们在使用电商或视频的手机客户端时,通常会看到广告条的效果.在网上搜索时才知道使用的是ViewPager,于是自己也做了一个Demo. 以下是效果图: 图中包括背景图片.文字描述以及白点. 其中Vie ...
- c语言学习之基础知识点介绍(十一):字符串的介绍、使用
本节主要介绍c语言中的字符串的应用. 一:字符串介绍 因为c语言中没有像Java.C#那样的字符串类型,所以无法直接用字符串.需要借助数组来解决这个问题. /* 定义:把多个字符连在一起就叫字符串.但 ...
- c++ 学习之const专题之const成员函数
一些成员函数改变对象,一些成员函数不改变对象. 例如: int Point::GetY() { return yVal; } 这个函数被调用时,不改变Point对象,而下面的函数改变Point对象: ...
- IOS_OC_本地推送知识总结
知识点介绍 一. 推送通知介绍(了解) 二. 本地推送通知 本地通知的基本使用 本地通知的不常用属性 删除重复的通知 通知的处理1-跳转界面 通知的处理2-程序退出 分类的设置/快捷回复 一. 推送通 ...
- window redis 安装配置
1 下载 https://github.com/MSOpenTech/redis/releases 当前最新版本为 redis-2.8.21 下载的为zip包,下载连接为:https://gith ...
- 桶排序之python实现源码
tmp = [] def bucket_sort(old): for i in range(len(old)): tmp.append([]) for i in old: tmp[int( i * l ...
- ubuntu下编译安装apache
官网http://httpd.apache.org/download.cgi下载apache源码包后 /*解包*/ gzip -d httpd-2_x_NN.tar.gz tar -xf httpd- ...
- u-boot和linux的机器码
先看u-boot的机器码和linux的机器码是在什么地方决定的. 1. u-boot的机器码是在u-boot的board/fs2410/fs2410.c文件里决定的: /* arch numb ...
- [C#]递归遍历文件夹
/// <summary> /// 递归获取文件夹目录下文件 /// </summary> /// <param name="pathName"> ...
- event的属性
t获取鼠标相对于浏览器左上角的坐标 <div id="dv" style=" width:300px; height:200px; background-color ...