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. [RN] 05 - Let's start with UI Design

    aws-mobile-react-native-starter 官方的例子,当然要摸一次. 代码要跑起来:aws-samples/aws-mobile-react-native-starter 教程: ...

  2. MyBatis 与 Spring 是如何结合在一起工作的——mybatis-spring(version:1.2.2)

    在MyBatis-Spring的项目中,我们一般会为MyBatis配置两个配置文件 beans-mybatis.xml 和 mybatis-config.xml.其中 beans-mybatis.xm ...

  3. Java使用for循环输出杨辉三角

    杨辉三角形由数字排列,可以把它看做一个数字表,其基本特性是两侧数值均为1,其他位置的数值是其正上方的数字与左上角数值之和.编写程序,使用for循环输出包括10行在内的杨辉三角形. 思路是创建一个整型二 ...

  4. APP图标制作以及替换步骤

    1 首先要有一张1024X1024像素以上的的大图片(长宽最好相等)   2 如果app图标需要的是圆角的,那先通过以下这个工具转换一下: http://www.360doc.com/content/ ...

  5. 5 -- Hibernate的基本用法 --4 6 Hibernate事务属性

    事务也是Hibernate持久层访问的重要方面,Hibernate不仅提供了局部事务支持,也允许使用容器管理的全局事务. Hibernate关于事务管理的属性: ⊙ hibernate.transac ...

  6. Python对象(下)

    前面一篇文章介绍了一些Python对象的基本概念,这篇接着来看看Python对象相关的一些内容. Python对象的比较 Python对象有三个要素:身份,类型和值,所以我们就分别从这三个角度出发看看 ...

  7. Web暴力破解--前端JS表单加密进行爆破

    0x01 前言 常见的js实现加密的方式有:md5.base64.shal,写了一个简单的demo作为测试. 0x02 代码 login.html <!DOCTYPE HTML> < ...

  8. PHP 使用 MongoDB

    PHP 想要往 MongoDB 里增删查改数据,需要先安装 mongodb 或 mongo 扩展模块,一般两个都装上: cd /usr/local/src/ wget https://pecl.php ...

  9. 分析JobInProgress中Map/Reduce任务分配

    1.JobTracker能否决定给当前的TaskTracker节点分配一个Job的具体的哪一个任务? 2.什么是map本地任务? 3.nonRunningMapCache的作用是什么? 4.从Task ...

  10. tomcat端口被占用的两个解决方法

    tomcat 的 8080 端口经常会被占用,解决办法两个: 1.关闭占用8080端口的进程:8080端口被占用的话执行startup.bat会报错,可在cmd下执行netstat -ano命令查看8 ...