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

TLE的不行了,各位大侠帮忙看看代码吧

Posted by yzhw9981 at 2009-04-07 17:07:17 on Problem 3723
# include <iostream>
# include <algorithm>
# include <vector>
using namespace std;
struct point
{
	int start;
	int end;
	int len;
}data[50001];
int n,m,r,num;
int set[20001];
long long total;
int cmppoint(const void *a,const void *b)
{
	point *aa=(point *)a;
	point *bb=(point *)b;
	return aa->len-bb->len;
}

int det(int num)
{
	int res=num;
	while(set[res]!=-1) res=set[res];
	if(res!=num) set[num]=res;
	return res;
}
void kus()
{
	for(int i=1;i<=r&&num<m+n;i++)
	{
		point temp=data[i];
		int s=det(temp.start),e=det(temp.end);
		if(s==e) continue;
		num++;
		set[s]=e;
		total+=temp.len;
	}
}	
int main()
{
	int test;
	cin>>test;
	for(int testcount=1;testcount<=test;testcount++)
	{
		cin>>n>>m>>r;
		for(int i=0;i<m+n;i++) set[i]=-1;//初始化并查表
		total=0;
		num=0;
		for(int i=1;i<=r;i++)
		{
			cin>>data[i].start>>data[i].end>>data[i].len;
			data[i].end=m+data[i].end;
			data[i].len=10000-data[i].len;
		}
		qsort(data+1,r,sizeof(point),cmppoint);
		kus();
		for(int i=0;i<m+n&&num<n+m;i++)
		{
			if(det(i)==i) 
			{
				total+=10000;
				num++;
			}
		}
		printf("%I64d\n",total);
	}
	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