堆优化的dijkstra
https://www.luogu.org/problem/show?pid=1339
#include<cstdio> #include<queue> #include<cstring> #define N 55000 #define M 1000000 using namespace std; int n,m,s,t,tot; int nxt[M],to[M],front[N],cap[M]; int DIS[N]; bool vis[N]; struct edge { int number,dis; bool operator <(edge b) const { return dis>b.dis; } }now,nt; priority_queue<edge>q; void add(int u,int v,int w) { to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; cap[tot]=w; to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; cap[tot]=w; } bool dijkstra() { for(int i=1;i<=n;i++) DIS[i]=2e9; q.push((edge){s,0}); DIS[s]=0; while(!q.empty()) { now=q.top();q.pop(); if(vis[now.number]) continue; vis[now.number]=true; for(int i=front[now.number];i;i=nxt[i]) { if(DIS[to[i]]>DIS[now.number]+cap[i]) { DIS[to[i]]=DIS[now.number]+cap[i]; q.push((edge){to[i],DIS[to[i]]}); } } } printf("%d",DIS[t]); } int main() { int u,v,w; scanf("%d%d%d%d",&n,&m,&s,&t); while(m--) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); } dijkstra(); }