http://poj.org/problem?id=3662

Telephone Lines
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 {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$条边,每条边有一个权值(花费)。将第$1$个点和第$n$个点连通,并且可以有$k$条边是免费的。剩下的不免费的边的最大值作为最终的花费。求最终花费的最小值。
思路:
刚开始题意理解错了。以为是有$k$长度的是免费的,边的那个权值是长度。想了半天搞不懂。
后来发现其实就是求一个使路径上第$k+1$大的边权尽量小的路径。【虽然还是不会】
因为当我们支付的钱变多时,合法的路径一定包含了花费更少的路径。答案具有单调性。
所以我们可以二分答案。【注意想题目答案的单调性尝试二分】
这时候问题就变成了,把价格超过$mid$的边花费看成是$1$,不超过的边花费看成是$0$,然后求$1~N$的最短路是否不超过$k$就可以了。
要注意考虑$1$和$N$不连通的情况,输出是$-1$
 
虐狗宝典上还有一个思路是用dp,但是我不是很会写。
用$D[x,p]$表示从$1$号节点到基站$x$,途中已经指定了$p$条电缆免费时,经过的路径上最贵的电缆的花费最小是多少(选择一条$1$到$x$的路径,使路径上第$p+1$大的边权尽量小)。若有一条从$x$到$y$长度是$z$的无向边,用$max(D[x,p],z)$更新$D[y,p]$的最小值,用$D[x,p]$更新$D[y, p+1]$的最小值。前者表示不在电缆$(x,y,z)$上使用免费升级服务,后者表示使用。用迭代的思想,借助SPFA进行动态规划,直至所有状态收敛。
还可以把图中节点拓展到二维,用二元组$(x,p)$表示一个节点,从$(x,p)$到$(y,p)$有长度为$z$的边,从$(x,p)$到$(y,p+1)$有长度为0的边。问题就变成了$N*K$个点,$P*K$条边的广义单源最短路问题。
 #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【最短路】【二分】的更多相关文章

  1. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

  2. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...

  3. POJ3662 Telephone Lines (dijkstra+二分)

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...

  4. POJ3662 Telephone Lines( dijkstral + 二分 )

    POJ3662 Telephone Lines 题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的, 求这m-k条边中花费最大的一条边的最 ...

  5. Luogu P1948 [USACO08JAN]电话线Telephone Lines(最短路+dp)

    P1948 [USACO08JAN]电话线Telephone Lines 题意 题目描述 Farmer John wants to set up a telephone line at his far ...

  6. poj-3662 Telephone Lines 二分答案+最短路

    链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...

  7. [Usaco2007 Jan]Telephone Lines架设电话线[二分答案+最短路思想]

    Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...

  8. BZOJ1614:[USACO]Telephone Lines架设电话线(二分,最短路)

    Description FarmerJohn打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司 支付一定的费用.FJ的农场周围分布着N(1<=N< ...

  9. BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)

    题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...

随机推荐

  1. apache 配置会话保持

    1.修改apache_home/conf/httpd.conf,增加以下模块(取消注释,如有其他依赖, 则相应取消注释) LoadModule proxy_module modules/mod_pro ...

  2. SPOJ QTREE5 lct

    题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set ...

  3. java图片裁剪和java生成缩略图

    一.缩略图 在浏览相冊的时候.可能须要生成相应的缩略图. 直接上代码: public class ImageUtil { private Logger log = LoggerFactory.getL ...

  4. Dubbo -- 系统学习 笔记 -- 示例 -- 直连提供者

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 直连提供者 在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候 ...

  5. bat批处理设置静态、动态、ping、查看网络配置

    @echo off :startIP set /p source=S or D or C or P or E: echo source:%source% if /i "%source%&qu ...

  6. 在eclipse中查看android源代码

    自己写了一个类MainAcvitivity extends Activity, 按F12(我把转到定义改成了F12的快捷键),转到Activity的定义,弹出下面这样的界面 就是说没有找到androi ...

  7. javascript 显示一定范围内的素数(质数)

    素数又称质数,是大于1的自然数,并且只有1和它本身两个因数. 具体实现代码如下: 运行代码 <!DOCTYPE HTML> <html> <head lang=" ...

  8. GSAP JS基础教程--认识GSAP JS

    第一次写博文呢,这次写博客是因为应一位同学的要求,写一下GSAP JS的一个小教程.为什么说小呢?因为它实际上就是小,只是一个入门级的小教程.如果你想问:“那你为什么不写详细一点呢?”,我想说,说., ...

  9. Maven构建Web项目

    1. Maven的作用: [清理项目]→[编译项目]→[测试项目]→[生成测试报告]→[打包项目]→[部署项目] 2. 安装maven maven对JDK版本有要求,所以要设置好JDK的环境变量. M ...

  10. es 5.0 拼音分词器 mac

    安装方法和ik中文分词器一样, 先下载: https://github.com/medcl/elasticsearch-analysis-pinyin 执行: mvn package; 打包成功以后, ...