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 ...
随机推荐
- sql server 笔记1--case、WAITFOR、TRY CATCH
一.case 转自:http://blog.csdn.net/add8849/article/details/576424 深入使用:http://blog.csdn.net/akuoma/artic ...
- poj 2585 Window Pains 暴力枚举排列
题意: 在4*4的格子中有9个窗体,窗体会覆盖它之下的窗体,问是否存在一个窗体放置的顺序使得最后的结果与输入同样. 分析: 在数据规模较小且不须要剪枝的情况下能够暴力(思路清晰代码简单),暴力一般分为 ...
- hdu 4925 Apple Tree--2014 Multi-University Training Contest 6
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others ...
- How to use filters in a GridPanel
You can just link statically required files in your index.html <link rel="stylesheet" t ...
- 5.2【Linux 内核网络协议栈源码剖析】socket 函数剖析 ☆☆☆
深度剖析网络协议栈中的 socket 函数,可以说是把前面介绍的串联起来,将网络协议栈各层关联起来. 应用层 FTP SMTP HTTP ... 传输层 TCP UDP 网络层 IP ICMP ARP ...
- centOS安装mysql---glibc方式
写在前面: 首先,centos是自己集成mysql的.但是我要用的服务器人家没给装. 其次,centos是可以yum安装mysql的,我很高兴而且轻松的用yum把mysql安装上了.但是,运行的时候很 ...
- Android开发之自定义对话框
由于系统自带的对话框不好看,于是本人就自定义了一个对话框,以后有类似的就可以直接使用啦.先上效果图: 1,布局文件dialog_clear_normal.xml <?xml version=&q ...
- HttpWebRequest以及HttpWebResponse
上次介绍了用WebClient的方式提交POST请求,这次,我继续来介绍用其它一种方式 HttpWebRequest以及HttpWebResponse 自认为与上次介绍的WebClient最大的不同之 ...
- I.MX6 简单电路模拟USB设备的插入
/**************************************************************************** * I.MX6 简单电路模拟USB设备的插入 ...
- I.MX6 DNS 查看、修改方法
/************************************************************************** * I.MX6 DNS 查看.修改方法 * 说明 ...