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

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

题意:给你n个节点,p条路(不一定每个节点之间都有路),每条路都对应一定的花费,现在修路公司可以免费为你修k条路,求不是免费的路中花费最大的一条的花费;
 
错因:分析出了这道问题其实就是最小化第k+1个数,然后一直在往这个方向是上想,又知道求数组中第k大数只要>x的数量>=k的最大x+1,就好,
 
分析:这道题很典型啊,
 http://blog.163.com/boatswain%40126/blog/static/169396412201071784521862/  分析的很到位
想像一个单调的函数,就不难写二分了,不过最后要注意区间的开闭性会影响到左右端点的初始化

总结二分搜索的两句话:1:球数组中的第k小数,就是求<x的数量>=k的最小x-1;

2.求数组中的第k大数,就是求>=x的数量>=k的最大x

最后非常重要的是:这两个求出来的数在数组中一定是存在的。

这个总结几乎可以概括来这段时间做的所有题了

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const double g = ;
int tu[][], tu2[][], d[], used[];
int n, p, k, f, t, c;
void init2(int mid)
{
memset(used, , sizeof(used));
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
if (tu[i][j] == inf)
tu2[i][j] = inf;
else if (tu[i][j] >mid)
tu2[i][j] = ;
else
tu2[i][j] = ;
for (int i = ; i <= n; i++)
d[i] = tu2[][i];
d[] = ; used[] = ;
}
int ok(int mid)
{
init2(mid);
while ()
{
int u = , minn = inf;
for (int i = ; i <= n; i++)
if (d[i]<minn&&!used[i])
{
minn = d[i];
u = i;
}
if (!u) break;
used[u] = ;
for (int i = ; i <= n; i++)
if (d[i]>d[u] + tu2[u][i] && !used[i])
d[i] = d[u] + tu2[u][i];
}
if (d[n] >= inf)
return -;
else return d[n] <= k;
}
void init1()
{
memset(tu, inf, sizeof(tu));
for (int i = ; i <= n; i++)
tu[i][i] = ;
}
int main()
{
while (~scanf("%d %d %d", &n, &p, &k))
{
int l=-, r = ;
init1();
for (int i = ; i <= p; i++)
{
scanf("%d %d %d", &f, &t, &c);
tu[f][t] = tu[t][f] = c;
if (c>r) r = c;
}
int flag = ;
while (r - l> && flag)
{
int mid = (l + r) >> ;
int w = ok(mid);
if (w == )
r = mid;
else if (w == )
l = mid;
else if (w == -)
{
printf("-1\n");
flag = ;
}
}
if (flag)
printf("%d\n", r);
}
return ;
}

  下面是第一次wa的代码:

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const double g=;
int tu[][],tu2[][],d[],used[];
int n,p,k,f,t,c;
int ok(int mid)
{
memset(tu2,inf,sizeof(tu2));
memset(d,inf,sizeof(d));
memset(used,,sizeof(used));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(tu[i][j]!=inf)
if(tu[i][j]>mid)
tu2[i][j]=;
else
tu2[i][j]=;
d[]=;used[]=;
while()
{
int u=,minn=inf;
for(int i=;i<=n;i++)
if(d[i]<minn&&!used[i])
{
minn=d[i];
u=i;
}
if(!u)
break;
used[u]=;
for(int i=;i<=n;i++)
if(d[i]>d[u]+tu2[u][i]&&!used[i])
d[i]=d[u]+tu2[u][i];
}
return d[n]>=k;
}
int main()
{
while(~scanf("%d %d %d",&n,&p,&k))
{
int l=,r=;
memset(tu,inf,sizeof(tu));
for(int i=;i<=p;i++)
{
scanf("%d %d %d",&f,&t,&c);
tu[f][t]=tu[t][f]=c;
if(c>r) r=c;
}
r++;
while(r-l>)
{
int mid=(l+r)>>;
if(ok(mid))
l=mid;
else
r=mid;
}
printf("%d\n",l+);
}
return ;
}

poj 3662 Telephone Lines dijkstra+二分搜索的更多相关文章

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

    题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...

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

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

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

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

  4. poj 3662 Telephone Lines

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

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

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

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

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

  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)

    Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...

  9. POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)

    题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...

随机推荐

  1. Docker守护进程

    Docker安装完成之后, 需要确定Docker的守护进程是否已经运行. Docker是使用root 权限运行他的程序,进而可以处理普通用户无法完成的操作(比如挂载文件系统). docker程序是Do ...

  2. C++ Primer 回炉重铸(一)

    过去学C++语法都是用的这本C++Primer第五版 说实话,这本书应该是业界用的最多的一本类似于C++语法的百科全书了.. 但是感觉自己学了这么长时间的C++,语法层次还是不够牢固. 比如templ ...

  3. 下载安装npm和cnpm

    下载安装npm http://nodejs.cn/download/ 下载安装cnpm 完成npm的安装后,再安装cnpm https://npm.taobao.org/ 切换源为 taobao 源 ...

  4. Swoft 2.0.5 更新,新增高效秒级定时任务、异常管理组件

    什么是 Swoft ? Swoft 是一款基于 Swoole 扩展实现的 PHP 微服务协程框架.Swoft 能像 Go 一样,内置协程网络服务器及常用的协程客户端且常驻内存,不依赖传统的 PHP-F ...

  5. js执行多次事件,而非一次

    晚上查阅了很多文章,都是避免点击事件多次执行.反过来要是让事件多次执行该如何做? 这里可以配个setTimeout():来执行 这里我们用layui <link rel="styles ...

  6. PHP与MySQL的连接

    一.PHP的相关扩展 PHP与MySQL的交互需要要借助PHP提供的数据库扩展,在PHP中提供了多种数据库扩展,常用的MySQL扩展, MySQLi扩展和PDO扩展. 1.三者各自的特点: MySQL ...

  7. js跳转页面的方法

    js跳转页面的几种方法 第一种:(跳转到b.html) <script language="javascript" type="text/javascript&qu ...

  8. Clang调试CUDA代码

    Clang调试CUDA代码全过程 有空再进行编辑,最近有点忙,抱歉 使用的llvm4.0+Clang4.0的版本,依据的是上次发的llvm4.0和clang4.0源码安装的教程https://www. ...

  9. python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取

    基于示例infer_simple.py 修改165行vis_utils.vis_one_image为vis_utils.vis_one_image_opencv 在detectron.utils.vi ...

  10. Spring整合Hessian访问远程服务

    声明:该文章转载自Spring整合Hessian访问远程服务,本人搬过来只是为了记录下学习Hessian的过程,忘此博主理解,在此感谢,等本人有能力了再学一些原创的东东,本人实践了下,hessianS ...