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

我这代码超时了,求哪位大牛帮我改进一下,谢谢先

Posted by 372087045 at 2012-09-21 11:09:42 on Problem 1826
#include <iostream>
#include <algorithm>
using namespace std;
#define N 200001

struct P
{
	int x,y,w;
	int flag;
}point[N];

int q[N],sum,n,maxx;

int cmp(P a,P b)
{
	if(a.x==b.x)
		return a.y<b.y;
	else
		return a.x<b.x; 
}

bool Is_link(int a,int b)  
{  
      if(abs(point[a].x-point[b].x)+abs(point[a].y-point[b].y)==1) 
		  return  true;  
      return false;    
}  


void bfs(int x)
{
	int begin,end,i,x1;
	begin=end=0;
	q[end]=x;
	sum += point[x].w;
	end++;
	while (begin<=end)
	{
		x1 = q[begin];
		begin++;
		for (i=0;i<n;i++)
		{
			if(x1==i)
				continue;
			if(point[i].flag==1 && Is_link(x1,i))
			{
				q[end] = i;
				sum += point[i].w;
				point[i].flag = 0;
				end++;
			}
		}
	}
}

int main()
{
	int i;
	while (cin>>n,n)
	{
		for (i=0;i<n;i++)
		{
			scanf("%d %d %d",&point[i].x,&point[i].y,&point[i].w);
			point[i].flag=1;
		}
		sort(point,point+n,cmp);
		maxx = -1;
		for (i=0;i<n;i++)
		{
			sum=0;
			if(point[i].flag==1)
			{
				point[i].flag=0;
				bfs(i);
			}
			if ( maxx < sum )
				maxx = sum;
		}
		cout<<maxx<<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