vector版SPFA求助
查看原帖
vector版SPFA求助
408071
TankYu楼主2022/3/1 20:57

RT,WA on #10

#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>
#include <cstring>
#define D double
#define LD long double
#define LL long long
#define ULL unsigned long long
#define S string
#define mp make_pair
using namespace std;

const LL N = 2501;
LL n, m, sta;

vector<pair<LL, LL>> e[N];

void add(LL a, LL b, LL s)
{
	e[a].push_back(mp(b, s));
	e[b].push_back(mp(a, s));
}

LL q[N];
LL head, tail;
bool in[N];
LL dis[N];

void spfa()
{
	memset(dis, 0x7f, sizeof(dis));
	q[++tail] = sta;
	in[sta] = true;
	dis[sta] = 0;
	while (tail > head)
		{
			int x = q[++head];
			in[x] = 0;
			if (head == N)
				{
					head = 0;
				}
			for (auto i : e[x])
				{
					int y = i.first;
					if (dis[y] > dis[x] + (i.second))
						{
							dis[y] = dis[x] + (i.second);
							if (in[y] == 0)
								{
									q[++tail] = y;
									in[y] = true;
									if (tail == N)
										{
											tail = 0;
										}
								}
						}
				}
		}
}

int main()
{
	int e;
	cin >> n >> m >> sta >> e;
	for (int i = 0; i < m; i++)
		{
			int x, y, z;
			cin >> x >> y >> z;
			add(x, y, z);
		}
	spfa();
	cout << dis[e];
	return 0;
}
2022/3/1 20:57
加载中...