poj3662 Telephone Lines
思路:
二分+最短路。最短路也可以用来计算从a到达b所需的边权不超过x的边的数量。
实现:
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
int G[MAXN][MAXN], d[MAXN];
bool in[MAXN];
int n, p, k;
int spfa(int s, int t, int x)
{
memset(in, false, sizeof in);
memset(d, 0x3f, sizeof d);
queue<int> q;
d[s] = ;
q.push(s);
in[s] = true;
while (!q.empty())
{
int tmp = q.front(); q.pop();
in[tmp] = false;
for (int i = ; i <= n; i++)
{
int w = INF;
if (G[tmp][i] != INF) w = G[tmp][i] > x ? : ;
if (d[tmp] + w < d[i])
{
d[i] = d[tmp] + w;
if (!in[i]) { in[i] = true; q.push(i); }
}
}
}
return d[t];
}
bool check(int x)
{
int cnt = spfa(, n, x);
return cnt <= k;
}
int main()
{
scanf("%d %d %d", &n, &p, &k);
int a, b, w, maxw = -INF;
memset(G, 0x3f, sizeof G);
for (int i = ; i < p; i++)
{
scanf("%d %d %d", &a, &b, &w);
G[a][b] = G[b][a] = w;
maxw = max(maxw, w);
}
int l = , r = maxw, ans = INF;
while (l <= r)
{
int m = l + r >> ;
if (check(m)) { ans = m; r = m - ; }
else l = m + ;
}
if (ans == INF) puts("-1");
else printf("%d\n", ans);
return ;
}
poj3662 Telephone Lines的更多相关文章
- POJ3662 Telephone Lines( dijkstral + 二分 )
POJ3662 Telephone Lines 题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的, 求这m-k条边中花费最大的一条边的最 ...
- poj3662 Telephone Lines【最短路】【二分】
http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ3662 Telephone Lines (dijkstra+二分)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...
- poj-3662 Telephone Lines 二分答案+最短路
链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...
- POJ-3662 Telephone Lines 二分+双端队列
题目传送门 题意:有n个点, p条路,每条道路有个花费Li, 然后现在要建一条1-n的路线,然后可以选k条道路免费, 然后可以在剩下的道路中选择价格最高的边支付费用, 求这个答案最小. 题解: 二分答 ...
- BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线
1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 892 Solved: ...
- BZOJ 1614: [Usaco2007 Jan]Telephone Lines架设电话线
题目 1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec Memory Limit: 64 MB Description Farm ...
- 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 ...
随机推荐
- 使用Blender批量导出/转换模型
2.4版本号的Blender API和2.5以上版本号的API有非常大的不同,这里仅仅是提供了思路和2.4版本号的导出方案. 先提供一个脚本,这个是由Blender调用的.用于转换Ogre的Mesh文 ...
- JS基本特效 -- 常见40个常用的js页面效果图(转载)
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 <table border oncontextmenu ...
- 6.游戏特别离不开脚本(3)-JS脚本操作java(2)(直接解析JS公式,并非完整JS文件或者函数)
在游戏中可以考虑数据由javabean保存,逻辑方法由JS提供. public class Bean4JS { private int id; private String name; private ...
- mp3 pcm
mp3 pcm javaMP3转pcm 百度语音识别 - 且学且珍惜 - SegmentFault 思否 https://segmentfault.com/a/1190000013383967
- Hessian Matrix 多元函数的极值 半正定矩阵 正定矩阵
https://baike.baidu.com/item/黑塞矩阵/2248782?fr=aladdin 海塞矩阵 Hasse https://baike.baidu.com/item/半正定矩阵
- JDK各版本内容和新特性
JDK各版本内容和新特性 - yanlzhl - 博客园 https://www.cnblogs.com/yanlzhl/articles/5694470.html 版本JDK1.0:1995年 ...
- 如何快速定位JVM中消耗CPU最多的线程? Java 性能调优
https://mp.weixin.qq.com/s/ZqlhPC06_KW6a9OSgEuIVw 上面的线程栈我们注意到 nid 的值其实就是线程 ID,它是十六进制的,我们将消耗 CPU 最高的线 ...
- 定时任务 bash 对远程数据库 备份 读写
1g表 每行都有可能被更新,故全表备份 检测备份是否在进行 [root@hadoop1 ~]# netstat --numeric-ports | grep 3306tcp 0 ...
- how to create modals with Bootstrap
In this tutorial you will learn how to create modals with Bootstrap. Creating Modals with Bootstrap ...
- 设计模式-(16)模版模式 (swift版)
一,概念: 定义一个算法中的操作框架,而将一些步骤延迟到子类中.使得子类可以不改变算法的结构即可重定义该算法的某些特定步骤.(Define the skeleton of an algorithm i ...