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 mrlizzzzz at 2017-04-05 21:47:56 on Problem 3259
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<set>
using namespace std;
int n;
int f;
const int MAX = 0x3f3f3f3f;
const int NUM = 510;
int map[NUM][NUM] ;
int dis[NUM];
int vis[NUM];
int p[NUM];
int c[NUM];
int spfa()
{
	memset(vis, 0, sizeof(vis));
	memset(c, 0, sizeof(vis));
	queue<int> q;
	dis[1] = 0;
	q.push(1);
	vis[1] = 1;
	c[1] = 1;
	while (!q.empty())
	{
		int x;
		x = q.front();
		q.pop();
		vis[x] = 0;
		for (int k = 1; k <= n; k++)
		{
			if (dis[x] + ::map[x][k] < dis[k]&&k!=x)
			{
				dis[k] = dis[x] + ::map[x][k];
				if (!vis[k])
				{
					vis[k] = 1;
					c[k]++;
					q.push(k);
					if (c[k] > 1000)
						return 1;
				}
			}
		}
	}
	return 0;
}
int main()
{
	cin >> f;
	for (int i = 0; i < f; i++)
	{
		int  m, w;
		for (int i = 0; i < NUM; i++)
			for (int j = 0; j < NUM; j++)
				::map[i][j] = MAX;
		for (int i = 0; i < NUM; i++)
			::dis[i] = MAX;
		cin >> n >> m >> w;
		for (int i = 1; i <= m; i++)
		{
			int s, e, t;
			cin >> s >> e >> t;
			if (::map[s][e] == 0 || t < ::map[s][e])
				::map[s][e] = t;
			if (::map[e][s] == 0 || t < ::map[e][s])
				::map[e][s] = t;
		}
		for (int i = 1; i <= w; i++)
		{
			int s, e, t;
			cin >> s >> e >> t;
			if (::map[s][e] == 0 || -t < ::map[s][e])
				::map[s][e] = -t;
		}
		if (spfa())
			printf("YES\n");
		else
			printf("NO\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