#4 #8 WA 84pts 求调
查看原帖
#4 #8 WA 84pts 求调
724966
YiBoRrui6楼主2023/2/18 16:24

rt

#include<bits/stdc++.h>
using namespace std;

int que;
int inf = 214748364;
int ans=inf;
int dis[200050], inqueue[200050];
int n, m;
queue<int> q;
bool dict[200050], flag;
int edgecnt = 0;
int h[200050];
int showans[200050];
int tot;

struct Edge
{
	int to, w, next;
}edge[200050];

void addedge(int u, int v, int w)
{
	edge[++edgecnt].to = v;
	edge[edgecnt].w = w;
	edge[edgecnt].next = h[u];
	h[u] = edgecnt;
}

void init()
{
	edgecnt = 0;
	for (int i = 0; i <= n; i++) h[i] = -1;
}

void spfa(int s)
{
	for (int i = 1; i <= n; i++) dis[i] = inf;
	memset(dict, false, sizeof(dict));
	memset(inqueue, 0, sizeof(inqueue));
	dis[s] = 0; dict[s] = 1;
	inqueue[s] = 1;
	q.push(s);
	while (!q.empty())
	{
		int x = q.front();
		q.pop();
		dict[x] = 0;
		for (int i = h[x]; i != -1; i = edge[i].next){
			if (inqueue[x] > n-1){
				flag = false; return;
			}
			if (dis[edge[i].to] > dis[x] + edge[i].w){
				dis[edge[i].to] = dis[x] + edge[i].w;
				if (!dict[edge[i].to]){
					q.push(edge[i].to);
					inqueue[edge[i].to]++;
					dict[edge[i].to] = 1;
				}
			}
		}
	}
	flag = true;
}

int main()
{
	scanf("%d", &que);
	while (que--)
	{
		scanf("%d%d", &n, &m);
		init();
		int u, v, w;
		for (int i = 1; i <= m; i++){
			scanf("%d%d%d", &u, &v, &w);
			if (w >= 0){
				addedge(u, v, w);
				addedge(v, u, w);
			}
			else addedge(u, v, w);
		}
		spfa(1);
		if (flag) showans[tot++] = 0;
		else showans[tot++] = 1;
	}
	for (int i = 0; i <= tot-1; i++){
		if (showans[i]) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

蒟蒻猜测是不是多测清零的问题??qwq求解

2023/2/18 16:24
加载中...