11pts,不知道什么唐诗问题,差分约束板子
查看原帖
11pts,不知道什么唐诗问题,差分约束板子
824868
Sunset_afterglow楼主2025/6/24 20:48

RT,大佬求助

#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
	int x = 0 ,f = 1;
	char ch = getchar();
	while('0' > ch || ch > '9'){
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while('0' <= ch && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch & 15);
		ch = getchar();
	}
	return x * f;
}
const int N = 6e3 + 10;
int dis[N] ,cnt[N] ,n ,m ,T;
bool flag[N];
vector<pair<int,int> >a[N];
queue<int> que;
void SPFA() {
	for(int i = 0;i <= n;++ i)
		dis[i] = INT_MAX ,a[n + 1].push_back(make_pair(i ,0));
	dis[n + 1] = 0;
	memset(flag ,0 ,sizeof(flag));
	while(!que.empty())que.pop();
	que.push(n + 1);
	while(!que.empty()){
		int u = que.front();
		flag[u] = 0;
		que.pop();
		for(auto i: a[u]){
			if(dis[u] + i.second < dis[i.first]) {
				dis[i.first] = dis[u] + i.second;
				if(!flag[i.first]) {
					if(++ cnt[i.first] > n + 1) {
						puts("false");
						return;
					}
					flag[i.first] = true;
					que.push(i.first);
				}
			}
		}
	}
	puts("true");
	return ;
}
void solve() {
	n = read() ,m = read();
	for(int i = 0;i <= n + 1;++ i) a[i].clear();
	for(int i = 1 ,u ,v ,w;i <= m;++ i) {
		u = read() ,v = read() ,w = read();
		a[u - 1].push_back(make_pair(v ,w));
		a[v].push_back(make_pair(u - 1, -w));
	}
	SPFA();
	return ;
}
signed main() {
	T = read();
	while(T --) {solve();}
	return 0;
}
2025/6/24 20:48
加载中...