Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我的也63ms^_^

Posted by nazgul1991 at 2010-04-06 16:07:28 on Problem 2395
In Reply To:用了63ms,希望能抛砖引玉 Posted by:suikay at 2008-12-20 17:45:20
#include <stdio.h>
#include <algorithm>

struct edge
{
	int d, u, v;
}e[10010];
int n, m, r[2005];
int t, i, j, p, maxroad;

int cmp( const void *a, const void *b )
{
	return *(int *)a-*(int *)b;
}

int Top( int x )
{
	int t = x;
	while ( r[x]!=x )
		x = r[x];
	return r[t] = x;
}

int Union( int x, int y )
{
	int t1, t2;
	t1 = Top( x ); t2 = Top( y );
	if ( t1==t2 ) return 0;
	r[t1] = t2;
	return 1;
}

int main()
{
	while ( scanf( "%d%d", &n, &m )!=EOF )
	{
		for ( i=0; i<m; ++i )
			scanf( "%d%d%d", &e[i].u, &e[i].v, &e[i].d );
		qsort( e, m, sizeof( e[0] ), cmp );
		for ( i=0; i<=n; ++i )
			r[i] = i;
		i = 0; p = 0;
		while ( i<m && p<n-1 )
		{
			if ( Union( e[i].u, e[i].v ) )
				maxroad = e[i].d;
			++i;
		}
		printf( "%d\n", maxroad );
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator