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. 使用javascript完成一个简单工厂设计模式。

    在JS中创建对象会习惯的使用new关键字和类构造函数(也是可以用对象字面量). 工厂模式就是一种有助于消除两个类依赖性的模式. 工厂模式分为简单工厂模式和复杂工厂模式,这篇主要讲简单工厂模式. 简单工 ...

  2. 像写SQL语句一样写Java代码

    @Data @AllArgsConstructor public class Trader { private final String name; private final String city ...

  3. C/C++的几个输入流

    C: 1.scanf( ) 存在于<stdio.h>(C++为<cstdio>)中,根据stdin读取数据并根据参数格式进行赋值,以第一个非空格字符(空格字符如:空格,制符表, ...

  4. c# ListView 简单操作

    1. 添加数据 listView1.Items.Clear(); for (int i = 0; i < 50; i++) { ListViewItem lv = new ListViewIte ...

  5. thinkphp3.2.3 自动验证 unique 出错的解决办法

    场景:修改数据时,唯一验证name字段出错,提示已存在. 排查: 1.传入的参数是否包含主键,因为 D('模型名')->create() 会自动判断是否是新增或者修改,根据传入的参数判断是否包含 ...

  6. Docker 环境下部署 redash

    环境: centos7 官网:https://redash.io/help/open-source/dev-guide/docker 一.安装步骤 1.虚拟机安装 安装vmware,并安装centos ...

  7. 好用的 python 工具集合

    图标处理小程序, 妈妈再也不用担心我不会制作图标了 # PythonMargick包可以到Unofficial Windows Binaries for Python Extension Packag ...

  8. CentOS MySql5.6编译安装

    生产环境中,mysql服务器上边最好什么服务都不要再安装!!! 一.准备工作: # yum -y install make gcc-c++ cmake bison-devel ncurses-deve ...

  9. Delphi BitBtn组件

  10. linux下测试web访问及网络相关的命令

    curl命令 curl是linux系统命令行下用来简单测试web访问的工具. curl -xip:port www.baidu.com    -x可以指定ip和端口,省略写hosts,方便实用 -I  ...