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

求大牛看看为何超时

Posted by 13091118 at 2010-08-13 15:48:52 on Problem 3259
#include<iostream>
using namespace std;
int n,m,w,num;
struct node{
	int u,v,t;
}eadge[3000];
int bellman_ford(int num){
	int i,j,dis[501];
	for(i=1;i<=n;i++)
			dis[i]=0x007fffff;
		dis[1]=0;
	bool flod;
	for(i=1;i<n;i++){
		flod=0;
		for(j=0;j<num;j++)
			if(dis[eadge[j].v]>dis[eadge[j].u]+eadge[j].t)
			{
				dis[eadge[j].v]=dis[eadge[j].u]+eadge[j].t;
				flod=1;
			}
			if(flod==0)
				return 1;
	}
	for(j=0;j<num;j++)
		if(dis[eadge[j].v]>dis[eadge[j].u]+eadge[j].t)
			return 0;
		return 1;

}
int main()
{
	int i,a,b,c,j;
	
	cin>>j;
	while(j--)
	{
		num=0;
		
		cin>>n>>m>>w;
		
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			eadge[num].u=a;
			eadge[num].v=b;
			eadge[num++].t=c;
			eadge[num].u=b;
			eadge[num].v=a;
			eadge[num++].t=c;
			}
		for(i=1;i<=w;i++)
		{
			cin>>a>>b>>c;
			eadge[num].u=a;
			eadge[num].v=b;
			eadge[num++].t=-c;
		}
		if(!bellman_ford(num))
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;

	}
	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