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

求大佬看看我的代码。。数据都过了 但就是WA

Posted by wzazzy at 2018-05-22 20:46:55 on Problem 1251
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1000;
int u[maxn],v[maxn],w[maxn],p[maxn],r[maxn];

int cmp(const int i,int j)
{
	return w[i]<w[j];
}

int find(int x)
{
	return p[x]==x ? x:p[x]=find(p[x]);
}

int kruskal(int m)
{
	int ans=0;
	for(int i=0;i<m;i++) p[i]=i;
	for(int i=0;i<m;i++) r[i]=i;
	sort(r,r+m,cmp);
	for(int i=0;i<m;i++)
	{
		int e=r[i];
		int x=find(u[e]);
		int y=find(v[e]);
		if(x!=y)
		{
			ans+=w[e];
			p[x]=y;
		}
	}
	return ans;
}
int main()
{
	int n,m=0,cnt1=0,cnt2=0,j; char a,b;
	while(cin>>n&&n!=0)
	{
		memset(u,0,sizeof(u));
		memset(v,0,sizeof(v));
		memset(w,0,sizeof(w));
		memset(p,0,sizeof(p));
		memset(r,0,sizeof(r));
		for(int i=0;i<n-1;i++)
		{
			cin>>a;
			cin>>j;
			if(j>0)
			{
				u[cnt1++]=a-'A'+1;
				for(int vv=1;vv<j;vv++) u[cnt1++]=a-'A'+1;
			    for(int k=0;k<j;k++)
			    {
				cin>>b; v[cnt2++]=b-'A'+1;
				cin>>w[m++];
			    }
			}
		}
		cout<<kruskal(m)<<endl; 
	}
	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