Telephone Lines POJ - 3662 (二分+spfa)
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 题意:n个基站,p条双向边,免费k条路,剩余的路的花费为剩余最大的那条路的权值,求最小花费, 即是求从1到n的路径,使得k+1大的边权尽量小的路径
思路:二分枚举第k+1条路的权值,跑最短路,把所有权值大于该值的边权当作1,其余当作0,如果求出最短路值不超过k,那么继续二分,找到临界值。 另外还有dp做法(留坑
#include<deque>
#include<cstdio>
#include<cstring>
#include<iostream> using namespace std; int n,p,k;
const int maxn = ;
int head[maxn];
int cnt; struct Node
{
int x,y,val;
int next;
Node(int x=,int y=,int val=,int next = ):x(x),y(y),val(val),next(next){}
}node[maxn<<]; void add(int x,int y,int val)
{
node[++cnt].x = x;
node[cnt].y = y;
node[cnt].val = val;
node[cnt].next = head[x];
head[x] = cnt;
} bool vis[maxn];
int dist[maxn];
bool bfs(int mid)
{
deque<int>que;
while(!que.empty())que.pop_back();
que.push_back();
memset(vis,,sizeof(vis));
memset(dist,0x3f,sizeof(dist));
dist[] = ;
vis[] = ;
while(!que.empty())
{
int s = que.front();
que.pop_front();
if(s == n)return dist[n] <= k;
for(int i=head[s];i;i=node[i].next)
{
int to = node[i].y;
int val = node[i].val;
if(val <= mid)
{
dist[to] = min(dist[to],dist[s]);
if(!vis[to])que.push_front(to);
}
else
{
dist[to] = min(dist[to],dist[s]+);
if(!vis[to])que.push_back(to);
}
vis[to] = ;
} }
return ;
}
int cal(int r)
{
int l = ;
int ans = -;
while(l <= r)
{
int mid = (l+r)>>;
if(bfs(mid))r = mid - ,ans = mid;
else l = mid + ;
}
return ans;
} int main()
{
scanf("%d%d%d",&n,&p,&k);
int maxx = cnt = ;
for(int i=;i<=p;i++)
{
int u,v,val;
scanf("%d%d%d",&u,&v,&val);
add(u,v,val);
add(v,u,val);
maxx = max(maxx,val);
}
int ans = cal(maxx);
printf("%d\n",ans);
}
Telephone Lines POJ - 3662 (二分+spfa)的更多相关文章
- Divide and conquer:Telephone Lines(POJ 3662)
电话线 题目大意:一堆电话线要你接,现在有N个接口,总线已经在1端,要你想办法接到N端去,电话公司发好心免费送你几段不用拉网线,剩下的费用等于剩余最长电话线的长度,要你求出最小的费用. 这一看又是一个 ...
- 【bzoj1614】[Usaco2007 Jan]Telephone Lines架设电话线 二分+SPFA
题目描述 Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1 ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...
- POJ - 3662 Telephone Lines (dijstra+二分)
题意:有N个独立点,其中有P对可用电缆相连的点,要使点1与点N连通,在K条电缆免费的情况下,问剩下的电缆中,长度最大的电缆可能的最小值为多少. 分析: 1.二分临界线(符合的情况的点在右边),找可能的 ...
- BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线:spfa + 二分【路径中最大边长最小】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1614 题意: 给你一个无向图,n个点,m条边. 你需要找出一条从1到n的路径,使得这条路径 ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- [Usaco2007 Jan]Telephone Lines架设电话线[二分答案+最短路思想]
Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...
- bzoj 1614 Telephone Lines架设电话线 - 二分答案 - 最短路
Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...
随机推荐
- LoadRunner【第三篇】录制脚本实践:订票网站
启动服务 安装好loadrunner,我们就可以实践了. loadrunner自带订票网站,可以方便我们练习, 先把下面两个发送到桌面快捷方式 首先,启动服务,点击下面图标(如果服务无法启动,检查端口 ...
- 【mysql】mysql基准测试
基准测试定义 基准测试其实是一种测量和评估软件性能指标的方法,用于建立某个时间点的性能基准,以便当系统的软硬件发生变化的时候重新进行基准测试以评估变化对性能的影响.所以对系统性能的测量,才能知道我们的 ...
- 基于Matlab实现多次最佳一致的函数逼近(类似求渐进函数)
%%%做系统识别很重要,方法上完全符合系统识别最基础的理论 function [sun]=main(n) fplot(,],'r'); x=ones(n+,); :n+ x(j+)=cos(pi*(n ...
- LOJ#2723 多边形
解:首先,n<=20的直接暴力建图然后状压哈密顿回路,相信大家都会.固定1为起点,fi,s表示结尾为i点,状态为s.每次遍历i的出边转移,最后遍历1的出边统计答案.n22n. 然后就是正经题解了 ...
- Node.js模块化教程
Node.js模块化教程 下载安装node.js 创建项目结构 |-modules |-module1.js |-module2.js |-module3.js|-app.js|-package.js ...
- js重点--闭包
闭包: 1.获取到局部变量,相当于是函数局部与外部的桥梁 2.使局部变量保存在内存中,不被回收 <script> function outerFn() { var outerVar = 0 ...
- Shiro 系列 - 基本知识
和 Spring Security 项目一样, Apache Shiro 也是一个被广泛使用安全框架, 它们都能完成认证.授权.会话管理等. 简单对比一下 Apache Shiro 和 Spring ...
- Java CAS 比较并且更换值
原文:Java中CAS详解 作者:jayxu无捷之径 在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会 ...
- websocket 与Socket.IO介绍
一 websocket WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例如 Chrome,Safrie,Firefox,Opera,IE等等,对该协议支持最早的 ...
- django drf 基础学习4
0 简介:介绍ModelViewSet基本使用规则1 views引用以及初始化 from rest_framework.viewsets import ModelViewSet clas ...