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

晕,BFS超了,不知道是卡STL,还是没优化,G++过,C++超,附Code

Posted by dynamic_study at 2009-09-11 08:54:43 on Problem 2248 and last updated at 2009-09-11 09:19:15
#include<iostream>
#include<queue>
#include<stdio.h>
#include<iostream>
using namespace std;
#define MAXN 100001
struct node
{
	int data;
	int pre;
	int no;
}road[MAXN];
int ans[101],n;
void bfs(int n)
{
	queue<node>temp;
	node mid2;
	road[0].data=1;
	road[0].pre=-1;
	road[0].no=0;
	int k=1;
	bool flag=false;
	temp.push(road[0]);
	while(!temp.empty()&&!flag)
	{
		mid2=temp.front();
		temp.pop();
		if(mid2.data==n)
		{   
            next:
			int end=mid2.no,t=0;
			while(end!=-1)
			{
				ans[t++]=road[end].data;
				end=road[end].pre;
			}
			for(int i=t-1;i>=0;i--)
				printf("%d ",ans[i]);
			printf("\n");
			flag=true;
			return;
		}	
		int end=mid2.no;
		while(end!=-1&&!flag)
		{
			if(mid2.data+road[end].data<=n)
			{
				road[k].data=mid2.data+road[end].data;
				road[k].pre=mid2.no;
				road[k].no=k;
				temp.push(road[k]);
				k++;
			}
			if(road[k-1].data==n)
			{
				mid2=road[k-1];
				goto next;
			}
			end=road[end].pre;
		}
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF&&n)
	{
		bfs(n);
	}
	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