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

Re:我竟然用宽搜写了一个,*^◎^*

Posted by TSERROF at 2012-12-02 22:37:45 on Problem 1502
In Reply To:我竟然用宽搜写了一个,*^◎^* Posted by:TSERROF at 2012-12-02 22:36:36
这个改进版可以显示路径
struct Point
{
	int point,journey;   
	string trace;
};
int BFS(int startp,int endp)
{
	Point p;
	queue<Point>q;
	p.journey=0,p.point=startp,p.trace.append(to_string(p.point));
	q.push(p);
	int record[10000];
	fill(record,record+vertex+1,0);
	string ans;
	int min=0xffff;
	record[startp]=1;
	while(!q.empty())      
	{
		p=q.front();
		q.pop();
		if(p.point==endp && min>p.journey)
		{
			min=p.journey;
			ans=p.trace;
		}
		for(int i=1;i<=vertex;++i)     //搜索每一个可能和p相邻的顶点
		{
			if(i==p.point )continue;
			if(matrix[i][p.point])                 //如果有边
			{
				if(record[i]!=0 && p.journey+matrix[i][p.point]>record[i])continue;
				record[i]=p.journey+matrix[i][p.point];
				Point temp=p;
				temp.journey=record[i],temp.point=i,temp.trace.append(to_string(i));
				q.push(temp);				
			}
		}
	}
	for(unsigned int i=0;i<ans.size();++i)
	{
		if(i)cout<<"->";
		cout<<ans[i];
	}
	cout<<endl;
	return min;
}

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