P1396 营救

218

通过

571

提交

题目提供者yeszy

标签 二分 图论 并查集 福建省历届夏令营

难度 普及-

题目描述

“咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门……

妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小明被带到了t区,而自己在s区

该市有m条大道连接n个区,一条大道将两个区相连接,每个大道有一个拥挤度。小明的妈妈虽然很着急,但是不愿意拥挤的人潮冲乱了她优雅的步伐。所以请你帮她规划一条从s至t的路线,使得经过道路的拥挤度最大值最小。

输入输出格式

输入格式:

第一行四个数字n,m,s,t。

接下来m行,每行三个数字,分别表示两个区和拥挤度。

(有可能两个区之间有多条大道相连。)

输出格式:

输出题目要求的拥挤度。

输入输出样例

输入样例#1:

3 3 1 3

1 2 2

2 3 1

1 3 3

输出样例#1:

2

说明

数据范围

30% n<=10

60% n<=100

100% n<=10000,m<=2n,拥挤度<=10000

题目保证1<=s,t<=n且s<>t,保证可以从s区出发到t区。

样例解释:

小明的妈妈要从1号点去3号点,最优路线为1->2->3。

/*
MST最小瓶颈生成树.
定理:MST中的最长边必定小于其他生成树中的最长边.
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#define MAXN 10001
using namespace std;
int n,m,father[MAXN],tot,ss,tt;
struct data
{
int x,y,z;
}s[MAXN*2];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
bool cmp(const data &x,const data &y)
{
return x.z<y.z;
}
int find(int x)
{
return x!=father[x]? father[x]=find(father[x]):x;
}
int main()
{
n=read();m=read();ss=read();tt=read();
for(int i=1;i<=n;i++) father[i]=i;
for(int i=1;i<=m;i++)
s[i].x=read(),s[i].y=read(),s[i].z=read();
sort(s+1,s+m+1,cmp);
for(int i=1;i<=m;i++)
{
int l1=find(s[i].x),l2=find(s[i].y);
if(l1!=l2) tot++,father[l2]=l1;
if(find(ss)==find(tt))
{
printf("%d",s[i].z);
return 0;
}
if(tot==n-1) break;
}
return 0;
}
/*
二分拥挤值.
spfa大于拥挤值的边不选.
然后判能不能到达终点.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 10001
using namespace std;
int n,m,s,t,head[MAXN],tot,dis[MAXN];
bool b[MAXN];
struct data
{
int v,next,x;
}e[MAXN*4];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
void add(int u,int v,int x)
{
e[++tot].v=v;
e[tot].x=x;
e[tot].next=head[u];
head[u]=tot;
}
bool spfa(int x)
{
int q[MAXN*2]={0},head1=1,tail=0;
memset(b,0,sizeof(b));
memset(dis,127/3,sizeof(dis));dis[s]=0;q[++tail]=s;b[s]=true;
while(head1<=tail)
{
int u=q[head1++];b[u]=false;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(!b[v]&&e[i].x<=x&&dis[v]>dis[u]+e[i].x)
{
b[v]=true;
dis[v]=dis[u]+e[i].x;
q[++tail]=v;
}
}
}
if(dis[t]==dis[0]) return false;
return true;
}
int erfen(int l,int r)
{
int mid,ans=0;
while(l<=r)
{
mid=(l+r)>>1;
if(spfa(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
int main()
{
int x,y,z;
n=read();m=read();s=read();t=read();
for(int i=1;i<=m;i++)
{
x=read();y=read();z=read();
add(x,y,z);add(y,x,z);
}
printf("%d",erfen(1,10000));
return 0;
}

P1396 营救的更多相关文章

  1. 洛谷 P1396 营救

    题目链接 https://www.luogu.org/problemnew/show/P1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪 ...

  2. 洛谷——P1396 营救

    P1396 营救 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈 ...

  3. 洛谷P1396 营救 题解

    题目:https://www.luogu.org/problemnew/show/P1396 分析: 这其实一看就是一个最短路的近似模板的题目,但我们要注意到两个区之间可能会有多条道路,所以说我们只需 ...

  4. P1396 营救(并查集+二分)

    思路:检验函数中,先初始化每个节点的下标,每调用检验函数就从新使用一次并查集(并查集的时间复杂度非常低),然后,就看当一条路的价值val<=假设最大值x时,就把他们连接起来. #include& ...

  5. [P1396]营救 (并查集)

    大佬都是用最短路做的 我用最小生成树 #include<bits/stdc++.h> #include<algorithm> using namespace std; stru ...

  6. 洛谷P1396 营救

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  7. luogu P1396 营救

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  8. P1396 营救(最小瓶颈路)

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  9. P1396 营救 洛谷

    https://www.luogu.org/problem/show?pid=1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起 ...

随机推荐

  1. uva_12535 - Probability Through Experiments

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. Select the best path in a matrix

    Amazon interview question: Given a 2-dimensional array with arbitrary sizes and contains random posi ...

  3. 北京Uber优步司机奖励政策(2月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 【Mysql学习笔记】浅析mysql的binlog

    最近读一份关于“数据库事务故障恢复"的技术资料,发现对mysql的binlog的认识不够清楚,查阅mysql reference manual有所收获,作为笔记,记录于此. 1. What' ...

  5. EF搜索数据自动将表名变复数问题

    原因这个是自己生成的需要在model加Table 其他博主写了aweier2011

  6. 在Visual Studio中使用Pseudovariables来帮助调试

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:在Visual Studio中使用Pseudovariables来帮助调试.

  7. Oracle--SQL Developer创建连接及使用

    安装好Oracle之后,有几种方式可以来管理Oracle中的数据库,首先就是登陆网页版的界面:https://localhost:1158/em,这种方式管理的东西太多,使用起来有点不方便,第二种方式 ...

  8. 遍历map的四方方法

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

  9. Android软键盘调用及隐藏,以及获得点击软键盘输入的字母信息

    在Android提供的EditText中单击的时候,会自动的弹出软键盘,其实对于软键盘的控制我们可以通过InputMethodManager这个类来实现.我们需要控制软键盘的方式就是两种一个是像Edi ...

  10. System Address Map Initialization in x86/x64 Architecture Part 2: PCI Express-Based Systems

      原文  http://resources.infosecinstitute.com/system-address-map-initialization-x86x64-architecture-pa ...