poj 3259 wormholes AC代码(负权环判断, Bellmanford)

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>

using namespace std;

const int INF = 10001;

struct Edge {
	int from;
	int to;
	int wgt;
	Edge(int f = -1, int t = -1, int w = INF) :
		from(f), to(t), wgt(w){}
};

bool bellmanford(vector<Edge> &G, int num, int src) {
	vector<int> dist(num+1, INF);
	dist[src] = 0;

	int st;
	for (st = 0; st < num; ++st) {
		bool chg = false;
		for (int i = 0; i < G.size(); ++i) {
			if (dist[G[i].to] > dist[G[i].from] + G[i].wgt) {
				dist[G[i].to] = dist[G[i].from] + G[i].wgt;
				chg = true;
			}
		}
		if (!chg) break;
	}

	if (st == num) return true;
	else return false;
}

int main(void)
{
	int F;
	cin >> F;
	while (F--) {
		int N, M, W;
		cin >> N >> M >> W;

		vector<Edge> G;

		for (int i = 0; i < M; ++i) {
			int s, e, t;
			scanf("%d%d%d", &s, &e, &t);
			G.push_back(Edge(s, e, t));
			G.push_back(Edge(e, s, t));
		}

		for (int i = 0; i < W; ++i) {
			int s, e, t;
			scanf("%d%d%d", &s, &e, &t);
			G.push_back(Edge(s, e, -t));
		}

		if (bellmanford(G, N, 1))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	//system("pause");
	return 0;

}
如果用STL做邻接矩阵的话会超时,内存申请释放开销太大.而且题目中<span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;">N</span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;"> (1 ≤ </span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;">N</span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;"> ≤ 500), <span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;">M</span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;"> (1 ≤ </span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;">M</span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;"> ≤ 2500), <span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;">W</span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;"> (1 ≤ </span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;">W</span><span style="font-family: 'Times New Roman', Times, serif; font-size: 16px;"> ≤ 200) 明显为稀疏图,如果用邻接矩阵表示的话,搜索的开销也会很大.于是考虑储存边的信息.</span></span></span>
时间: 2024-10-01 02:25:38

poj 3259 wormholes AC代码(负权环判断, Bellmanford)的相关文章

POJ 3259 Wormholes 虫洞(负权最短路,负环)

题意:给一个混合图,求判断是否有负环的存在,若有,输出YES,否则NO.有重边. 思路:这是spfa的功能范围.一个点入队列超过n次就是有负环了.因为是混合图,所以当你跑一次spfa时发现没有负环,但是负环仍可能存在,因为有向边! 但是单源最短路也有起点啊,难道穷举起点?不用,负环是必须有某些边是带负权的,那么我们只要穷举负权边的起点就行了,因为单单跑一次spfa不能保证能遍历所有点,但是如果穷举负权边起点还没有找到负环,那么负环不可能存在(剩下的都是正权,怎么可能有负环). 1 //#incl

POJ - 3259 Wormholes(求负权回路)

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's fa

POJ 3259 Wormholes (判负环)

Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path

POJ 3259 Wormholes(SPFA判负环)

题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是记录这个点松弛进队的次数,次数超过点的个数的话,就说明存在负环使其不断松弛. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using na

poj 3259 Wormholes[ bellman_ford 判负环]

Wormholes Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wor

poj 3259 Wormholes 【最短路之负权环的判断】

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 54435   Accepted: 20273 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

ACM: POJ 3259 Wormholes - SPFA负环判定

POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way pa

[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

Poj 3259 Wormholes 负环判断 SPFA &amp; BellmanFord

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <