Dijkstra 84pts 求调
查看原帖
Dijkstra 84pts 求调
374769
Epi4any楼主2022/10/28 17:36

【WA on test #5】

#include <iostream>
#include <cstdio>
#include <bitset>
#include <queue>
#include <cstring>
using namespace std;

inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}

const int maxn = 100005;
const int maxm = 200005;
const int INF = 0x7ffffff;

int n, m, s;
int cnt, head[maxm], dis[maxm], vis[maxm];

struct Edge {
	int v, w, next;
} edge[maxm];

struct node {
	int dis, ind;
	bool operator > (const node &x)const {
		return dis > x.dis;
	}
};

priority_queue<node, vector<node>, greater<node> > q;

inline void addedge(int u, int v, int w) {
	edge[++cnt].v = v;
	edge[cnt].w = w;
	edge[cnt].next = head[u];
	head[u] = cnt;
}

void dijkstra(int st) {
	for (int i = 1; i <= n; i++) dis[i] = INF;
	dis[st] = 0;
	q.push((node) {
		0, st
	});
	while (!q.empty()) {
		int cur = q.top().ind;
		q.pop();
		if (vis[cur]) continue;
		vis[cur]=1;
		for (int j = head[cur]; j; j = edge[j].next) {
			if (dis[edge[j].v] > dis[cur] + edge[j].w) {
				dis[edge[j].v] = dis[cur] + edge[j].w;
				if (vis[edge[j].v] == 0) q.push((node) {
					dis[edge[j].v], edge[j].v
				});
			}
		}
	}
}

int main() {
	n = read(), m = read(), s = read();
	for (int i = 1, u, v, w; i <= m; i++) {
		u = read(), v = read(), w = read();
		addedge(u, v, w);
	}
	dijkstra(s);
	for (int i = 1; i <= n; i++) {
		if (dis[i] != INF) cout << dis[i] << " ";
		else cout << (1 << 37) - 1 << endl;
	}
	cout << endl;
	return 0;
}

大佬轻喷

2022/10/28 17:36
加载中...