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:我也扔2个spfa的,,,一个是队列的,,,一个dfs的,,,

Posted by yy17yy at 2010-12-22 09:25:35 on Problem 3259
In Reply To:终于AC了!!!附代码!如果大家能给意见,在下感激不尽! Posted by:xiangrui at 2010-03-20 01:13:57
之所以写dfs的,,是因为,,有网上说dfs很快,,结果实在太让人失望了,,慢的要死,,,400多ms,,,队列的才32ms,,,,,
队列&&&&&&&&&&&&&&&&&&&&&&
#include <iostream>
using namespace std;
#define N 510
int p[N],d[N],time[N],q[N];
bool vis[N];
int t,n,m,mm;
struct edge
{
	int to,w,next;
}e[N*N];
bool spfa()
{
	memset(time,0,sizeof(time));
	time[1]++;
	int head=-1,tail=0;
	q[tail]=1;
	while(head!=tail)
	{
		int u=q[head=(head+1)%n];
		vis[u]=false;
		for(int x=p[u];x!=-1;x=e[x].next)
		{
			int v=e[x].to,w=e[x].w;
			if(d[v]>d[u]+w)
			{
				if(v==1)
					return true;
				d[v]=d[u]+w;
				if(!vis[v])
				{
					q[tail=(tail+1)%n]=v;
					vis[v]=true;
					time[v]++;
					if(time[v]>n)
						return true;
				}
			}
		}
	}
	return false;
}
int main()
{
	for(cin>>t;t--;)
	{
		scanf("%d%d%d",&n,&m,&mm);
		int i,j=0;
		memset(p,-1,sizeof(p));
		int from,to,w;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&from,&to,&w);
			e[j].to=to;e[j].w=w;e[j].next=p[from];p[from]=j++;
			e[j].to=from;e[j].w=w;e[j].next=p[to];p[to]=j++;
		}
		for(i=1;i<=mm;i++)
		{
			scanf("%d%d%d",&from,&to,&w);
			e[j].to=to;e[j].w=0-w;e[j].next=p[from];p[from]=j++;
		}
		memset(vis,false,sizeof(vis));
		for(i=1;i<=n;i++)
			d[i]=INT_MAX;
		d[1]=0;
		vis[1]=true;
		if(spfa())
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}



dfs,,,就换了一个函数....
bool spfa(int s)
{
	for(int x=p[s];x!=-1;x=e[x].next)
	{
		int v=e[x].to,w=e[x].w;
		if(d[v]>d[s]+w)
		{
			if(vis[v])
				return true;
			vis[v]=true;
			d[v]=d[s]+w;
			if(spfa(v))
				return true;
			vis[v]=false;
		}
	}
	return false;
}

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