poj3259:负环判定,SPFA

代码

几天没有AC了..实在手痒啊,上题。

Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 41416 Accepted: 15226

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 wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input

Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

Hint

For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

Source

题目大意:
有F组测试数据。有n个点,m条权值为正的道路,w条权值为负(其实需要在插入时取负值,因为输入的实际上是正的)的道路,判断是否存在负环。
思路:
难得没看题解做出来的一题。
偏水吧,有几个注意点。(针对SPFA)
  1. 正权道路是双向的;
  2. 负权道路是单向的;
  3. 题目的数据范围貌似小了;
  4. 初始化...(大神请无视)

一开始WA了几次,看了discuss说要判连通,到了最后发现其实并不用...

O(nke)做法吧貌似,不太会算..

贴上代码。

/*
Source Code

Problem: 3259		User: aclolicon
Memory: 596K		Time: 1766MS
Language: C++		Result: Accepted
Source Code
*/
#include
#include
#define MAXN 40000
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
	int to;
	int next;
	int w;
}edge[MAXN];
int head[MAXN];
int cnt = 0;
int n, m, w;

void add(int s, int e, int t){
	edge[cnt].w = t;
	edge[cnt].to = e;
	edge[cnt].next = head[s];
	head[s] = cnt++;
}
bool spfa(int s){
	queue  q;
	q.push(s);
	int cc[MAXN], dis[MAXN];
	bool vis[MAXN];
	for (int i = 0; i < n + 1; i++){
		cc[i] = 0;
		dis[i] = INF;
		vis[i] = 0;
	}
	dis[s] = 0;
	bool flag = 0;
	while(!q.empty()){
		int x = q.front();
		q.pop();
		vis[x] = 0;
		for (int i = head[x]; i != -1 && !flag; i = edge[i].next){
			int to = edge[i].to;
			if (dis[x] + edge[i].w < dis[to]){
				dis[to] = dis[x] + edge[i].w;
				if (!vis[to]){
					vis[to] = 1;
					q.push(to);
					cc[to]++;
					if (cc[to] >= n){
						flag = 1;
						break;
					}
				}
			}
		}
		if (flag) break;
	}
	if (flag) return 0;
	return 1;
}

void init(){
	for (int i = 0; i < n + 1; i++){
		edge[i].to = -1;
		edge[i].w = 0;
		edge[i].next = -1;
		head[i] = -1;
	}
}

void solve(){
	bool flag = 0;
	cnt = 0;
	int s, e, t;
	for (int i = 0; i < m; i++){
		scanf("%d%d%d", &s, &e, &t);
		add(s, e, t);
		add(e, s, t);
	}
	for (int i = 0; i < w; i++){
		scanf("%d%d%d", &s, &e, &t);
		add(s, e, -t);
	}
	for (int i = 1; i <= n; i++){
		if (!spfa(i)) {
			flag = 1;
			break;
		}
	}
	if (flag) printf("YESn");
	else printf("NOn");	
}
 
int main(){
	int f;
	scanf("%d", &f);
	while(f--) {
		scanf("%d%d%d", &n, &m, &w);
		init();
		solve();
	}
	return 0;
} 

我还没有学会写个人说明!

发表评论

电子邮件地址不会被公开。 必填项已用*标注