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:这题WA了我16次,问题居然是我把N,M,W跟F一起读了,导致所有N,M,W都一样,郁闷死了,附邻接矩阵+邻接表Bellman_ford

Posted by Belldandy at 2011-10-26 17:18:20 on Problem 3259
In Reply To:这题WA了我16次,问题居然是我把N,M,W跟F一起读了,导致所有N,M,W都一样,郁闷死了,附邻接矩阵+邻接表Bellman_ford Posted by:Belldandy at 2011-10-26 17:16:23
邻接矩阵:
Source Code

Problem: 3259		User: Belldandy
Memory: 1300K		Time: 860MS
Language: C		Result: Accepted
Source Code
#include<stdio.h>
#define INF 999999
int farm[600][600]={0};
int bellman(int n)
{
	int i=0,j=0,k=0,check[600],flag=0;
	for(i=1;i<=n;i++)
	{
		check[i]=INF;
	}
	check[1]=0;
	for(i=0;i<=n;i++)
	{
		flag=0;
		for(j=1;j<=n;j++)
		{
			for(k=1;k<=n;k++)
			{
				if(farm[j][k]+check[j]<check[k])
				{
					check[k]=check[j]+farm[j][k];
					flag=1;
				}
			}
		}
		if(!flag)
		{
			return 0;
		}
		if(i==n)
		{
			return 1;
		}
	}
}
int main()
{
	int f=0,n=0,m=0,w=0;
	int i=0,s=0,e=0,t=0,j=0;
	scanf("%d",&f);
	while(f--)
	{
		scanf("%d%d%d",&n,&m,&w);
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				farm[i][j]=INF;
			}
		}
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&s,&e,&t);
			if(farm[s][e]>t)
			{
				farm[s][e]=t;
				farm[e][s]=t;
			}
		}
		for(i=0;i<w;i++)
		{
			scanf("%d%d%d",&s,&e,&t);
			if(farm[s][e]>-t)
			{
				farm[s][e]=-t;
			}
		}
		if(bellman(n))
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
}

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