社区讨论

90pts求调

P3275[SCOI2011] 糖果参与者 2已保存回复 1

讨论操作

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

当前回复
1 条
当前快照
1 份
快照标识符
@mhjdlams
此快照首次捕获于
2025/11/04 00:49
4 个月前
此快照最后确认于
2025/11/04 00:49
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>

using namespace std;
#define int long long

const int N = 1e5 + 10;
int n, k;

struct edge{
	int u, v, nxt, w;
}e[N * 4];
int cnt = 0, head[N * 4];
int tot[N], dis[N];
bool vis[N];

inline int read(){
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9'){
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9'){
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}

inline void add(int u, int v, int w){
	cnt++;
	e[cnt].u = u;
	e[cnt].v = v;
	e[cnt].w = w;
	e[cnt].nxt = head[u];
	head[u] = cnt;
}

inline bool spfa(int s){
	for (int i = 1; i <= n; i++)
		dis[i] = 0x3f3f3f3f;
	dis[s] = 0;
	vis[s] = 1;
	queue<int> q;
	q.push(s);
	int sum = 0;
	while (!q.empty()){
		int u = q.front();
		q.pop();
		vis[u] = 0;
		for (int i = head[u]; i; i = e[i].nxt){
			int v = e[i].v;
			int w = e[i].w;
			if (dis[v] > dis[u] + w){
				dis[v] = dis[u] + w;
				sum++;
				if (sum > 5e6) return false;
				if (!vis[v]){
					vis[v] = 1;	
					q.push(v);
				}
				tot[v]++;
				if (tot[v] > n + 1) return false;	
			}
		}
	}
	return true;
}

signed main(){
	n = read(), k = read();
	for (int i = 1; i <= k; i++){
		int x, u, v;
		x = read(), u = read(), v = read();
		if (x == 1) add(u, v, 0), add(v, u, 0);
		if (x == 2) add(u, v, -1);
		if (x == 3) add(v, u, 0);
		if (x == 4) add(v, u, -1);
		if (x == 5) add(u, v, 0);
	}
	for (int i = 1; i <= n; i++)
		add(0, i, -1);
	if (!spfa(0)){
		cout << "-1";
		return 0;
	}
	int ans = 0;
	for (int i = 1; i <= n; i++)
		ans += dis[i];
	cout << abs(ans);
	return 0;
}

回复

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

正在加载回复...