Example:https://www.luogu.org/problemnew/show/3371
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <queue>5#include <vector>6#include <cstring>7#include <algorithm>8 using namespacestd;9vector<int>v[10005], c[10005];Ten Const intINF =2147483647; One intdis[10005]; A BOOLvis[10005]; - voidSsspintA//Single source Shortest path - { the for(inti =0; I <10005; ++i) Dis[i] =INF; -Dis[a] =0; -memset (Vis,0,sizeof(Vis)); -queue<int>Q;q.push (a); + while(!q.empty ()) - { + intnow =Q.front (); Q.pop (); AVis[now] =0; at for(inti =0; I < (int) V[now].size (); ++i) - { - if(Dis[now] + C[now][i] <Dis[v[now][i]]) - { -Dis[v[now][i]] = Dis[now] +C[now][i]; - if(Vis[v[now][i]] = =0) inQ.push (V[now][i]); Vis[v[now][i]] =1; - } to } + } - } the intMain () * { $ intN, M, s;Panax Notoginsengscanf"%d%d%d", &n, &m, &s); - for(inti =0; I < m; ++i) the { + intF, G, W; Ascanf"%d%d%d", &f, &g, &W); the V[f].push_back (g); C[f].push_back (w); + } - SSSP (s); $ for(inti =1; I <= N; i++){ $printf"%d%c", dis[i], i = = n?'\ n':' '); - } -}
Shortest circuit on graph (Bellman-ford + queue optimization algorithm)