0分求调
查看原帖
0分求调
1142439
ZXmax2005楼主2024/11/27 22:45

用的acwing的模板,0分求调试,本地能过样例

#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 1e5 + 10;

int h[N], e[N], ne[N], idx;
int w[N];
int dist[N];
bool vistied[N];
int n, m;

void add(int a, int b, int c)
{
	w[idx] = c;
	e[idx] = b;
	ne[idx] = h[a];
	h[a] = idx++;
}

void dijkstra()
{
	memset(dist, 0x3f, sizeof dist);
	dist[1] = 0;

	priority_queue<PII, vector<PII>, greater<PII>> heap;
	heap.push({ 0,1 });

	while (heap.size())
	{
		PII k = heap.top();
		heap.pop();

		int ver = k.second, distance = k.first;

		if (vistied[ver]) continue;
		vistied[ver] = true;

		for (int i = h[ver]; i != -1; i = ne[i])
		{
			int j = e[i];
			if (dist[j] > distance + w[i])
			{
				dist[j] = distance + w[i];
				heap.push({ dist[j],j });
			}
		}
	}

	for (int i = 1; i <= n; i++)
	{
		cout << dist[i] << " ";
	}
}

int main()
{
	memset(h, -1, sizeof h);
	cin >> n >> m;

	while (m--)
	{
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
	}
	
	dijkstra();

	return 0;
}
2024/11/27 22:45
加载中...