博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3662 (二分+SPFA
阅读量:6153 次
发布时间:2019-06-21

本文共 3523 字,大约阅读时间需要 11 分钟。

Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8856   Accepted: 3211

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 11 2 53 1 42 4 83 2 35 2 93 4 74 5 6

Sample Output

4

Source

题意:求一条路径从1到n使第k+1大的边最小。
思路:二分第k+1大的路径长度mid,大于mid的边标记为1,否则标记为0,跑一遍SPFA,当d[n]<=k就为满足条件,取最小的mid。
代码:
1 #include"bits/stdc++.h" 2 #define db double 3 #define ll long long 4 #define vl vector
5 #define ci(x) scanf("%d",&x) 6 #define cd(x) scanf("%lf",&x) 7 #define cl(x) scanf("%lld",&x) 8 #define pi(x) printf("%d\n",x) 9 #define pd(x) printf("%f\n",x)10 #define pl(x) printf("%lld\n",x)11 #define rep(i, n) for(int i=0;i
'9'){
if(e=='-')f=-1;e=getchar();}23 while(e>='0'&&e<='9'){x=x*10+e-'0';e=getchar();}24 return f*x;25 }26 27 const int inf=1128481603;28 int n,m,k;29 struct P{
int v,dis,nxt;}E[50005];30 int head[N],tot;31 int d[2005],vis[2005];//数组开太大会T32 int l=0,r=1e6+10,mid;33 34 void add(int u,int v,int dis)35 {36 E[++tot].nxt=head[u];37 E[tot].v=v; E[tot].dis=dis;38 head[u]=tot;39 }40 41 void SPFA()42 {43 memset(d,67,sizeof(d)); d[1]=0;44 queue
q; q.push(1);45 memset(vis,0,sizeof(vis));46 while(!q.empty())47 {48 int u=q.front();49 q.pop(); vis[u]=0;50 for(int i=head[u];i;i=E[i].nxt)51 {52 int v=E[i].v,dis=(E[i].dis>mid);53 if(d[v]>d[u]+dis)54 {55 d[v]=d[u]+dis;56 if(!vis[v])q.push(v),vis[v]=1;57 }58 }59 }60 }61 62 int main()63 {64 n=R();m=R();k=R();65 for(int i=1;i<=m;++i)66 {67 int u=R(),v=R(),dis=R();68 add(u,v,dis);add(v,u,dis);69 }70 int ans=-1;71 while(l<=r)72 {73 mid=(l+r)>>1;74 SPFA();75 if(d[n]==inf){ printf("-1"); return 0;}76 if(d[n]<=k) ans=mid,r=mid-1;77 else l=mid+1;78 }79 printf("%d",ans);80 return 0;81 }

 

转载于:https://www.cnblogs.com/mj-liylho/p/9502436.html

你可能感兴趣的文章
发布一个TCP 吞吐性能测试小工具
查看>>
java学习:jdbc连接示例
查看>>
PHP执行批量mysql语句
查看>>
Extjs4.1.x 框架搭建 采用Application动态按需加载MVC各模块
查看>>
Silverlight 如何手动打包xap
查看>>
建筑电气暖通给排水协作流程
查看>>
JavaScript面向对象编程深入分析(2)
查看>>
linux 编码转换
查看>>
POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
查看>>
Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月7日-1月14日)
查看>>
关于C#导出 文本文件
查看>>
使用native 查询时,对特殊字符的处理。
查看>>
maclean liu的oracle学习经历--长篇连载
查看>>
ECSHOP调用指定分类的文章列表
查看>>
分享:动态库的链接和链接选项-L,-rpath-link,-rpath
查看>>
Javascript一些小细节
查看>>
禁用ViewState
查看>>
Android图片压缩(质量压缩和尺寸压缩)
查看>>
nilfs (a continuent snapshot file system) used with PostgreSQL
查看>>
【SICP练习】150 练习4.6
查看>>