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。。。

Posted by hjs1228 at 2015-10-16 20:22:38 on Problem 3259
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;

int f,n,m,w,map[510][510],a,b,c,dis[510],e[510],cnt[510],fail;
queue<int> que;

void spfa()
{
	while(!que.empty()&&!fail)
	{
		int v=que.front();
		que.pop();
		e[v]=0;
//		if (dis[v]>10000||dis[v]<-10000)	
//		{
//			fail=1;
//			break;
//		}
		for (int i=1;i<=n;i++)
			if (map[v][i])
				if (dis[i]>map[v][i]+dis[v])
				{
					dis[i]=map[v][i]+dis[v];
					if (!e[i])
					{
						e[i]=1;
						que.push(i);
						cnt[i]++;
						if (cnt[i]>=n) 
						{
							fail=1;
							break;
						}
					}
				}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin>>f;
	for (int l=1;l<=f;l++)
	{
		memset(map,0,sizeof(map));
		memset(e,0,sizeof(e));
		memset(cnt,0,sizeof(cnt));
		fail=0;
		while (!que.empty()) que.pop();
//		que.clear();
		cin>>n>>m>>w;
		for (int i=1;i<=n;i++)
			dis[i]=10000000;
		for (int i=1;i<=m;i++)
		{
			cin>>a>>b>>c;
			if (map[a][b]) 
				map[a][b]=min(map[a][b],c);
			else map[a][b]=c;
			if (map[b][a]) 
				map[b][a]=min(map[b][a],c);
			else map[b][a]=c;
		}
		for (int i=1;i<=w;i++)
		{
			cin>>a>>b>>c;
			if (map[a][b]) 
				map[a][b]=min(map[a][b],-c);
			else map[a][b]=-c;
		}
		que.push(1);e[1]=1;dis[1]=0;cnt[1]=1;
		spfa();
		if (fail) 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