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

run time error的同学请改为cin输入!用scanf和getchar()容易run time error,0MS ac代码附上

Posted by su03121231 at 2013-07-28 19:04:44 on Problem 1251
代码**********************
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
typedef struct
{
	int u;
	int v;
	int w;
}init;
int p[30];
void set()
{
	for(int i=0;i<30;i++)
		p[i]=i;
}
int find(int x)
{
	return p[x]==x?x:p[x]=find(p[x]);
}
int cmp(const void *a,const void *b)
{
	init *c=(init *)a;
	init *d=(init *)b;
	return c->w - d->w;
}
int main()
{
	int n,i,m,len,j;
	char ch,ch_1;
	init map[100];
	while(cin>>n,n)
	{
		int t=0;
		int sum=0;
		memset(map,0,sizeof(map));
		set();
		for(i=0;i<n-1;i++)
		{
			getchar();
			cin>>ch;
			cin>>m;
			while(m--)
			{
				map[t].u=ch-'A';
				cin>>ch_1;
				cin>>len;
				map[t].v=ch_1-'A';
				map[t].w=len;
				t++;
			}
		}
		qsort(map,100,sizeof(map[0]),cmp);
		for(i=0;!map[i].w;i++);
		for(j=0;j<t;j++,i++)
		{
			int x=find(map[i].u);
			int y=find(map[i].v);
			if(x!=y)
			{
				sum+=map[i].w;
				p[x]=y;
			}
		}
		cout<<sum<<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