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 ...
随机推荐
- 解决centos yum安装"No package nginx available."问题
问题原因: nginx位于第三方的yum源里面,而不在centos官方yum源里面 解决方法: 安装epel(Extra Packages for Enterprise Linux) a.去epel网 ...
- udhcp源码详解(四) 之租赁IP的管理
Server端对于租赁出去的IP的管理是基于结构体dhcpOfferedAddr的,该结构体的定义是在leases.c文件里:(结构体的成员介绍说明见详解之数据结构) 1: struct dhcpOf ...
- Android 封装实现各种样式对话框
先上图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/disso ...
- Hibernate 之 How
分享自: http://blog.csdn.net/jnqqls/article/details/8242520 在上一篇文章Hibernate 之 Why? 中对Hibernate有了一个初步的了解 ...
- haproxy tcp 反向代理
配置如下: global log 127.0.0.1 local3 warning nbproc 1 maxconn 65535 daemon defaults log global option d ...
- JFreeChart教程(二)(转)
JFreeChart教程(二) 分类: java Component2007-05-31 17:01 8408人阅读 评论(11) 收藏 举报 jfreechartstringplotclassdat ...
- codeforces 686D D. Kay and Snowflake(dfs)
题目链接: D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- 【Bitset】 BZOJ4810
难得学习一下C++的库..[至今连map,vector都没用过的我.. 首先#include<bitset>或<bits/stdc++.h> 定义函数: { bitset & ...
- UI:归档、反归档、数据持久化
支持的文件读写类型:字符串.数组.字典.NSdata (可变的.不可变的.共有8个类) 对于数组.字典在写入文件时,其中的元素也必须是以上四种类型之一. 支持的数据类型有限.且简单 写入文件: 字符 ...
- 切换或者用户登录时 出现 显示 -bash-4.2$ 问题 的解决
集群的普通用户在登录用户或者切换用户时,出现 -bash-4.2$ 的状态: [goldwind@TR-OS-DB 9.4]#su postgres bash-4.2$ 原因:在linux下通过use ...