(poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8248 | Accepted: 2977 |
Description
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.
There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.
The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.
As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.
Determine the minimum amount that Farmer John must pay.
Input
* Line 1: Three space-separated integers: N, P, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li
Output
* Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.
Sample Input
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
Sample Output
4
Source
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include <cmath>
#include<vector>
using namespace std;
#define LL long long
#define N 1010
#define mod 1000000007
#define INF 0x3f3f3f3f
struct node
{
int u,v,c,next,w; }s[N*];
int head[N],k = ,mi,n;
int dis[N],vis[N];
void add(int u,int v,int c)
{
s[k].u = u;
s[k].v = v;
s[k].c = c;
s[k].next = head[u];
head[u] = k++;
} int spfa(int u)
{
queue<int>que;
que.push(u);
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
vis[u] = ;
dis[u] = ;
while(que.size())
{
int x = que.front();
q.pop();
vis[x] = ;
for(int i = head[x];i != -;i = s[i].next)
{
int v = s[i].v;
if(dis[v]>dis[x]+s[i].w)
{
dis[v] = dis[x]+s[i].w;
if(!vis[v])
{
vis[v] = ;
que.push(v);
}
}
}
}
return dis[n] <= mi;
} int ok(int m)
{
for(int i=;i<=n;i++)
{
for(int j = head[i];j != -;j = s[j].next)
{
if(s[j].c<=m)
s[j].w = ;
else s[j].w = ;
}
}
return spfa();
} int main()
{
int m;
while(scanf("%d %d %d",&n, &m,&mi)!=EOF)
{
memset(head,-,sizeof(head));
k = ;
int u,v,c;
int r = ;
for(int i = ; i < m; i++)
{
scanf("%d %d %d",&u,&v,&c);
add(u,v,c);
add(v,u,c);
r = max(r,c);
}
int l = ,mid;
int ans = -;
while(l <= r)
{
mid = (l+r)/;
if(ok(mid))
{
ans = mid;
r = mid-;
}
else l = mid+;
}
printf("%d\n",ans);
}
}
(poj 3662) Telephone Lines 最短路+二分的更多相关文章
- POJ - 3662 Telephone Lines (dijstra+二分)
题意:有N个独立点,其中有P对可用电缆相连的点,要使点1与点N连通,在K条电缆免费的情况下,问剩下的电缆中,长度最大的电缆可能的最小值为多少. 分析: 1.二分临界线(符合的情况的点在右边),找可能的 ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- poj 3662 Telephone Lines(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
- poj 3662 Telephone Lines
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7115 Accepted: 2603 D ...
- 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...
- POJ 3662 Telephone Lines (分层图)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6785 Accepted: 2498 D ...
- poj 3662 Telephone Lines dijkstra+二分搜索
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accepted: 2071 D ...
- POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】
<题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...
随机推荐
- 第5章 简单的C程序设计——循环结构程序设计
5.1 为什么需要循环控制 前面介绍了程序中常用到的顺序结构和选择结构,但是只有这两种结构是不够的,还需要用到循环结构(或称重复结构).因为在程序所处理的问题中常常遇到需要重复处理的问题. 循环结构和 ...
- Spring之旅第五篇-AOP详解
一.什么是AOP? Aspect oritention programming(面向切面编程),AOP是一种思想,高度概括的话是“横向重复,纵向抽取”,如何理解呢?举个例子:访问页面时需要权限认证,如 ...
- .NET Core微服务之基于Steeltoe使用Eureka实现服务注册与发现
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- pycharm安装svn插件
弄了svn的服务端和客户端,为了方便我pycharm的使用,我又在pycharm里进行了配置,要用到subversion 下载 https://sourceforge.net/projects/win ...
- Spring Boot(四):Thymeleaf 使用详解
在上篇文章Spring Boot (二):Web 综合开发中简单介绍了一下 Thymeleaf,这篇文章将更加全面详细的介绍 Thymeleaf 的使用.Thymeleaf 是新一代的模板引擎,在 S ...
- 图像检索(2):均值聚类-构建BoF
在图像检索时,通常首先提取图像的局部特征,这些局部特征通常有很高的维度(例如,sift是128维),有很多的冗余信息,直接利用局部特征进行检索,效率和准确度上都不是很好.这就需要重新对提取到的局部特征 ...
- C#的Lock
有时候在编写线程并发的时候需要考虑异步和同步的问题.有些资源只能是一个线程访问,其他的线程在这个线程没有释放资源前不能访问.类似于操作系统中临界资源的访问.C#Lock包裹的代码块具有原子操作的特性( ...
- Openlayers系列(一)关于地图投影相关错误的解决方案
背景 近期开发以MongoDB为基础的分布式地理数据管理平台系统,被要求做一个简单的demo给客户进行演示.于是笔者便打算向数据库中存储一部分瓦片数据,写一个简单的存取服务器,使用Openlayers ...
- typescript的数据类型
typescript是JavaScript的扩展,说到js的数据类型,大家肯定会想到它是一种弱类型的语言,不需要指定特定的数据类型,ts的语言也正在往java这些强类型的语言靠近: 看看typescr ...
- DEDE整站动态/静态转换
方法一:使用DEDE后台的SQL命令行工具 入口:织梦后台-系统-SQL命令行工具 DEDE整站动态化 将所有栏目设置为“使用动态页”: 将所有文档设置为“仅动态”: DEDE整站静态化 将所有栏目设 ...