奆佬求助 WA on #9 #10
  • 板块P1396 营救
  • 楼主_sh1kong_
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/1/1 20:38
  • 上次更新2023/10/24 05:52:18
查看原帖
奆佬求助 WA on #9 #10
823773
_sh1kong_楼主2023/1/1 20:38

悬赏关注

#include <iostream>
#include <string.h>
#include <cmath>
#include <algorithm>

const int N = 2010;
const int M = 51010;

using namespace std;//barricades

int n,m,s,t;
int fa[N];

struct node{
	int u,v,w;
}k[M];

bool compare(node x,node y)
{
	if(x.w<y.w) return true;
	else return false;
}

int find(int x)
{
	if(x!=fa[x]) fa[x]=find(fa[x]);
	return fa[x];
}

int main()

{
	cin >> n >> m >> s >> t;
	for (int i=1;i<=m;i++) fa[i]=i;
	for (int i=1;i<=m;i++)
	{
		int u,v,w;
		cin >> u >> v >> w;
		k[i].u=u;k[i].v=v;k[i].w=w;
	}
	sort(k+1,k+m+1,compare);
	for (int i=1;i<=m;i++)
	{
		int a=find(k[i].u),b=find(k[i].v);
		if(a!=b)
		{
			fa[b]=a;
			if(find(s)==find(t))
			{
				cout << k[i].w << endl;
				return 0;
			}
		}
	}
}
2023/1/1 20:38
加载中...