(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+ ...
随机推荐
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十五║初探SSR服务端渲染(个人博客二)
缘起 时间真快,现在已经是这个系列教程的下半部 Vue 第 12 篇了,昨天我也简单思考了下,可能明天再来一篇,Vue 就基本告一段落了,因为什么呢,这里给大家说个题外话,当时写博文的时候,只是想给大 ...
- 过程 sp_addextendedproperty, 对象无效。不允许有扩展属性,或对象不存在。
过程 sp_addextendedproperty, 对象无效.不允许有扩展属性,或对象不存在. 首先这个是创建数据表的SQL,有Power Designer创建模型,直接生成的创建SQL会出现的问题 ...
- 『集群』006 Slithice 后期改进 和 Slithice可能存在的BUG
Slithice 后期改进 和 Slithice可能存在的BUG Slithice 可能存在的 BUG: >Slithice 暂时 没有 对 循环调度 进行控制:不正确的 配置 可能导致 调度死 ...
- .net4.5部署到docker容器
.net4.5部署到docker容器 部署到windows容器 部署到linux容器 部署到windows容器 由于.net本身就是运行在windows平台的,所以它与windows容器也是更加适合, ...
- [开发技巧]·Numpy中对axis的理解与应用
[开发技巧]·Numpy中对axis的理解与应用 1.问题描述 在使用Numpy时我们经常要对Array进行操作,如果需要针对Array的某一个纬度进行操作时,就会用到axis参数. 一般的教程都是针 ...
- Flutter 即学即用系列博客——09 EventChannel 实现原生与 Flutter 通信(一)
前言 紧接着上一篇,这一篇我们讲一下原生怎么给 Flutter 发信号,即原生-> Flutter 还是通过 Flutter 官网的 Example 来讲解. 案例 接着上一次,这一次我们让原生 ...
- Jquer + Ajax 制作上传图片文件
没什么 说的 直接 上代码 //选择图片并上传 function selectImg(node){ var f = node.value; var file = node.files[0]; if( ...
- 关于C#chart图表实现多条折线动态绑定数据的问题
之前就已经实现了多条折线绑定数据并显示,但不是动态绑定,而是每一条数据都要进行一次绑定,个人觉得在解决实际问题时,这样的解决方法过于笨重且缺乏扩展性,这次主要是对代码进行优化,实现写一遍代码,无论数据 ...
- 如何在Linux服务器和windows系统之间上传与下载文件
Do not let dream just be your dream. 背景:Linux服务器文件上传下载. XShell+Xftp安装包(解压即用)百度网盘链接:https://pan.baidu ...
- 基于hadoop分析,了解hive的使用
一.Hadoop理论 Hadoop是一个专为离线和大规模数据分析而设计的,并不适合那种对几个记录随机读写的在线事务处理模式. Hadoop=HDFS(文件系统,数据存储技术相关)+ Mapreduce ...