标签:cstring algorithm code pop nbsp void ace 链接 rom
题目链接:https://www.luogu.org/problemnew/show/P3371
我永远都喜欢Flyod、dijkstra + heap、SPFA
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 1000000; 7 const int inf = 0x7fffffff; 8 int n, m, s, dis[maxn]; 9 bool vis[maxn]; 10 struct edge{ 11 int to, next, len, from; 12 }e[maxn]; 13 int head[maxn], cnt; 14 queue<int> q; 15 void add(int u, int v, int w) 16 { 17 e[++cnt].from = u; 18 e[cnt].to = v; 19 e[cnt].len = w; 20 e[cnt].next = head[u]; 21 head[u] = cnt; 22 } 23 int SPFA() 24 { 25 while(!q.empty()) 26 { 27 int now = q.front(); 28 q.pop(); 29 vis[now] = 0; 30 for(int i = head[now]; i ; i = e[i].next) 31 { 32 if(dis[e[i].to] > dis[now]+e[i].len) 33 { 34 dis[e[i].to] = dis[now]+e[i].len; 35 if(!vis[e[i].to]) 36 { 37 vis[vis[e[i].to]] = 1; 38 q.push(e[i].to); 39 } 40 } 41 } 42 } 43 } 44 int main() 45 { 46 scanf("%d%d%d",&n,&m,&s); 47 for(int i = 1; i <= n; i++) 48 dis[i] = inf; 49 for(int i = 1; i <= m; i++) 50 { 51 int u,v,w; 52 scanf("%d%d%d",&u,&v,&w); 53 add(u,v,w); 54 } 55 dis[s] = 0; 56 q.push(s); 57 vis[s] = 1; 58 SPFA(); 59 for(int i = 1; i <= n; i++) 60 printf("%d ",dis[i]); 61 }
标签:cstring algorithm code pop nbsp void ace 链接 rom
原文地址:https://www.cnblogs.com/MisakaAzusa/p/8947140.html