rt,朴素dij,实在不知道错在哪里, nn 的范围已经调到尽可能大了仍然re。 求调

#include <iostream>
#include <cstdio>
using namespace std;
#define inf 2147483647

const int N = 3009, M = 5000009;
int h[N][N], n, m, s;
int dis[N], vis[N];

inline void dijkstra() {
	for (int i = 1; i <= n; i++) dis[i] = inf;
	dis[s] = 0;
	for (int i = 1; i <= n; i++) {
		int id, mind = inf;
		for (int j = 1; j <= n; j++) {
			if (dis[j] < mind && !vis[j]) {
				id = j;
				mind = dis[j];
			}
		}
		vis[id] = 1;
		for (int j = 1; j <= n; j++) {
			if (h[id][j] != inf) dis[j] = min(dis[j], dis[id] + h[id][j]);
		}
	}
}

signed main () {
	int c, u, v;
	scanf ("%d%d%d", &n, &m, &s);
	for (int i = 1; i <= n; i++) 
		for (int j = 1; j <= n; j++) 
			h[i][j] = inf;
	for (int i = 1; i <= m; i++) {
		scanf ("%d%d%d", &u, &v, &c);
		h[u][v] = min(c, h[u][v]);
	}
	dijkstra();
	for (int i = 1; i <= n; i++) printf ("%d ", dis[i]);
	return 0;
}

3 条评论

  • 1

信息

ID
2307
时间
1000ms
内存
256MiB
难度
8
标签
(无)
递交数
37
已通过
7
上传者