思路:

二分+最短路。最短路也可以用来计算从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的更多相关文章

  1. POJ3662 Telephone Lines( dijkstral + 二分 )

    POJ3662 Telephone Lines 题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的, 求这m-k条边中花费最大的一条边的最 ...

  2. poj3662 Telephone Lines【最短路】【二分】

    http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  3. POJ3662 Telephone Lines (dijkstra+二分)

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...

  4. poj-3662 Telephone Lines 二分答案+最短路

    链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...

  5. POJ-3662 Telephone Lines 二分+双端队列

    题目传送门 题意:有n个点, p条路,每条道路有个花费Li, 然后现在要建一条1-n的路线,然后可以选k条道路免费, 然后可以在剩下的道路中选择价格最高的边支付费用, 求这个答案最小. 题解: 二分答 ...

  6. BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 892  Solved: ...

  7. BZOJ 1614: [Usaco2007 Jan]Telephone Lines架设电话线

    题目 1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farm ...

  8. poj 3662 Telephone Lines

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7115   Accepted: 2603 D ...

  9. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

随机推荐

  1. 开源 java CMS - FreeCMS2.2 敏感词管理

    项目地址:http://www.freeteam.cn/ 敏感词管理 管理敏感词.系统会自己主动将敏感词替换为指定字符. 系统进行敏感词处理的功能有: 信息:标题.内容,摘要. 栏目:名称,描写叙述. ...

  2. Leetcode Single Number II (面试题推荐)

    还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here  相信大家都知道用异或在O(n)的时间复 ...

  3. 解决javah生成.h头文件找不到找不到android.support.v7.app.AppCompatActivity的问题

    问题描写叙述: 在使用Android Studio进行JNI开发时,须要使用javah生成C或C++的头文件,可是可能会遇到: 错误: 无法訪问android.support.v7.app.AppCo ...

  4. Oracle 表的创建 及相关參数

    1. 创建表完整语法 CREATE TABLE [schema.]table (column datatype [, column datatype] - ) [TABLESPACE tablespa ...

  5. Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support.constraint:constraint-layout:1.1.0. Could not resolve com.android.support.constraint:constraint-l

    File->Settings->Build, Execution, Deployment->Gradle->取消选中 Offline work 按钮

  6. fscanf()函数详解【转】

    本文转载自:http://blog.csdn.net/liangxanhai/article/details/8026496 以前解析有规律的文件的时候要么用正则表达式,要么就是傻傻的自己写程序来解析 ...

  7. 通过Mesos、Docker和Go,使用300行代码创建一个分布式系统

    [摘要]虽然 Docker 和 Mesos 已成为不折不扣的 Buzzwords ,但是对于大部分人来说它们仍然是陌生的,下面我们就一起领略 Mesos .Docker 和 Go 配合带来的强大破坏力 ...

  8. mac多线程下载神器

    本文参考:https://blog.csdn.net/orangleliu/article/details/46834429 神器:axel 安装(已经安装homebrew前提下,没有请参考:http ...

  9. scrapy学习笔记:项目中 使用代理ip

    做为一个爬虫,最头疼的问题就是你的ip被封,想要在Scrapy领域无限制畅游,做好伪装是第一步,于是乎,抓取代理IP成了很多教程的开始部分.这里我说一下代理scrapy中代理ip,仅供大家借鉴! 代理 ...

  10. css3 all属性

    ie不支持,谷歌火狐支持,safari9+支持,移动端高版本支持 all属性实际上是所有CSS属性的缩写,表示,所有的CSS属性都怎样怎样,但是,不包括unicode-bidi和direction这两 ...