USACO Telephone Lines
洛谷 P1948 [USACO08JAN]电话线Telephone Lines
https://www.luogu.org/problem/P1948
JDOJ 2556: USACO 2008 Jan Silver 3.Telephone Lines
https://neooj.com/oldoj/problem.php?id=2556
题目描述
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.
输入输出样例
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 SPFA+二分答案 因为它给你k次免费的机会,所以根据贪心思想,我们应该尽可能地把路线上前k长的路交给电信来修,也就是说我们要求1-n的路径的第k+1长的边的最小值。
我们二分答案就是第k+1条边的最小值。
边界很好设置。
现在就是判断了。
我们可以考虑这样一个重新建图的思路(说是重新建图,其实就是把边权重新更改了一个定义而已。),因为我们二分的是第k+1的边权最小值,那么针对我们二分出来的每一个mid,我们可以跑所有的边,如果这个边权小于mid,说明它是免费的,如果大于的话,说明需要使用一次免费的资格,所以边权重置成1.
然后我们在新的边权图上跑最短路,求出f[n],这时的f[n]就表示从1到n需要几次免费的资格,如果小于等于k,符合题意,范围左缩,反之亦然。 这就是正解了。 代码:
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int n,p,k,ans=-;
int tot,to[],nxt[],val[],head[],s[];
int f[],v[];
void add(int x,int y,int z)
{
to[++tot]=y;
nxt[tot]=head[x];
val[tot]=z;
head[x]=tot;
}
void spfa()
{
memset(f,0x3f,sizeof(f));
memset(v,,sizeof(v));
queue<int> q;
f[]=;q.push();v[]=;
while(!q.empty())
{
int x=q.front();
q.pop();
v[x]=;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(f[y]>f[x]+s[i])
{
f[y]=f[x]+s[i];
if(v[y]==)
{
q.push(y);
v[y]=;
}
}
}
}
}
bool check(int x)
{
for(int i=;i<=tot;i++)
{
if(val[i]<=x)
s[i]=;
else
s[i]=;
}
spfa();
if(f[n]<=k)
return ;
else
return ;
}
int main()
{
scanf("%d%d%d",&n,&p,&k);
for(int i=;i<=p;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
int l=;
int r=;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid))
ans=mid,r=mid-;
else
l=mid+;
}
printf("%d",ans);
return ;
}
USACO Telephone Lines的更多相关文章
- BZOJ1614:[USACO]Telephone Lines架设电话线(二分,最短路)
Description FarmerJohn打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司 支付一定的费用.FJ的农场周围分布着N(1<=N< ...
- poj 3662 Telephone Lines
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7115 Accepted: 2603 D ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj3662 Telephone Lines【最短路】【二分】
http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- [POJ] 3362 Telephone Lines
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7978 Accepted: 2885 Descr ...
- poj 3662 Telephone Lines(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
- poj 3662 Telephone Lines dijkstra+二分搜索
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accepted: 2071 D ...
- BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线
1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 892 Solved: ...
随机推荐
- ASP.NET开发实战——(十一)ASP.NET MVC 与数据库之EntityFramework配置与连接字符串
前几篇文章中介绍了如何使用Entity Framework来操作数据库,但是对EF的配置.连接字符串的指定仍然存在一些疑问,EF可以通过两种方式来实现配置,分别是代码方式和配置文件. 本章将通过以下几 ...
- 洛谷P3723 [AH2017/HNOI2017]礼物
吴迪说他化学会考上十分钟就想出来了,太神了%%%不过我也十分钟 但是调了一个多小时啊大草 懒得人话翻译了,自己康吧: 我的室友(真的是室友吗?)最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决 ...
- asp.net mvc 系统操作日志设计
第一步.系统登录日志 通过signalr来管理用户的登录情况,并保存用户的登录记录. 第二步 通过mvc过滤器,来横切路由访问记录. 保存方式:通过httpclient异步请求webapi 数据通过m ...
- java.util.concurrent各组件分析 一 sun.misc.Unsafe
java.util.concurrent各组件分析 一 sun.misc.Unsafe 说到concurrent包也叫并发包,该包下主要是线程操作,方便的进行并发编程,提到并发那么锁自然是不可缺少的, ...
- 大话设计模式Python实现-备忘录模式
备忘录模式(Memento Pattern):不破坏封装性的前提下捕获一个对象的内部状态,并在该对象之外保存这个状态,这样已经后就可将该对象恢复到原先保存的状态 下面是一个备忘录模式的demo: #! ...
- 【shell脚本】定时备份数据库===dbbackup.sh
定时备份数据库是很有必要的 一.脚本内容 [root@localhost dbbackup]# cat dbbackup.sh #!/bin/bash #备份数据库 mysqldump -uroot ...
- 超详细Pycharm部署项目视频教程
在实际的工作中,不管你是开发.测试还是运维人员,都应该掌握的一项技能就是部署项目,简单说就是把项目放到服务器中,使其正常运行.今天猪哥就以咱们的微信机器人项目为例子,带大家来部署一下项目.本文将会详细 ...
- Prometheus 监控K8S集群资源监控
Prometheus 监控K8S集群中Pod 目前cAdvisor集成到了kubelet组件内,可以在kubernetes集群中每个启动了kubelet的节点使用cAdvisor提供的metrics接 ...
- .NetCore+WebUploader实现大文件分片上传
项目要求通过网站上传大文件,比如视频文件,通过摸索实现了文件分片来上传,然后后台进行合并. 使用了开源的前台上传插件WebUploader(http://fex.baidu.com/webupload ...
- ASP.NET MVC IOC 之 Autofac 系列开篇
本系列主要讲述Autofac在.NET MVC项目以及webform中的使用. autofac为IOC组件,实现控制反转,主要结合面向接口编程,完成较大程度的解耦工作. 作为初学者,将学习到的每一步, ...