洛谷 P1948 [USACO08JAN]电话线Telephone Lines
题目描述
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.
多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。
第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。
电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.
请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?
输入输出格式
输入格式:
输入文件的第一行包含三个数字n,p,k;
第二行到第p+1行,每行分别都为三个整数ai,bi,li。
输出格式:
一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.
输入输出样例
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
4
思路:spfa+二分。本题实际就是求1到n路径上k+1大的边最小是几,自然而然的就想到了二分,二分那条边是第k+1条边。
错因:没有分析最终边数小于k时,答案是0的情况(洛谷不分析AC了,cogsWA了一个点)
吐槽:洛谷的数据太水了。
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAX 1001
#define MAXN 10001
using namespace std;
struct nond{
int x,y,z;
}edge[MAXN];
int n,p,k,l,r,mid,ans=-,tot,num;
int dis[MAX],vis[MAX];
int to[MAXN*],head[MAXN*],net[MAXN*],cap[MAXN*];
int cmp(nond a,nond b){
return a.z<b.z;
}
void add(int u,int v,int w){
to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot;
to[++tot]=u;net[tot]=head[v];cap[tot]=w;head[v]=tot;
}
bool spfa(int s){
queue<int>que;
memset(vis,,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
que.push(s);
vis[s]=;dis[s]=;
while(!que.empty()){
int now=que.front();
que.pop();
vis[now]=;
for(int i=head[now];i;i=net[i]){
if(dis[to[i]]>dis[now]+(cap[i]>edge[mid].z)){
dis[to[i]]=dis[now]+(cap[i]>edge[mid].z);
if(!vis[to[i]]){
vis[to[i]]=;
que.push(to[i]);
}
}
}
}
}
int main(){
//freopen("phoneline.in","r",stdin);
//freopen("phoneline.out","w",stdout);
scanf("%d%d%d",&n,&p,&k);
for(int i=;i<=p;i++){
scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
add(edge[i].x,edge[i].y,edge[i].z);
}
sort(edge+,edge++p,cmp);
spfa();
if(dis[n]==){
cout<<"-1";
return ;
}
l=;r=p;
while(l<r){
mid=(l+r)/;
spfa();
if(dis[n]>k) l=mid+;
else{
r=mid;
ans=edge[mid].z;
}
}
if(dis[n]<k) cout<<"";
else cout<<ans;
}
怕你飞远去 怕你离我而去
更怕你永远停留在这里
每一滴泪水 都向你流淌去
倒流进天空的海底
洛谷 P1948 [USACO08JAN]电话线Telephone Lines的更多相关文章
- 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 题解
P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...
- 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...
- 洛谷P1948 [USACO08JAN]电话线Telephone Lines
题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is u ...
- Luogu P1948 [USACO08JAN]电话线Telephone Lines(最短路+dp)
P1948 [USACO08JAN]电话线Telephone Lines 题意 题目描述 Farmer John wants to set up a telephone line at his far ...
- P1948 [USACO08JAN]电话线Telephone Lines(二分答案+最短路)
思路 考虑题目要求求出最小的第k+1大的边权,想到二分答案 然后二分第k+1大的边权wx 把所有边权<=wx的边权变为0,边权>wx的边权变为0,找出最短路之后,如果dis[T]<= ...
- P1948 [USACO08JAN]电话线Telephone Lines
传送门 思路: 二分+最短路径:可以将长度小于等于 mid 的边视为长度为 0 的边,大于 mid 的边视为长度为 1 的边,最后用 dijkstra 检查 d [ n ] 是否小于等于 k 即可. ...
- [USACO08JAN]电话线Telephone Lines
多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人.该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意 ...
- [USACO08JAN]电话线Telephone Lines(分层图)/洛谷P1948
这道题其实是分层图,但和裸的分层图不太一样.因为它只要求路径总权值为路径上最大一条路径的权值,但仔细考虑,这同时也满足一个贪心的性质,那就是当你每次用路径总权值小的方案来更新,那么可以保证新的路径权值 ...
- 题解【洛谷P1948】[USACO08JAN]电话线Telephone Lines
题面 题解 很显然,答案满足单调性. 因此,可以使用二分答案求解. 考虑\(check\)的实现. 贪心地想,免费的\(k\)对电话线一定都要用上. 每次\(check\)时将小于\(mid\)的边权 ...
随机推荐
- E20170828-mk
utils 工具类; 效用; 实用工具; 实用菜单; Documentation n. 记录; 证明某事属实的证据; 参考资料; 文献的编集,文件分类; receive vt. 收到; 接待; ...
- yii引入js文件
作者:zccst 四.在视图层(../views/..)添加CSS文件或JavaScript文件 Yii::app()->clientScript->registerScriptFile( ...
- Your configuration specifies to merge with the ref 'refs/heads/v.autoCheckProduct.20190325' from the remote, but no such ref was fetched.
问题: 创建新的分支,当我们执行git pull,出现如下错误 解决办法: 1.切换到主分支(或者被依赖的分支,也就是你从哪个分支上拉取新的分支),博主这里是master分支 2.执行以下两个命令 3 ...
- JQuery 动态创建表单,并自动提交
前言:写这个是为了实现使用cookie进行自动登录的功能, 下面的代码是一个元素一个元素进行创建和赋值的, (可以尝试下将所有的html代码(form.input)全部拼好以后放到${ } 中,再进行 ...
- angular js 正序倒叙
<!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- php数据库分页
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java ---书写自己的名字
public class hello { public static void main(String[] args) { // TODO 自动生成的方法存根 System.out.println(& ...
- 【SQL】字符型函数
1. ASCII ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统. 1) 返回 ...
- alert弹出框 弹出窗口 ----sweetAlert
推荐一款好用的alert,下面地址是demo,很直观的看到效果,wap可以使用 http://www.dglives.com/demo/sweetalert-master/example/ < ...
- layui confirm
layer.confirm('是否要删除信息!', { btn: ['确定', '取消'] }, function (index, layero) { //移除元素 $("#tr" ...