P1396 营救
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 营救的更多相关文章
- 洛谷 P1396 营救
题目链接 https://www.luogu.org/problemnew/show/P1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪 ...
- 洛谷——P1396 营救
P1396 营救 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈 ...
- 洛谷P1396 营救 题解
题目:https://www.luogu.org/problemnew/show/P1396 分析: 这其实一看就是一个最短路的近似模板的题目,但我们要注意到两个区之间可能会有多条道路,所以说我们只需 ...
- P1396 营救(并查集+二分)
思路:检验函数中,先初始化每个节点的下标,每调用检验函数就从新使用一次并查集(并查集的时间复杂度非常低),然后,就看当一条路的价值val<=假设最大值x时,就把他们连接起来. #include& ...
- [P1396]营救 (并查集)
大佬都是用最短路做的 我用最小生成树 #include<bits/stdc++.h> #include<algorithm> using namespace std; stru ...
- 洛谷P1396 营救
题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...
- luogu P1396 营救
题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...
- P1396 营救(最小瓶颈路)
题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...
- P1396 营救 洛谷
https://www.luogu.org/problem/show?pid=1396 题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起 ...
随机推荐
- HW2.11
控制台: import java.util.Scanner; public class Solution { public static void main(String[] args) { Scan ...
- java字节流
一. 字节输入流:InputStream(抽象类,所有字节输入流的超类) 1.FileInputStream: 文件输入流 FileInputStream fileIS = new FileIn ...
- java Graphics2D 画图
在Java中,当需要画一些特殊的形状时,比如说椭圆.矩形等,可以使用 Graphics2D 来绘图. 一些API: g.drawLine(3,3,50,50);//画一条线段 g.drawRect(8 ...
- sping获取bean方法 解决资源耗尽
// ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationCon ...
- Mac下eclipse安装SVN插件
eclipse中最常使用的SVN插件是subclipse,先到subclipse官网:http://subclipse.tigris.org下载该插件. 如上图,点击“Download and I ...
- C# 基础知识 protected 关键字
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Console ...
- Palindrome(poj3974)(manacher算法)
http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...
- Swift编程语言的相关资料
苹果官方Swift文档<The Swift Programming Language> 苹果开发人员Swift文档及介绍 网友整理的Swift中文文档<Apple Swift编程语言 ...
- linux下查看本机socket端口详细信息
netstat -paut [root@OA-JRY-SY-FDEP1 nginx-]# netstat -paut Active Internet connections (servers and ...
- oracle数据库导入导出的dmp(转)
window下: imp必须要dba用户,所以用sysdba用户登陆,然后给予chnlmgr用户dba权限 grant connect,resource,dba to chnlmgr; 全部导入 im ...