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 TSERROF at 2012-12-02 22:36:36 on Problem 1502
#include <iostream>
#include <string>
#include <queue>
#include <cstring>
#include <cstdlib>
using namespace std;
#define MAXN 102
#define MAX 0xffff
int matrix[MAXN][MAXN], vertex;
int dp[MAXN][MAXN];
int BFS(int startp,int endp)
{
	int p=startp;
	queue<int>q;
	q.push(p);
	memset(dp,0,sizeof(dp));
	while(!q.empty())      
	{
		p=q.front();
		q.pop();
		for(int i=1;i<=vertex;++i)     //搜索每一个可能和p相邻的顶点
		{
			if(i==p )continue;
			if(matrix[i][p])                 //如果有边
			{
				if(dp[startp][i]==0)      //如果是第一次走过来
				{
					dp[startp][i]=dp[startp][p]+matrix[p][i];
					q.push(i);
				}
				else if(dp[startp][i]>dp[startp][p]+matrix[p][i]) //如果已经来过了,检查哪次路径短
				{ 
					dp[startp][i]=dp[startp][p]+matrix[p][i]; 
					q.push(i);   //这时候应该更新状态,不要急着往下走,从i点再走一次,始终保持子路线最短
				}
			}
		}
	}
	return dp[startp][endp];
}
int main()
{
	char s[100];
	while(cin>>vertex)
	{
		for(int i=1;i<=vertex;++i)
		{
			for(int j=1;j<=i;++j)
			{
				if(i==j)matrix[i][j]=0;
				else
				{
					cin>>s;
					if(!strcmp(s,"x"))matrix[i][j]=matrix[j][i]=MAX;
					else matrix[j][i]=matrix[i][j]=atoi(s);
				}
			}
		}
		int ans=0;
		for(int i=2;i<=vertex;++i)
		{
			int t=BFS(1,i);
			if(ans<t)ans=t;
		}
		cout<<ans<<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