社区讨论

新手求条,RE第二遍不输出(可关)

P3385【模板】负环参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhizemdh
此快照首次捕获于
2025/11/03 18:12
4 个月前
此快照最后确认于
2025/11/03 18:12
4 个月前
查看原帖
代码:
CPP
#include <bits/stdc++.h>
using namespace std;

int group, pot, imf;
bool visited[1003];
const int N = 1e3 * 3;

struct Node {
	int num;
	int vh[1003];
	int count[1003];
}t[N];

void insert(int ffrom, int tto, int val) {
	t[ffrom].num++;
	t[ffrom].vh[tto] = val;
	t[ffrom].count[t[ffrom].num] = tto;
	if (val >= 0) {
		t[tto].num++;
		t[tto].vh[ffrom] = val;
		t[tto].count[t[tto].num] = ffrom;
	}
}

bool check(int cnt, int temp, int val) {
	if (cnt == 1 && !temp && val < 0) {
		return true;
	}
	if (visited[cnt]) return false;
	visited[cnt] = true;
	bool judge = false;
	for (int i = 1; i <= t[cnt].num; i++) {
		bool judge = (judge || check(t[cnt].count[i], temp + 1, val + t[cnt].vh[t[cnt].count[i]]));
	}
	return judge;
}

int main() {
	cin >> group >> pot >> imf;
	for (int  j = 1; j <= group; j++) {
		for (int i = 0; i < imf; i++) {
			int ffrom, tto, vval;
			cin >> ffrom >> tto >> vval;
			insert(ffrom, tto, vval);
		}
		string s = check(1, 1, 0) ? "YES" : "NO";
		cout << s;
		for (int i = 1; i <= N; i++) {
			for (int k = 1; k <= t[i].num; k++) {
				t[i].vh[t[i].count[k]] = 0;
				t[i].count[k] = 0;
			}
			t[i].num = 0;
		}
	}
	
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...