POJ 3662 (二分+SPFA
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 {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.
Input
* Line 1: Three space-separated integers: N, P, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, 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
#include"bits/stdc++.h"
#define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
int R()
{
int f=,x=;
char e=getchar();
while(e<''||e>''){if(e=='-')f=-;e=getchar();}
while(e>=''&&e<=''){x=x*+e-'';e=getchar();}
return f*x;
} const int inf=;
int n,m,k;
struct P{int v,dis,nxt;}E[];
int head[N],tot;
int d[],vis[];//数组开太大会T
int l=,r=1e6+,mid; void add(int u,int v,int dis)
{
E[++tot].nxt=head[u];
E[tot].v=v; E[tot].dis=dis;
head[u]=tot;
} void SPFA()
{
memset(d,,sizeof(d)); d[]=;
queue<int> q; q.push();
memset(vis,,sizeof(vis));
while(!q.empty())
{
int u=q.front();
q.pop(); vis[u]=;
for(int i=head[u];i;i=E[i].nxt)
{
int v=E[i].v,dis=(E[i].dis>mid);
if(d[v]>d[u]+dis)
{
d[v]=d[u]+dis;
if(!vis[v])q.push(v),vis[v]=;
}
}
}
} int main()
{
n=R();m=R();k=R();
for(int i=;i<=m;++i)
{
int u=R(),v=R(),dis=R();
add(u,v,dis);add(v,u,dis);
}
int ans=-;
while(l<=r)
{
mid=(l+r)>>;
SPFA();
if(d[n]==inf){ printf("-1"); return ;}
if(d[n]<=k) ans=mid,r=mid-;
else l=mid+;
}
printf("%d",ans);
return ;
}
POJ 3662 (二分+SPFA的更多相关文章
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- poj 3621 二分+spfa
题意:给出一个有向图,问求一个回路,使得回路上的点权之和/边权之和最大. 这题主要是分析出如何确定ans值.我们将(a1*x1+a2*x2+..+an*xn)/(b1*x1+b2*x2+..+bn*x ...
- POJ 3662 二分+Dijkstra
题意: 思路: 二分+Disjktra 二分一个值 如果某条边的边权比它小,则连上边权为0的边,否则连上边权为1的边 最后的d[n]就是最小要免费连接多少电话线. //By SiriusRen #in ...
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- UVALive 4223 Trucking 二分+spfa
Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- POJ - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- 洛谷P1462 通往奥格瑞玛的道路(二分+spfa,二分+Dijkstra)
洛谷P1462 通往奥格瑞玛的道路 二分费用. 用血量花费建图,用单源最短路判断 \(1\) 到 \(n\) 的最短路花费是否小于 \(b\) .二分时需要不断记录合法的 \(mid\) 值. 这里建 ...
- POJ 3662 Telephone Lines(二分答案+SPFA)
[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...
随机推荐
- IsBackground对线程的重要作用
要点: 1.当在主线程中创建了一个线程,那么该线程的IsBackground默认是设置为FALSE的. 2.当主线程退出的时候,IsBackground=FALSE的线程还会继续执行下去,直到线程执行 ...
- textarea存起来的数据把空格也存起来
textarea的属性wrap="hard"可以把换行的内容也存起来. <html> <head> <title>这是一个小测试</tit ...
- js屏蔽鼠标操作
document.body.onselectstart=document.body.oncontextmenu=function(){ return false;}
- 安装Android模拟器Genymotion【Android学习入门】
安装Android模拟器Genymotion 推荐教程:一个强大的Android模拟器Genymotion具体内容如下: 相信很多Android开发者一定受够了速度慢.体验差效率及其地下的官方模拟器了 ...
- sqlalchemy使用tip
https://docs.sqlalchemy.org/en/latest/orm/tutorial.html http://docs.sqlalchemy.org/en/latest/core/sq ...
- TP5.1 配置的获取与设置
我们现在学习对配置文件的获取(Config::get)与设置(Config::set) 我们将学会: (1)获取到一级配置文件 (2)获取到二级配置文件 (3)设置二级配置文件 1.获取一级配置文件 ...
- 将TIMESTAMP类型的差值转化为秒的方法
两个TIMESTAMP之差得到的是INTERVAL类型,而有时我们只需要得到两个时间相差的秒数,如果变成INTERVAL之后,想要获取这个值会非常麻烦. 比较常见的方法是使用EXTRACT来抽取获得的 ...
- Poj(2488),按照字典序深搜
题目链接:http://poj.org/problem?id=2488 思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序. 后来,凡哥说可以在搜索路径的时候就按照字 ...
- 种类并查集,TOJ(1706)
题目链接:http://acm.tju.edu.cn/toj/showp1706.html 很类似Poj的一道帮派的问题,记得找到的可疑的关系,不要将集合刷新就可以了. 1706. A Bug's ...
- CentOS 6\7修改主机名
1.CentOS6修改主机名 1)临时修改主机名: 显示主机名: oracle@localhost:~$ hostname localhost 修改 oracle@localhost:~$ sudo ...