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 ...
随机推荐
- Spring框架是怎么解决Bean之间的循环依赖的 (转)
问题: 循环依赖其实就是循环引用,也就是两个或则两个以上的bean互相持有对方,最终形成闭环.比如A依赖于B,B依赖于C,C又依赖于A.如下图: 如何理解“依赖”呢,在Spring中有: 构造器循 ...
- myBatis+Spring+SpringMVC框架面试题整理
myBatis+Spring+SpringMVC框架面试题整理(一) 2018年09月06日 13:36:01 新新许愿树 阅读数 14034更多 分类专栏: SSM 版权声明:本文为博主原创文章 ...
- java构造方法的注意事项总结
构造方法细节总结~~~~~ 1:首先要了解为什么需要构造方法,,,类中有太多的属性,每次给属性赋值时非常麻烦:编码量大,无法重用给属性赋值的代码.. 2:什么是构造方法呢? 构造方法负责初始化类中的实 ...
- 前端Ajax通过设置 timeout 参数,轮询后台API
因为我连接的数据库在台湾,相距较远,所以conn.Open()方法打开极慢.前端Ajax访问API时,API的数据还未返回,前端Ajax访问已经超时. 所以设置一个轮询,设置相隔多少秒之后进行一次查询 ...
- 二、redis学习(java操作redis缓存的工具jedis)
- 爬取快代理的免费IP并测试
各大免费IP的网站的反爬手段往往是封掉在一定时间内访问过于频繁的IP,因此在爬取的时候需要设定一定的时间间隔,不过说实话,免费代理很多时候基本都不能用,可能一千个下来只有十几个可以用,而且几分钟之后估 ...
- 键盘事件 Ctrl+p 模拟(vue)
方法定义 // 打印页面 printpage(myDiv) { // myDiv 为打印对象的id名 var newstr = document.getElementById(myDiv).inner ...
- 转pip更新后ImportError: cannot import name ‘main'
更新pip后,报出:ImportError: cannot import name ‘main' 根据https://www.cnblogs.com/dylan9/p/8981155.html的教程进 ...
- 从FBV到CBV二(认证器)
span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror ...
- filepath:处理文件路径的一把好手
1.ToSlash(path string) string 将相关平台的路径分隔符转为/ package main import ( "fmt" "os" &q ...