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

spfa,,,呵呵,,,刚学会的,,

Posted by yy17yy at 2010-12-21 12:27:51 on Problem 1502
#include <iostream>
using namespace std;
#define oo 99999999
#define max(a,b) a>b?a:b
#define N 110
bool vis[N];
int map[N][N];
int q[N];
int d[N];
int n;
int spfa()
{
	memset(vis,false,sizeof(vis));
	int i;
	for(i=1;i<=n;i++)
		d[i]=oo;
	int tail=0,head=-1;
	vis[1]=true;
	q[tail]=1;
	d[1]=0;
	while(head!=tail)
	{
		int u=q[head=(head+1)%n];
		vis[u]=false;
		int j;
		for(j=1;j<=n;j++)
		{
			if(d[j]>d[u]+map[u][j])
			{
				d[j]=d[u]+map[u][j];
				if(!vis[j])
				{
					q[tail=(tail+1)%n]=j;
					vis[j]=true;
				}
			}
		}
	}
	int ans=INT_MIN;
	for(i=1;i<=n;i++)
		ans=max(ans,d[i]);
	return ans;
}
int main()
{
	char s[20];
	int i,j;
	while(scanf("%d",&n)!=-1)
	{
		for(i=2;i<=n;i++)
		{
			for(j=1;j<=i-1;j++)
			{
				scanf("%s",s);
				if(s[0]=='x')
					map[i][j]=map[j][i]=oo;
				else
					map[i][j]=map[j][i]=atoi(s);
			}
		}
		for(i=1;i<=n;i++)
			map[i][i]=0;
		printf("%d\n",spfa());
	}
	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