P1396 营救
题目描述
妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小明被带到了 t 区,而自己在 s 区。该市有 m 条大道连接 n 个区,一条大道将两个区相连接,每个大道有一个拥挤度。小明的妈妈虽然很着急,但是不愿意拥挤的人潮冲乱了她优雅的步伐。所以请你帮她规划一条从 s 至 t 的路线,使得经过道路的拥挤度最大值最小。
输入格式
第一行有四个用空格隔开的 n,m,s,t,其含义见【题目描述】。
接下来 m 行,每行三个整数 u, v, w表示有一条大道连接区 u 和区 v,且拥挤度为 w。两个区之间可能存在多条大道。
输出格式
输出一行一个整数,代表最大的拥挤度。
输入案例
3 3 1 3
1 2 2
2 3 1
1 3 3
输出案例
2
题目大意
输入n个区m个道,s起点,t终点
m条路起点,终点,权值
算法分析
最短路的问题使用kruskal最小生成树来进行边的遍历每次找的都是最小权的边,只要最小权的边相连能使起点到达终点.此时链接的拥挤度的最大值最小.
代码实现
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<math.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long int ll;
const int maxn=1e6+5;
typedef unsigned long long int ull;
ull p=131;
ll mod=1e9+7;
int rt[maxn];
int n;
struct node
{
int t,w,qz;
}edge[maxn];
bool cmp(const node &a,const node &b)//排序
{
return a.qz<b.qz;
}
void inti(int n)
{
for(int i=0;i<n;i++)
{
rt[i]=i;
}
}
int fdroot(int x)
{
if(rt[x]==x)
return x;
else
return rt[x]=fdroot(rt[x]);
}
int join(int x,int y)
{
int rx=fdroot(x);
int ry=fdroot(y);
if(rx!=ry)
{
rt[rx]=ry;
}
}
int main()
{
int n,m,s,t;
cin>>n>>m>>s>>t;
int u,v,w;
for(int i=0;i<m;i++)
{
cin>>u>>v>>w;
edge[i].t=u,edge[i].w=v,edge[i].qz=w;
}
inti(n);
sort(edge,edge+m,cmp);//权值排序
int k=0,sum=0;
for(int i=0;i<m;i++)
{
if(k==n-1) break;
if(fdroot(edge[i].t)!=fdroot(edge[i].w))
{
join(edge[i].t,edge[i].w);
sum=max(sum,edge[i].qz);//这里dp,只用存储最大值.
k++;
}
if(fdroot(s)==fdroot(t))//此时边由小到大排序,只要能连接就能保证最大值最小
{
cout<<sum<<endl;
return 0;
}
}
}