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+栈得到的是wa(附源码)大牛指点一二,代码易读

Posted by sige at 2009-04-12 09:00:41 on Problem 3159 and last updated at 2009-04-12 09:02:30
#include<iostream>
using namespace std;
struct EDGE
{
	int a,b,c;
	EDGE *next;
}edge[150005];
EDGE *temp;
struct KID
{
	int candy;
	EDGE *first;
};
KID kid[30005];
int n,m,start=0,end;
int spfa()
{
	int stack[30005];
	int i,k,l,top=0;
	EDGE *record[30005];
	for(i=0;i<n;i++)
	{
		record[i]=kid[i].first;
	}
	bool visite[30005];
	memset(visite,false,sizeof(visite));
	kid[0].candy=0;
	stack[top++]=start;
	visite[start]=true;
	while(top>0)
	{
		k=stack[top-1];
		for(temp=record[k];temp!=NULL;temp=temp->next)
		{
			l=temp->b;
			if(kid[k].candy+temp->c<kid[l].candy)
			{
				kid[l].candy=kid[k].candy+temp->c;
				if(visite[l]==false)
				{
					visite[l]=true;
					stack[top++]=l;
					record[k]=temp;
					break;
				}
			}
		}
		if(temp==NULL)
		{
			visite[k]=false;
			top--;
		}
	}
	return kid[end].candy;
}
int main()
{
	int i,a,b,c;
	scanf("%d%d",&n,&m);
	end=n-1;
	for(i=0;i<n;i++)
	{
		kid[i].candy=INT_MAX;
		kid[i].first=NULL;
	}
	for(i=0;i<m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		a--;
		b--;
		edge[i].a=a;
		edge[i].b=b;
		edge[i].c=c;
		edge[i].next=kid[a].first;
		kid[a].first=&edge[i];
	}
	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