过了但是有个疑问
查看原帖
过了但是有个疑问
546289
Link_Cut_qwq楼主2021/8/20 22:11

把mark整个去掉不会死循环吧?(试了一遍TLE了三个点)

#include <bits/stdc++.h>
#define N 100010
#define M 400020
using namespace std;

struct Edge
{
	int to, Nxt;
	long long val;
} E[M];

struct Node
{
	int t;
	long long s;
};

int n, m, s, a, b, cnt, h[N];
long long w, dis[N];
bool mark[N];

void add (int u, int v, long long d)
{
	E[++cnt].to = v;
	E[cnt].val = d;
	E[cnt].Nxt = h[u];
	h[u] = cnt;
}

struct Sort
{
	bool operator ()(Node x, Node y)
	{
		return x.s > y.s;
	}
}; 

void dij ()
{
	memset (dis, -245, sizeof (dis));
	dis[s] = 0;
	priority_queue <Node, vector <Node>, Sort> q;
	q.push({s, 0});
	while (!q.empty())
	{
		Node x = q.top();
		q.pop();
		if (mark[x.t]) continue;
		mark[x.t] = 1;
		for (int i = h[x.t]; i; i = E[i].Nxt)
		{
			if (dis[E[i].to] > x.s + E[i].val)
			{
				dis[E[i].to] = x.s + E[i].val;
				q.push({E[i].to, x.s + E[i].val});
			}
		}
	}
}

int main ()
{
	cin >> n >> m >> s;
	for (int i = 1; i <= m; i++)
	{
		scanf ("%d %d %lld", &a, &b, &w);
		add (a, b, w);
//		add (b, a, w);	
//		无向图时用 
	}
	dij ();
	for (int i = 1; i <= n; i++)
	{
		printf ("%lld ", dis[i]);
	}
	return 0;
}

2021/8/20 22:11
加载中...