第三个点本地能过,交上去咋wa了
查看原帖
第三个点本地能过,交上去咋wa了
757101
junee楼主2023/11/4 22:08
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<cstdio>
using namespace std;
const int N=1e6+10,M=N*5;
long h[N],e[N*2],ne[N*2],w[N],idx;
long dist[N];
bool st[N];
typedef pair<long long,long long>PII;
int n,m,s,en;
void add(int a,int b,int c){
	e[idx]=b;ne[idx]=h[a];w[idx]=c;h[a]=idx++;	
}
void dijkstra(){
	memset(dist,0x7f,sizeof dist);
	priority_queue<PII,vector<PII>,greater<PII> >heap;
	dist[s]=0;
	heap.push({0,s});
	while(heap.size()){
		int ver=heap.top().second;
		heap.pop();
		if(st[ver])continue;
		st[ver]=true;
		for(int i=h[ver];~i;i=ne[i]){
			int j=e[i];
			if(dist[j]>dist[ver]+w[i]){
				dist[j]=dist[ver]+w[i];
				heap.push({dist[j],j});
			}
		}
	}
}
int main(){
	memset(h,-1,sizeof h);
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++){
		long long a,b,c;
		cin>>a>>b>>c;
	}
	dijkstra();
	for(int i=1;i<=n;i++){
		if(dist[i]==0x7f7f7f7f)cout<<2147483647<<' ';
		else cout<<dist[i]<<" ";
	}
	return 0;
}
3.in
5 15 5
2 2 270
1 4 89
2 1 3
5 5 261
5 2 163
5 5 275
4 5 108
4 4 231
3 4 213
3 3 119
3 1 77
3 1 6
2 4 83
5 5 196
5 5 94
3.out
166 163 2147483647 246 0 

2023/11/4 22:08
加载中...