luogu P4779 【模板】单源最短路径(标准版)| dijkstra
寻找每一个点到原点的最小距离
#include<cstdio> #include<queue> using namespace std; int val[200020],vis[200020],head[100010],nxt[200020],to[200020]; int n,m,s,k; struct pot { int x,dis; pot(int _x=0,int _dis=0):x(_x),dis(_dis){} friend bool operator < (pot a,pot b) { return a.dis>b.dis; } }; priority_queue<pot> que; void dijkstra() { for(int i=1;i<=n;i++) dis[i]=2e9; que.push(pot(s,0)); dis[s]=0; while(!que.empty()) { pot now; now=que.top(); que.pop(); if(vis[now.x]) continue; vis[now.x]=true; for(int i=head[now.x];i;i=nxt[i]) { if(dis[to[i]]>dis[now.x]+val[i]) { dis[to[i]]=dis[now.x]+val[i]; que.push(pot(to[i],dis[to[i]])); } } } }