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 无数次了 ,该死的题目描述。。。哎,你这组数据也过了。。怎么我的还是wa了,大神帮我看看我的程序

Posted by taaini8691 at 2013-09-07 17:10:00 on Problem 1984
In Reply To:WA的人:要按输入顺序回答 Posted by:drcrow at 2010-12-25 11:18:51
/*未AC*/

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;

struct Node{   
	int x,y;
	int parent;
};
Node node[40010];
struct Node1{     //记录输入的数据
	int x,y;
	int dis;
	char dir;
};
Node1 node1[40010];
struct Node2{       //记录查询的数据
	int x,y;
	int index;
	int flag;
};
Node2 node2[10010];
int m,n,k;
int ans[10010];
bool cmp(Node2 a,Node2 b)
{
	return a.index<b.index;
}
void init()
{
	int i;
	for (i=0;i<=n;i++)
	{
		node[i].x=0;
		node[i].y=0;
		node[i].parent=-1;
	}
}
int Find(int s)
{
	//	printf("%d",node[s].parent);
	int x;
	for ( x=s;node[x].parent>=0;x=node[x].parent);
	if (node[s].parent>=0)
	{
		int temp=node[s].parent;
		node[s].parent=Find(node[s].parent);
		node[s].x=(node[s].x+node[temp].x);
		node[s].y=(node[s].y+node[temp].y);
	}
	return x;
}
void Merge(int a,int b,int aa,int bb,int x,int y)
{
	int temp=node[aa].parent+node[bb].parent;
	if (node[aa].parent<node[bb].parent)
	{
		int xx=node[b].x+x;
		int yy=node[b].y+y;
		node[aa].x=xx-node[a].x;
		node[aa].y=yy-node[a].y;
		node[aa].parent=bb;
		node[bb].parent=temp;
		
	}
	else{
		node[bb].x=(node[a].x-node[b].x)+x;
	    node[bb].y=(node[a].y-node[b].y)+y;
	//	printf("%d%d%d%d\n",node[a].x,x,node[b].x,node[bb].x);
		node[bb].parent=aa;
		node[aa].parent=temp;
		
	}
}
int main()
{
	scanf("%d%d",&n,&m);
		int i,j;
		int x,y,s;
		char d;
		for (i=1;i<=m;i++)
		{
			scanf("%d%d%d",&x,&y,&s);
			getchar();
			scanf("%c",&d);
			node1[i].x=x;node1[i].y=y;node1[i].dis=s;node1[i].dir=d;
		}
		
		scanf("%d",&k);
		for (j=1;j<=k;j++)
		{
			scanf("%d%d%d",&x,&y,&s);
			node2[j].x=x;node2[j].y=y;node2[j].index=s;
			node2[j].flag=j;
	//		printf("adsf");
		}
		int flag=1;
		int a,b;
		init();
		sort(node2+1,node2+1+k,cmp);
// 		for (i=1;i<=k;i++)
// 		{
// 			printf("%d",node2[i].index);
// 		}
		for (i=1;i<=m;i++)
		{
			if (node1[i].dir=='E')
			{
				a=node1[i].dis;
				b=0;
			}
			else if (node1[i].dir=='W')
			{
				a=-node1[i].dis;
				b=0;
			}
			else if(node1[i].dir=='N')
			{
				a=0;
				b=node1[i].dis;
			}
			else
			{
				a=0;
				b=-node1[i].dis;
			}
			int xx=Find(node1[i].x);
			int yy=Find(node1[i].y);
		//	printf("\n%d%d\n",xx,yy);
			Merge(node1[i].x,node1[i].y,xx,yy,a,b);
			if (node2[flag].index==i)
			do{
					int xx1=Find(node2[flag].x);
					int yy1=Find(node2[flag].y);
				//	printf("%d%d",xx1,yy1);
					if (xx1!=yy1)
					{
						ans[node2[flag].flag]=-1;
					//	printf("adsf\n");
					}
					else
					{
						ans[node2[flag].flag]=abs(node[node2[flag].x].x-node[node2[flag].y].x)+abs(node[node2[flag].x].y-node[node2[flag].y].y);
					//	printf("adsf\n");
					//	printf("%d%d%d%d",node[1].x,node[1].y,node[6].x,node[6].y);
					}
					flag++;
			}while(node2[flag].index==node2[flag-1].index);
			
			if (flag==k+2)
			{
				break;
			}
		}
		for (i=1;i<=k;i++)
		{
			printf("%d\n",ans[i]);
		}

	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