求助
查看原帖
求助
521118
Gyk1013楼主2023/7/3 10:15
#include<bits/stdc++.h>
#define N 300005
#define M 500005
#define INF 0x7fffffffffffffff
using namespace std;
int head[N],ver[M],val[M],nxt[M],vis[N],tot,n,m;
long long dis[N];
void add(int u,int v,int w){
	ver[++tot]=v;
	val[tot]=w;
	nxt[tot]=head[u];
	head[u]=tot;
}
struct node{
	int x,val;
	bool operator <(const node &a) const{
		return val>a.val;
	}
}; 
void dijkstra(int s){
	priority_queue<node>q;
	dis[s]=0;
	q.push({s,0});
	while(!q.empty()){
		node t=q.top();
		q.pop();
		int x=t.x;
		if(vis[x]) continue;
		vis[x]=1;
		for(int i=head[x];~i;i=nxt[i]){
			int y=ver[i];
			int z=val[i];
			if(dis[y]>dis[x]+z){
				dis[y]=dis[x]+z;
				q.push({y,dis[y]});
			}			
		}
	}
}
int main(){
	memset(dis,0x3f,sizeof dis);
	memset(head,-1,sizeof head);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c);
	}
	dijkstra(1);
	for(int i=1;i<=n;i++){
	    if(dis[i]==INF) printf("-1");
	    else printf("%lld ",dis[i]);
	}
	return 0;
}

WA on #6,求助,谢谢

2023/7/3 10:15
加载中...