全wa请教了
查看原帖
全wa请教了
890321
mooktian楼主2024/11/2 20:17

用标准版(堆优化)的dijkstra ac代码贴过来,7wa,3re。

换成朴素版的dijsktra,用邻接矩阵存图,全MLE了, 把领接矩阵换成邻接表,全WA了。

我晕,样例都对啊,怎么也该给点分啊。

代码如下,大佬帮瞅瞅。

//朴素dijkstra
//用邻接表存图

#include <bits/stdc++.h>

using namespace std;

const int N = 1e4+10, M = 5e5+10;

int dist[N],st[N];
int h[N],e[M],w[M],ne[M],idx;
int n,m,s;
void add(int a,int b,int c) {
	e[idx] = b,w[idx] = c,ne[idx] = h[a] ,h[a] = idx ++;
}
void dijkstra() {
	memset(dist,0x3f,sizeof dist);
	dist[1] = 0;
	for(int i = 0;i < n;i++) {
		int t = -1;
		for(int j = 1;j <= n;j++) 
			if(!st[j] && (t == -1 || dist[j] < dist[t] ) ) {
				t = j;
			}
		for(int j = h[t];j != -1;j = ne[j]) {
			int k = e[j];
			dist[k] = min(dist[k],dist[t] + w[j]);
		}
		st[t] = 1;		
	}
}
int main() {
	cin >> n >> m >> s;
	memset(h,-1,sizeof h);	
	while(m--) {
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c);
	}
	dijkstra();
	for(int i = 1;i <= n;i++) printf("%d ",dist[i]);
    return 0;
}

2024/11/2 20:17
加载中...