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.

输入输出样例

输入样例#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

输出样例#1:

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=-1,tot,num;
int dis[MAX],vis[MAX];
int to[MAXN*2],head[MAXN*2],net[MAXN*2],cap[MAXN*2];
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,0,sizeof(vis));memset(dis,0x3f,sizeof(dis));que.push(s);vis[s]=1;dis[s]=0;while(!que.empty()){int now=que.front();que.pop();vis[now]=0;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]]=1;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=1;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+1,edge+1+p,cmp);spfa(1);if(dis[n]==1061109567){cout<<"-1";return 0; } l=1;r=p;while(l<r){mid=(l+r)/2;spfa(1);if(dis[n]>k)    l=mid+1;else{r=mid;ans=edge[mid].z;}}if(dis[n]<k)    cout<<"0";else cout<<ans;
}

怕你飞远去 怕你离我而去

  更怕你永远停留在这里

    每一滴泪水 都向你流淌去

       倒流进天空的海底

转载于:https://www.cnblogs.com/cangT-Tlan/p/7469141.html

洛谷 P1948 [USACO08JAN]电话线Telephone Lines相关推荐

  1. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines【NOIP模拟笨笨的电话线】

    [二分,spfa chenck] 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the p ...

  2. 洛谷 P1948 / loj 10074 / 一本通 1496【分层图】

    坑点:求得是最大边权,dijk需要把求和改成最大值. #include <bits/stdc++.h> #define int long longusing namespace std;v ...

  3. POJ3612 洛谷P2885 [USACO07Nov] Telephone Wire 架设电话线 dp

    题目链接:洛谷 POJ 题目描述 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的 N ( ...

  4. [BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1806  Solve ...

  5. bzoj 1614: [Usaco2007 Jan]Telephone Lines架设电话线(二分+SPFA)

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1761  Solve ...

  6. [BZOJ1614][Usaco2007 Jan]Telephone Lines架设电话线

    [Usaco2007 Jan]Telephone Lines架设电话线 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Farmer John打算将电话线引到自己的农场,但电信公司并不打算 ...

  7. 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game

    洛谷 2953  [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...

  8. 记录奥林比克/课程录制 洛谷P2255 [USACO14JAN]

    题面在最下方. 本题贪心可解,我也不是很懂动规解法(双线程DP?) 先把各个课程(比赛)按结束时间从小到大排序,记录两个摄像机的结束时间. 然后枚举课程,如果某个课程的开始时间早于任何一台摄像机的结束 ...

  9. 洛谷 P3184 [USACO16DEC]Counting Haybales数草垛

    洛谷 P3184 [USACO16DEC]Counting Haybales数草垛 题目描述 Farmer John has just arranged his NN haybales (1 \leq ...

最新文章

  1. tensorboard 使用教程
  2. Netty的实现原理、特点与优势、以及适用场景
  3. 4005基于邻接表的顶点的删除(C++,附思路)
  4. npm 端口设置成80_13 个 NPM 快速开发技巧
  5. 利用VB函数Dir()实现递归搜索目录
  6. Java--接口、抽象与继承
  7. #leetcode刷题之路39-组合总和
  8. [转]ETL模型设计
  9. qtouch跨平台组态软件四位一体表现
  10. Markdown部分语法使用
  11. 磊科路由器信号按键_磊科路由器信号增强怎么设置方法
  12. 做短视频必须要知道的几个视频设置参数,爆款必备。
  13. 当代年轻人该如何跨越阶层?
  14. 香港机房BGP线路有什么用
  15. COGS-2049 疯狂动物城
  16. Hadoop3.3.4最新版本安装分布式集群部署
  17. 点击的时候直接跳转到 微信界面
  18. 在 VMware Workstation 16 Pro 中安装 Windows 10 x64
  19. 软件设计师考试整理-0-前言
  20. grub rescue修复引导

热门文章

  1. Android程序对不同手机屏幕分辨率自适应的总结
  2. 使用sharding-jdbc实现水平分表
  3. ORA-01502-对应的快速解决办法(索引或这类索引的分区处于不可用状态)
  4. 一道很简单却也很容易入坑的java面试题
  5. testng执行参数_初识TestNG测试框架
  6. OpenSSL新手自学:如何生成RSA私钥并用于数字签名
  7. keil窗口显示不全_使用Keil语言的嵌入式C编程教程(下)
  8. 12个免费的 Twitter Bootstrap 后台模板
  9. python 堆栈溢出_IAR堆栈溢出的问题
  10. 计算机软件系统课程导入,中学信息技术 计算机系统的组成课件 硬件软件导入恰当...