思路:

二分+最短路。最短路也可以用来计算从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. 深度学习笔记之基于R-CNN的物体检测

    不多说,直接上干货! 基于R-CNN的物体检测 原文地址:http://blog.csdn.net/hjimce/article/details/50187029 作者:hjimce 一.相关理论 本 ...

  2. PHP出现Warning: A non-numeric value encountered问题的原因及解决方法

    本文介绍php出现Warning: A non-numeric value encountered问题,用实例分析出现这种错误的原因,并提供避免及解决问题的方法. <?php error_rep ...

  3. 内部消息 微软中国云计算 内測Azure免费账号 赶紧申请 错过不再有

    内部消息 微软中国云计算 顶级内測Azure免费账号 火热申请 过期不再有! 微软MSDN俱乐部  29754721, [一大波Azure免费账号来袭]Windows Azure再次开启示放免费试用账 ...

  4. ABAP WEBRFC

    通过WEBRFC实现在网页下载SMW0上传的文件 FUNCTION zhr_download_test. *"---------------------------------------- ...

  5. (转)Java中JSON字符串与java对象的互换实例详解

    在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级的数据格式比xml效率要高,XML需要很多的标签,这无疑占据了网络流量,JSON在这方面则做的很好, ...

  6. 替换Android自带apk【转】

    本文转载自:http://www.voidcn.com/article/p-gonowdjh-vz.html 安卓自带的app放在/system/app/下,当我们想要替换这些应用时可以参考如下步骤: ...

  7. BZOJ_3105_[cqoi2013]新Nim游戏_线性基+博弈论

    BZOJ_3105_[cqoi2013]新Nim游戏_线性基+博弈论 Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作 ...

  8. uCos-III移植到STM32F10x

    最近在百度上看了uCOS-III 的介绍后,诸多功能有很大的提升和改进,感觉有必要升级一下开发环境.百度介绍:http://baike.baidu.com/view/8531313.htm 环境: S ...

  9. Linux CentOS 6.5中安装与配置Tomcat-8方法

    安装环境:CentOS-6.5 安装方式:源码安装 软件:apache-tomcat-8.0.0.RC3.tar.gz 下载地址:http://tomcat.apache.org/download-8 ...

  10. UI:UITextView

    #import "MainViewController.h" @interface MainViewController () <UITextViewDelegate> ...