pku3259Wormholes(Bellman-ford)

Xredman posted @ Apr 12, 2010 10:41:07 PM in Algorithm with tags pku Bellman-Ford , 1346 阅读
6736064 Xredman 3259 Accepted 296K 204MS C++ 1401B 2010-04-13 09:30:12
#include <iostream>
#include <cstdio>
using namespace std;

const int INF = 100000000;
const int MAX_SIZE = 7005;
const int MAX_POINT = 502;
int n, m, w;
int dist[MAX_POINT];
int edge[MAX_SIZE][3];
int cnt;

bool relax(int u, int v, int c)
{
	if(dist[u] != INF && dist[v] > dist[u] + c)
	{
		dist[v] = dist[u] + c;
		return true;
	}
	return false;
}

bool Bellman_ford(int src)
{
	int i, j;
	bool flag;
	for(i = 1; i <= n; i++)
	{
		dist[i] = INF;
	}
	dist[src] = 0;
	////////////////////////////////////
	for(i = 1; i < n; i++)
	{
		flag = false;
		for(j = 0; j < cnt; j++)
		{
			if(relax(edge[j][0], edge[j][01], edge[j][2]))
				flag = true;
		}
		if(!flag)
			break;
	}
	for(j = 0; j < cnt; j++)
		if(relax(edge[j][0], edge[j][1], edge[j][2]))
			return false;
	return true;
}


int main()
{
	int T;
	int a, b, d;
	int i;
	cin>>T;
	while(T--)
	{
		cnt = 0;
		cin>>n>>m>>w;
		for(i = 0; i < m; i++)
		{
			cin>>a>>b>>d;
			edge[cnt][0] = a;
			edge[cnt][1] = b;
			edge[cnt++][2] = d;
			edge[cnt][0] = b;
			edge[cnt][1] = a;
			edge[cnt++][2] = d;
		}
		for(; i < m + w; i++)
		{
			cin>>a>>b>>d;
			edge[cnt][0] = a;
			edge[cnt][1] = b;
			edge[cnt++][2] = -d;
		}
		if(Bellman_ford(1))
			puts("NO");
		else
			puts("YES");
	}
	return 0;
}

 






google maps verlauf 说:
Jul 23, 2023 04:02:07 AM

Google Maps sind grafische Karten, auf die über einen Computerbrowser zugegriffen werden kann. Der Google Maps-Dienst wurde ursprünglich entwickelt, um Wegbeschreibungen bereitzustellen, und aus diesem Grund wird Google Maps am häufigsten verwendet. google maps verlauf löschen Wenn Sie Google Maps häufig verwenden, haben Sie sicherlich eine lange Liste von Suchanfragen in dieser Maps-Aktivität. Nachdem Sie auf das Suchfeld von Google Maps getippt haben, wird eine Liste Ihrer letzten Suchanfragen angezeigt.

UP Board 6th Class Q 说:
Aug 30, 2023 09:28:35 PM

The Change in UP Board 6th, 7th, 8th, 9th Question paper Pattern is also explained below, Board of High School and Intermediate Education Uttar Pradesh is Going to Conduct the Formative (FA) and Summative Assessment (SA) in 6th, 7th, 8th, 9th class Examination, is a Board of School Education in State of Uttar Pradesh, India. It is a state agency of the Government of UP which is UP Board 6th Class Question Paper 2024 Responsible for the Promotion and Development of Secondary Education in the state, UPMSP is a Board of Education for Public and Private Schools under the State Government of UP, UP Board will announce its High School Education 6th, 7th, 8th, 9th SA, FA Exam Scheduled Announced soon,


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter