poj3662 Telephone Lines【最短路】【二分】
http://poj.org/problem?id=3662
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions:9310 | Accepted: 3374 |
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
#include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
using namespace std;
typedef long long LL;
#define N 100010
#define pi 3.1415926535 const int maxn = ;
const int maxp = ; int n, p, k;
struct node{
int v, w, nxt;
}e[maxp * ];
int tot = , head[maxn];
LL dis[maxn];
bool vis[maxn]; void addedge(int u, int v, int w)
{
e[tot].v = v;
e[tot].w = w;
e[tot].nxt = head[u];
head[u] = tot++;
e[tot].v = u;
e[tot].w = w;
e[tot].nxt = head[v];
head[v] = tot++;
} int dijkstra(int mid)
{
memset(dis, 0x3f, sizeof(dis));
memset(vis, , sizeof(vis));
dis[] = ;
priority_queue<pair<LL, int> >que;
que.push(make_pair(, ));
while(que.size()){
int x = que.top().second;que.pop();
if(vis[x])continue;
vis[x] = true;
for(int i = head[x]; i != -; i = e[i].nxt){
int y = e[i].v, z = e[i].w;
if(z > mid)z = ;
else z = ;
if(dis[y] > dis[x] + z){
dis[y] = dis[x] + z;
que.push(make_pair(-dis[y], y));
}
}
}
return dis[n];
} int main()
{
while(scanf("%d%d%d", &n, &p, &k) != EOF){
for(int i = ; i < n; i++){
head[i] = -;
}
tot = ; int ed = -;
for(int i = ; i < p; i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
ed = max(ed, w);
addedge(u, v, w);
} //printf("%d\n", dijkstra(0));
if(dijkstra() == )printf("-1\n");
else{
int st = , ans;
while(st <= ed){
int mid = (st + ed) / ;
if(dijkstra(mid) <= k){
ans = mid;
ed = mid - ;
}
else{
st = mid + ;
}
}
printf("%d\n", ans);
} }
return ;
}
poj3662 Telephone Lines【最短路】【二分】的更多相关文章
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...
- POJ3662 Telephone Lines (dijkstra+二分)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...
- POJ3662 Telephone Lines( dijkstral + 二分 )
POJ3662 Telephone Lines 题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的, 求这m-k条边中花费最大的一条边的最 ...
- Luogu P1948 [USACO08JAN]电话线Telephone Lines(最短路+dp)
P1948 [USACO08JAN]电话线Telephone Lines 题意 题目描述 Farmer John wants to set up a telephone line at his far ...
- poj-3662 Telephone Lines 二分答案+最短路
链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...
- [Usaco2007 Jan]Telephone Lines架设电话线[二分答案+最短路思想]
Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...
- BZOJ1614:[USACO]Telephone Lines架设电话线(二分,最短路)
Description FarmerJohn打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司 支付一定的费用.FJ的农场周围分布着N(1<=N< ...
- BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)
题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...
随机推荐
- UpLoader------实现上传大文件
代码: <div id="selectFile">选择文件1</div> <script> var da = newGuid(); var kk ...
- SpringMVC由浅入深day01_1springmvc框架介绍
springmvc 第一天 springmvc的基础知识 课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处 ...
- Git Step by Step – (8) Git的merge和rebase
前面一篇文章中提到了"git pull"等价于"git fetch"加上"git merge",然后还提到了pull命令支持rebase模式 ...
- 【代码审计】iZhanCMS_v2.1 后台任意文件删除漏洞分析
0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...
- Git 学习笔记--Git下的冲突解决
冲突的产生 很多命令都可能出现冲突,但从根本上来讲,都是merge 和 patch(应用补丁)时产生冲突. 而rebase就是重新设置基准,然后应用补丁的过程,所以也会冲突. git pull会自动m ...
- LNMP 简介
LNMP 代表的就是:Linux 操作系统下,Nginx + MySQL + PHP 这种网站服务器架构 工作流程:Nginx 直接处理静态请求,动态请求会转发给 php-fpm ( php 是作为一 ...
- C++易混淆知识点整理
// 1 /////////////////////////////////////////////////////////////////////// // 常量指针:,指针可修改,变量不可修改(只 ...
- Java面试题全集
Java面试题全集(上) Java面试题全集(中) Java面试题全集(下) http://www.importnew.com/21445.html
- 浏览器解析html全过程详解
前端文摘:深入解析浏览器的幕后工作原理 关于浏览器解析html全过程详解 输入URL到浏览器接收返回的数据的整个过程 TCP报文格式详解 IP报文格式详解 Linux IO模式及 select.pol ...
- Eclipse中如何在指定工程中搜索指定的字符串
1.点击Search: 2.在下拉框中先择Search. 3. 4. 5.选择Java 6.