题目链接:http://poj.org/problem?id=3662

Telephone Lines
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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

 
题目大意:找到一条从1 到n的最短路,使这条路的第(K+1)大的边最小。
思路:二分答案,对于每个mid进行最短路,对于大于mid的边变成1,小于等于mid的变成0,如果最短路的值小于等于k,则这个mid满足条件,找到最小的mid。
#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 最短路+二分的更多相关文章

  1. POJ - 3662 Telephone Lines (dijstra+二分)

    题意:有N个独立点,其中有P对可用电缆相连的点,要使点1与点N连通,在K条电缆免费的情况下,问剩下的电缆中,长度最大的电缆可能的最小值为多少. 分析: 1.二分临界线(符合的情况的点在右边),找可能的 ...

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

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

  3. poj 3662 Telephone Lines(最短路+二分)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

  4. poj 3662 Telephone Lines spfa算法灵活运用

    意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...

  5. poj 3662 Telephone Lines

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

  6. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...

  7. POJ 3662 Telephone Lines (分层图)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 D ...

  8. poj 3662 Telephone Lines dijkstra+二分搜索

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 D ...

  9. POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】

    <题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...

随机推荐

  1. AI - TensorFlow - 示例01:基本分类

    基本分类 基本分类(Basic classification):https://www.tensorflow.org/tutorials/keras/basic_classification Fash ...

  2. Hangfire源码解析-任务是如何执行的?

    一.Hangfire任务执行的流程 任务创建时: 将任务转换为Type并存储(如:HangFireWebTest.TestTask, HangFireWebTest, Version=1.0.0.0, ...

  3. springBoot(13)---整合Druid实现多数据源和可视化监控

    SpringBoot整合Druid实现多数据源和可视化监控 先献上github代码地址:https://github.com/yudiandemingzi/springboot-manydatasou ...

  4. 高效并发unsafe-星耀

    定义 Unsafe类是在sun.misc包下,不属于Java标准.但是很多Java的基础类库,包括一些被广泛使用的高性能开发库都是基于Unsafe类开发的,比如Netty.Cassandra.Hado ...

  5. Java集合详解4:HashMap和HashTable

    今天我们来探索一下HashMap和HashTable机制与比较器的源码. 具体代码在我的GitHub中可以找到 https://github.com/h2pl/MyTech 喜欢的话麻烦star一下哈 ...

  6. logistic逻辑回归公式推导及R语言实现

    Logistic逻辑回归 Logistic逻辑回归模型 线性回归模型简单,对于一些线性可分的场景还是简单易用的.Logistic逻辑回归也可以看成线性回归的变种,虽然名字带回归二字但实际上他主要用来二 ...

  7. 流水车间调度算法分析的简单+Leapms实践--混合整数规划的启发式建模

    流水车间调度算法分析的简单+Leapms实践--混合整数规划的启发式建模 清华大学出版社出版的白丹宇教授著作<流水车间与开放车间调度算法渐近分析>采用渐近分析方法分析多个NP-难类启发调度 ...

  8. oracle学习笔记(七) 预编译Statement介绍与使用

    预编译Statement优点 执行效率高 由于预编译语句使用占位符 "?",在执行SQL之前语句会被先发送到Oracle服务器进行语法检查和编译等工作,并将SQL语句加入到Orac ...

  9. MyBatis入门简述

    MyBatis前身是iBatis,为Apache的一个开源项目.2010年迁移到了Google Code,改名为MyBatis.2013年迁移到Github. MyBatis是一个优秀的持久层框架,它 ...

  10. Linux系列

    Linux入门及进阶学习. 目录 Linux的安装 GNOME图形界面的基本操作 命令行BASH的基本操作 Linux文件系统的基本结构 Linux文件基本操作管理 Linux系统目录架构 Linux ...