spfa没输出?
查看原帖
spfa没输出?
241484
Tomori_Nao_楼主2020/7/22 22:52
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
int elast[2005],dis[2005],cnt[2005],n,m,cntt;
bool use[2005];
struct node
{
	int x,y,next;
}e[6005];
queue<int> q;
string spfa(int x)
{
	dis[x] = 0;
	q.push(x);
	use[x] = true;
	cnt[x]++;
	while(!q.empty())
	{
		int tot = q.front(),t;
		q.pop();
		use[tot] = false;
		t = elast[tot];
		while(t != 0)
		{
			int u = e[t].x;
			if(dis[u] > dis[tot] + e[t].y)
			{
				dis[u] = dis[tot] + e[t].y;
				cnt[u]++;
				if(cnt[u] == n) return "YES";
				if(!use[u])
				{
					q.push(u);
					use[u] = true;
				}
			}
		}
	}
	return "NO";
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		memset(dis,0x3f3f3f3f,sizeof(dis));
		memset(use,false,sizeof(use));
		cntt = 0;
		for(int i = 1;i <= m;i++)
		{
			int a,b,c;
			scanf("%d %d %d",&a,&b,&c);
			if(c >= 0)
			{
				cntt++;
				e[cntt].x = b;
				e[cntt].y = c;
				e[cntt].next = elast[a];
				elast[a] = cntt;
				cntt++;
				e[cntt].x = a;
				e[cntt].y = c;
				e[cntt].next = elast[b];
				elast[b] = cntt;
			}
			else
			{
				cntt++;
				e[cntt].x = b;
				e[cntt].y = c;
				e[cntt].next = elast[a];
				elast[a] = cntt;
			}
		}
		cout << spfa(1) << endl;
	}
	return 0;
}
2020/7/22 22:52
加载中...