P1144 最短路计算 0分求调
查看原帖
P1144 最短路计算 0分求调
974048
zhangmuning1016楼主2024/9/25 19:16

大佬求救

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct node{
	int front,w,to;
}g[1000001];
int head[1000001],d[1000001],t,n,m; 
bool v[1000001];
queue<int> q;
void add(int x,int y,int z){
	t++;
	g[t].front=head[x];
	g[t].to=y;
	g[t].w=z;
	head[x]=t;
}
void spfa(){
	for(int i=1;i<=n;i++){
		d[i]=1e9;
	}
	d[1]=0;
	v[1]=1;
	q.push(1);
	while(q.size()){
		int x=q.front();
		q.pop();
		v[x]=0;
		for(int i=head[x];i;i=g[i].front){
			int j=g[i].to,k=g[i].w;
			if(d[j]>d[x]+k){
				d[j]=d[x]+k;
				if(v[j]==0){
					q.push(j);
					v[j]=1;
				}
			}
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin>>x>>y>>z; 
		add(x,y,z);
		add(y,x,z);
	}
	spfa();
	cout<<d[n];
	return 0;
}
2024/9/25 19:16
加载中...