跳到正文

个人记录

cf20c

zengjunyangQQ约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月13日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e5+5;
struct Node{
	int x,w;
	bool operator < (const Node & tmp)const{
		return w>tmp.w;
	}
};
vector<Node>e[N];
priority_queue<Node>q;
int n,dis[N],vid[N];
bool vis[N];
void dijkstra(int s){
	for(int i=1;i<=n;i++){
		dis[i]=LLONG_MAX-N;
	}
	dis[s]=0;
	q.push({s,0});
	while(!q.empty()){
		int x=q.top().x;
		q.pop();
		if(vis[x]==true)continue;
		vis[x]=true;
		for(int i=0;i<e[x].size();i++){
			int id=e[x][i].x,w=e[x][i].w;
			if(dis[x]+w<dis[id]){
				vid[id]=x;
				dis[id]=dis[x]+w;
				q.push({id,dis[id]});
			}
		}
	}
}
int ans[N],cnt=0;
signed main(){
	int m,s,u,v,w;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>u>>v>>w;
		e[u].push_back({v,w});
		e[v].push_back({u,w});
	}
	dijkstra(1);
	if(dis[n]==LLONG_MAX-N){
		cout<<-1<<endl;
		return 0;
	}
	int last=n;
	while(last!=0){
		ans[++cnt]=last;
		last=vid[last];
	}
	for(int i=cnt;i>=1;i--){
		cout<<ans[i]<<" ";
	}
	return 0;
}
原文