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

怎么用c++比g++快那么多

Posted by lovelove at 2009-04-07 15:46:16 on Problem 3723 and last updated at 2009-04-07 15:48:13
#include "iostream"
#include "algorithm"
using namespace std;
#define N 100000
int f[N],rank[N];

struct node{
	int u;
	int v;
	int c;
}gr[N];
void makeset(int x)
{
	f[x]=x;
	rank[x]=0;
}
int find(int x)
{
	if(f[x]!=x)
		f[x]=find(f[x]);
	return f[x];
}
void setunion(int a,int b)
{
    int x=find(a);
	int y=find(b);
	if(rank[x]>rank[y])
		f[y]=x;
	else
	{
		f[x]=f[y];
	if(rank[x]==rank[y])
		rank[y]++;
	}
}
bool cmp(node x,node y)
{
	return x.c>y.c;
}

int kruskal(int n,int m)
{
	int i;

	int sum=0;
	for(i=0;i<=n;i++)
		makeset(i);
	sort(gr+1,gr+1+m,cmp);
	for(i=1;i<=m;i++)
	{   
		int c=find(gr[i].u);
		int d=find(gr[i].v);
		if(c!=d)
		{
			sum+=gr[i].c;
			setunion(gr[i].u,gr[i].v);
		//	grs[++num]=gr[i];
		}
	}
		return sum;
}


int main()
{

	 int z,a,b,c,m,n,k,i;
	scanf("%d",&z);
	while(z--)
	{
		scanf("%d%d%d",&n,&m,&k);
		for(i=1;i<=k;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			gr[i].u=a;
			gr[i].v=b+n;
			gr[i].c=c;
		}
		int result=kruskal(n+m,k);
		//printf("%d\n",result);
		printf("%d\n",10000*(n+m)-result);


	}
}



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