您的位置:首页 > 其它

ZOJ 1914 Arctic Network【kruskal】

2011-08-08 13:20 197 查看
//2619230 	2011-08-08 13:18:12 	Accepted 	1914 	C++ 	130 	4720 	ylwh!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define N 505
#define MAX 200000000
struct coordinates
{
int x, y;
}l[505];

struct edge
{
int x, y;
double len;
int flag;
}e[130000];

double map

, d
;
int pre
;
int cmp(struct edge a, struct edge b)
{
return a.len < b.len;
}

double distence(double x, double y)
{
return (x*x+y*y);
}
int find_set(int x)
{
while(x != pre[x])
x = pre[x];
return x;
}
int main()
{
int n, s, p, x, y, i, j, k;
scanf("%d", &n);
while(n--)
{

scanf("%d%d", &s, &p);
for(i=1; i<=p; i++)
pre[i] = i;
for(i=1; i<=p; i++)
scanf("%d%d", &l[i].x, &l[i].y);
k = 0;
for(i=1; i<p; i++)
for(j=i+1; j<=p; j++)
{
e[k].x = i;
e[k].y = j;
e[k].len = distence(l[i].x - l[j].x, l[i].y - l[j].y);
e[k].flag = 0;
k++;
}
sort(e, e+k, cmp);
for(i=0; i<k; i++)
{
x = find_set(e[i].x);
y = find_set(e[i].y);
if(x != y)
{
pre[y] = x;
e[i].flag = 1;
}
}
j = 0;
for(i=0; i<k; i++)//刚开始这里又对flag拍了一次序,结果超时 = = !
{
if(e[i].flag)
j++;
if( j == p - s )
{
printf("%.2lf\n", sqrt(e[i].len));
break;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  network struct ie c