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. Docker系列(三)常用命令

    命令说明 docker pull 格式: docke pull [OPTIONS] NAME[:TAG] 作用:下载名称为 name 的镜像 例子: sudo docker pull dl.docke ...

  2. IntelliJ 直接编辑国际化文件(properties)方法

    IntelliJ 直接编辑国际化文件(properties)方法 settings-File Encodings 右下角 Transparent native-to-ascii conversion的 ...

  3. POJ-1151 Atlantis 矩形面积并

    题目链接:http://poj.org/problem?id=1151 扫描线+离散+线段树,线段树每个节点保存的是离散后节点右边的线段. //STATUS:C++_AC_16MS_208KB #in ...

  4. Weblogic 集群部署说明 --转

    代理web.xml 设置 <servlet> l                <servlet-name>HttpClusterServlet</servlet-nam ...

  5. 2015 CCPC D- Pick The Sticks(UESTC 1218) (01背包变形)

    http://acm.uestc.edu.cn/#/problem/show/1218 既然二维dp表示不了,就加一维表示是否在边界放置,放置一个,两个.有一个trick就是如果只放一根,那么多长都可 ...

  6. [struts2]开启struts2开发模式

    <constant name="struts.devMode" value="true" />

  7. Oracle学习过程(随时更新)

    1.入门 实用的一些查询语句: 查询用户所有表注释 select * from user_tab_comments 条件查询 根据两个值查询 select*from table where 字段 in ...

  8. ros和Android(一)

    ros和Android :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { ...

  9. 数据库数据导入导出系列之五 C#实现动态生成Word(转)

    1. 一个控制台例子,实现动态生成Word. 首先,添加引用:COM->Microsoft Word 11.0 Object Library. 2. 介绍几篇牛人写的关于操作Word的文章 [分 ...

  10. Linux crontab 命令格式与具体样例

    基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...