比赛 NOIP水题争霸赛 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 最小差异值 最终得分 100
用户昵称 Tony 运行时间 4.004 s
代码语言 C++ 内存使用 1.27 MiB
提交时间 2018-02-11 20:09:30
显示代码纯文本
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#define ll long long
using namespace std;
ll RD(){
	ll flag = 1,out = 0;char c = getchar();
	while(c < '0' || c > '9'){if(c == '-')flag = -1;c = getchar();}
	while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
	return flag * out;
	}
const ll maxn = 5010;
ll num,nr;
ll father[maxn];
struct Node{ll u,v,dis;}I[maxn * 10];
bool cmp(Node a,Node b){return a.dis < b.dis;}
ll findfather(ll v){
	if(father[v] == v)return v;
	else return father[v] = findfather(father[v]);
	}
void Union(ll a,ll b){
	ll faA = findfather(a);
	ll faB = findfather(b);
	if(faA != faB){
		father[faA] = faB;
		}
	}
int main(){
	freopen("dvalue.in","r",stdin);
	freopen("dvalue.out","w",stdout);
	num = RD();
	for(ll i = 1;i <= num;i++)father[i] = i;
	nr = RD();
	for(ll i = 1;i <= nr;i++){
		I[i].u = RD();
		I[i].v = RD();
		I[i].dis = RD();
		}
	sort(I + 1, I + 1 + nr,cmp);
	ll ans = 999999999;
	for(ll i = 1;i <= nr - num + 2;i++){
		ll cnt = 0;
		for(ll j = 1;j <= num;j++)father[j] = j;
		
		for(ll j = i;j <= nr;j++){
			if(findfather(I[j].u) != findfather(I[j].v)){
				Union(I[j].u,I[j].v);
				cnt++;
				}
			if(cnt == num - 1){
				ans = min(ans,I[j].dis - I[i].dis);
				//cout<<ans<<endl;
				break;
				}
			}
		}
	cout<<ans<<endl;
	fclose(stdin);
	fclose(stdout);
	return 0;
	}