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

老runtime 哪位大侠帮我看看啊!!!!!谢过!!

Posted by shdj111 at 2010-12-26 16:13:30 on Problem 1287
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int net[200][200],path[200];
struct way
{
	int v1,v2,dis;
};
int cmp(const void* way1,const void* way2)
{
	way*p1,*p2;
	p1=(way*)way1;
	p2=(way*)way2;
		return p1->dis-p2->dis;

}
int getRoot(int a)
{
	while(path[a]>=0)
		a=path[a];
	return a;
}
int main()
{
	int p,r,i,j,length;
	int a,b,tmp;
	way villway[20000];
	while(1)
	{
		memset(path,-1,sizeof(path));
		memset(net,0,sizeof(net));
		length=0;
		scanf("%d",&p);
		if(p==0)break;
		scanf("%d",&r);
		int k=0;
		for(i=0;i<r;i++)
		{
			scanf("%d%d",&a,&b);
			scanf("%d",&tmp);
			a--;b--;
			if((net[a][b]==0&&net[b][a]==0)||(tmp<net[a][b]||tmp<net[b][a]))
			{
				net[a][b]=tmp;net[b][a]=0;
			}
		}
		for(i=0;i<r;i++)
			for(j=0;j<r;j++)
				if(net[i][j]>0)
				{
					villway[k].v1=i,villway[k].v2=j,villway[k].dis=net[i][j];
					k++;
				}
		if(k>0) qsort(villway,k,sizeof(villway[0]),cmp);
		for(i=0;i<k;i++)
		{
			int root1=getRoot(villway[i].v1);
			int root2=getRoot(villway[i].v2);
			if(path[root1]+p==0||path[root2]+p==0)	break;
			if(root1!=root2)
			{
				if(path[root1]<path[root2])
				{
					path[root1]+=path[root2];
					path[root2]=root1;
				}
				else 
				{
					path[root2]+=path[root1];
					path[root1]=root2;
				}
				length+=villway[i].dis;
			}
		}
		printf("%d\n",length);

	}
	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