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

为什么还是tle不停,我已经把队列改成栈了,谁能给组测试数据?(附我的代码)

Posted by xiaohuang_17 at 2009-09-18 20:13:11 on Problem 3159
为什么还是tle不停,我已经把队列改成栈了,谁能给组测试数据?(附我的代码)


#include <iostream>
using namespace std;
#define M 30001
#define inf 999999
int dis[M];
int signal[M];
int n;
int Stack[M];
int top;
struct edge
{
	int u,v,w;
	edge *next;
}*head[M];
void addEdge(int u,int v,int w)
{
	edge *ptr = new edge;
	ptr->v = v;
	ptr->w = w;
	ptr->next = head[u];
	head[u] = ptr;
}
void SPFA(int source)
{
	int i;int temp;
	edge *p;
	for(i=1;i<=n;++i)
	{
		dis[i]=inf;
		signal[i]=false;//标志是否在队列中
	}
	dis[source]=0;
	Stack[top++]=source;
	signal[source]=true;
	while(top!=1)
	{
		temp=Stack[--top];
		signal[temp]=false;
		for(p=head[temp];p;p=p->next)
		{
			if(dis[p->v]>dis[temp]+p->w)
			{
				dis[p->v]=dis[temp]+p->w;
				Stack[top++]=p->v;
				signal[p->v]=true;
			}
		}
	}
}



int main()
{
	int m;int i,a,b,c;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=1;i<=n;++i)
			head[i]=NULL;//初始化
		top=1;
		for(i=0;i<m;++i)
		{
			scanf("%d%d%d",&a,&b,&c);
			addEdge(a,b,c);
		}
		SPFA(1);
		printf("%d\n",dis[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