(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+ ...
随机推荐
- Java基础——多线程(持续更新中)
如何建立一个执行路径呢? 通过查询API文档 java.lang.Thread类 该类的描述中有创建线程的两种方式 1.继承Thread类 (1).将类声明为 Thread 的子类 (2).该子类应重 ...
- C/C++性能测试工具GNU gprof
代码剖析(Code profiling)程序员在优化软件性能时要注意应尽量优化软件中被频繁调用的部分,这样才能对程序进行有效优化.使用真实的数据,精确的分析应用程序在时间上的花费的行为就成为_代码剖析 ...
- vue 传值 概述 个人理解
1 父传子 子组件 props:[‘自定义属性名’] 父组件 v-bind:自定义属性名="值" 理解 子组件创建一个自定属性 父组件使用vue指令绑定到 自定义 ...
- 【深度学习篇】--Seq2Seq模型从初识到应用
一.前述 架构: 问题: 1.压缩会损失信息 2.长度会影响准确率 解决办法: Attention机制:聚焦模式 “高分辨率”聚焦在图片的某个特定区域并以“低分辨率”,感知图像的周边区域的模式.通过大 ...
- .NET Core微服务之基于Jenkins+Docker实现持续部署(Part 1)
Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.CI, CD 与Jenkins 互联网软件的开发和发布,已经形成了一套标准流程,最重要的组成部分就是持续集成(Continuous i ...
- Spring Boot 1.5.x 基础学习示例
一.为啥要学Spring Boot? 今年从原来.Net Team“被”转到了Java Team开始了微服务开发的工作,接触了Spring Boot这个新瓶装旧酒的技术,也初步了解了微服务架构.Spr ...
- 【工利其器】必会工具之(三)systrace篇(2)
systrace工具打开路径 以AndroidStudio(后面简写为AS),在顶部菜单栏中 Tools>Android>Android Device Monitor 打开后看到如下界面, ...
- Python--开发简单爬虫
简单爬虫架构 动态运行流程 URL管理器的作用 URL管理器的3种实现方式 网页下载器的作用 Python网页下载器的种类 urllib2下载网页的3种方法 网页解析器的作用 Python的几种网页解 ...
- spring里的三大拦截器
Filter 新建 TimeFilter @Component public class TimeFilter implements Filter { @Override public void in ...
- Python编程从入门到实践笔记——类
Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...