比赛 |
NOIP水题争霸赛 |
评测结果 |
WAWWWWWWWWWWWWWWWWWW |
题目名称 |
最小差异值 |
最终得分 |
5 |
用户昵称 |
fall in you |
运行时间 |
0.046 s |
代码语言 |
C++ |
内存使用 |
0.32 MiB |
提交时间 |
2018-02-11 21:21:22 |
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 5000 +10
using namespace std;
int n,m,sum;
int first[maxn],next[maxn * 2],father[maxn];
struct E{
int u,v,w;
};
E edge[maxn * 2];
void add(int ,int ,int);
void Kruskal();
int mmp(E,E);
int find(int);
int main()
{
freopen("dvalue.in","r",stdin);
freopen("dvalue.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++) first[i] = -1;
for(int i = 1; i <= m; i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
// add(y,x,z);
}
sort(edge + 1, edge + m,mmp);
Kruskal();
}
inline void Kruskal()
{
int MAX = -2,MIN = 2147483647;
for(int i = 1; i <= n; i++) father[i] = i;
int cnt= 0,tot = 0;
for(int i = 1; i <= m; i++){
int x = find(edge[i].u);
int y = find(edge[i].v);
if(x != y){
father[x] = y;cnt++;
tot += edge[i].w;
MIN = min(MIN,edge[i].w);
MAX = max(MIN,edge[i].w);
}
if(cnt == n-1) {
printf("%d",MAX - MIN);
return;
}
}
}
int find(int x)
{
return x == father[x] ? x : father[x] = find(father[x]);
}
inline int mmp(E a, E b)
{
return a.w < b.w;
}
void add(int x,int y,int z)
{
edge[++sum].u = x,edge[sum].v = y,edge[sum].w = z;
next[sum] = first[edge[sum].u];
first[edge[sum].u] = sum;
}