求教,spfa10分...(50万条边的数据是什么鬼)
查看原帖
求教,spfa10分...(50万条边的数据是什么鬼)
238203
跟你沟通楼主2020/6/14 15:25
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 1000003
#define maxm 1000003
using namespace std;
int n, m, s,k, head = 0, tail = 0, dis[maxn], u[maxm], v[maxm], w[maxm], first[maxn], gnext[maxm], que[maxn * 2], book[maxn];
int main()
{
	scanf("%d%d%d", &n, &m, &s);
	for (int i = 1; i <= n; ++i)
	{
		dis[i] = 99999999;
		book[i] = 0;
		first[i] = -1;
	}
	dis[s] = 0;
	//memset(first, -1, sizeof(first));
	for (int i = 1; i <= m; ++i)
	{
		scanf("%d%d%d", &u[i], &v[i], &w[i]);
		gnext[i] = first[u[i]];
		first[u[i]] = i;
	}

	que[tail] = s; tail++;
	book[s] = 1;
	while (head<tail)
	{
		k = first[que[head]];
		while (k != -1)
		{
			if (dis[u[k]] + w[k] < dis[v[k]])
			{
				dis[v[k]] = dis[u[k]] + w[k];
				if (!book[v[k]])
				{
					book[v[k]]=1;
					que[tail] = v[k];
					++tail;
				}
			}
			k = gnext[k];
		}
		++head;
	}

	for (int i = 1; i <= n; ++i)
		printf("%d ", dis[i]);
	return 0;
}
2020/6/14 15:25
加载中...