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 AC过,刚好1500ms

Posted by liuzhi67 at 2010-11-27 20:54:22 on Problem 3159
一个代码刷了三次第一次1500ms,第二次1454ms,第三次1469ms
#include <iostream>
#define Vex_Size 30005
using namespace std;
struct Edge
{
	long e,c;
	Edge *next;
};
Edge edge[Vex_Size];
bool visited[Vex_Size];
long d[Vex_Size],size[Vex_Size];
long SPFA(long n)
{
	long tq;
	Edge *te;
	long stack[30005],top=0;
	memset(visited,0,sizeof(visited));
	memset(d,0x3f,sizeof(d));
	d[1]=0;
	stack[top++]=1,visited[1]=1;
	while(top!=0)
	{
		tq=stack[--top];
		te=edge[tq].next;
		while(te)
		{
			if(d[tq]+te->c<d[te->e])
			{
				d[te->e]=d[tq]+te->c;
				if(visited[te->e]==0)
				{
					stack[top++]=te->e;
					visited[te->e]=1;
				}
			}
			te=te->next;
		}
		visited[tq]=0;
	}
	return d[n];
}
int main()
{
    long n,m,i,s,e,c;
    Edge *te;
    //freopen("in.txt","r",stdin);
    scanf("%ld%ld",&n,&m);
    for(i=1;i<=n;i++)	edge[i].next=NULL;
    for(i=0;i<m;i++)
    {
    	scanf("%ld%ld%ld",&s,&e,&c);
    	te=new Edge;
    	te->e=e,te->c=c;			//初值
    	te->next=edge[s].next;
    	edge[s].next=te;
    }
    printf("%ld\n",SPFA(n));
    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