poj 3662 Telephone Lines dijkstra+二分搜索
| 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 {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
总结二分搜索的两句话: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+二分搜索的更多相关文章
- POJ - 3662 Telephone Lines (Dijkstra+二分)
题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- 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: 7115 Accepted: 2603 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: 6785 Accepted: 2498 D ...
- poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)
Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...
- POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)
题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...
随机推荐
- 小菜鸟之JAVA输入输出
Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观 ...
- Java基础开篇
我是一个2019毕业的非计算机的毕业生,从大二开始喜欢上Java直到现在一直都在学习,Brid从小就对计算机感兴趣,可惜高中的时候不懂事,没有规划未来,考上了一所专科学院,然后大一并不能转专业,现在毕 ...
- Eratosthenes筛法
复杂度为nlogn. 算法思想为:枚举1~sqrt(n),然后把每一个数的倍数都都打上不是素数的标记. 还要特别注意0,1不是素数,打标记枚举到i*k<=n. 代码如下 #include< ...
- mysql以及mysql bench安装教程
首先,我们需要去官网下载mysql(这里以下载) 1 2 3 4 5 下载好了自己好了之后,点击安装好的东西出现如下界面: 1.接受使用条款并点击next 2.点击custom,可以根据个人习惯进行安 ...
- html重置模板
新浪的初始化: html,body,ul,li,ol,dl,dd,dt,p,h1,h2,h3,h4,h5,h6,form,fieldset,legend,img { margin: 0; paddin ...
- js创建jsonArray传输至后台及后台解析
//产品清单 //产品数量长度 var cnbvLength = FieldCount9 + 1; var arrayList=[]; for(var i = 0; i < cnbvLength ...
- (转)Redis持久化的几种方式
radis持久化的几种方式 1.前言 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服 ...
- windows10 mysql主从复制配置
注意:mysql主从复制,主从版本要一致! 生手永远在学习的路上,为了学习mysql主从复制,实现读写分离,于是在本地安装多个mysql实例来进行验证. 也因此有了下面的笔记,一来自我总结一下经验,二 ...
- 借助Charles来测试移动端-上篇
随着现在互联网的兴起,移动端的测试需求越来越多,但是随着用户越来越多,迭代需求越来越频繁,或因为测试环境的接口不稳定,或因为多个业务系统互相关联,导致移动端测试后置,有时候提前介入了,也只能干耗时间, ...
- ubuntu版本查看命令
简单的 在命令终端输入 1.cat /etc/issue (简单) 2.cat /etc/lsb-release(具体) 3.uname -a(内核) 具体的 有时候我们安装软件或者搭建服务的时候,需 ...