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 ...
随机推荐
- JAVAEE 7 api.chm
JAVAEE 7 api.chm 链接:https://pan.baidu.com/s/1LUD3oam5B-Hp8tdpfQYk2w 提取码:x1kc
- C++多线程基础学习笔记(二)
先总结延申以下前面(一)所讲的内容. 主线程从main()函数开始执行,我们创建的线程也需要一个函数作为入口开始执行,所以第一步先初始化函数. 整个进程是否执行完毕的标志是主线程是否执行完毕,一般情况 ...
- Vue的快速入门
1 环境准备 1 下载安装Node 地址https://nodejs.org/en/download/ 完成后通过cmd打开控制台输入node -v 可以看到版本信息 2 通过该node自带的npm ...
- IDEA项目目录里下找不到src,但是src确实存在的的解决方案
写代码的时候可能出现写着写着src就找不到了,我个人认为是触发了热键导致src被隐藏了,下面就是设置src可见和不可见的操作 这个其实是被隐藏了,打开就好,位置如下:
- pwd命令和修改PS1环境变量在bash行的显示
一.pwd:显示当前所在的位置 语法 pwd [选项] ... 描述 打印当前工作目录的完整文件名. -L,--logical 从环境使用PWD,即使它包含符号链 ...
- 移动端、pc端通用点击复制
点击复制 function copyArticle(event){ const range = document.createRange(); range.selectNode(document.ge ...
- 关于IDEA,多服务运行 Services -> Run Dashboard 部分服务添加变灰色,限制使用5个启动类,重启之后需要重新添加,服务在 Run Dashboard 中的显示排序问题,不显示 Services(Run Dashboard)
我的IDEA版本为最新版本 变灰色的原因就是因为右键删除了那个启动的主配置类,然后就会显示灰色,再次打开这个醒目,就不会在Run Dashboard中显示这个主配置类了 解决方法 如果你要 调整这些服 ...
- python多线程之_thread
多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...
- JS异步上传文件
直接调用Upload(option)方法,即可上传文件,不需要额外的插件辅助,采用原生js编写. /* *异步上传文件 *option参数 **url:上传路径 **data:上传的其他数据{id:& ...
- 用python编写一个合格的ftp程序,思路是怎样的?
经验1.一般在比较正规的类中的构造函数.都会有一个verify_args函数,用于验证传入参数.尤其是对于系统传参.2.并且系统传参,其实后面大概都是一个函数名 例如:python server. ...