1. 程式人生 > >[模板]單源最短路徑

[模板]單源最短路徑

open http efi push_back continue operator 技術 cli body

https://www.luogu.org/problemnew/show/P3371 技術分享圖片
 1 #include <cstring>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <queue>
 5 #define lop(i,a,b) for(register int i=a;i<=b;++i)
 6 const int N = 10010, M = 500010, inf = 2147483647;
 7 using namespace std;
 8 const
int bufsize = 500000; 9 char buf[bufsize], *p1, *p2; 10 #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,bufsize,stdin),p1==p2)?EOF:*p1++) 11 12 inline int read(){ 13 register int c = getchar(), f = 1, x = 0; 14 while (!isdigit(c)){if (c == -)f = -1; c = getchar();} 15 while
(isdigit(c)){x = x * 10 + c - 0; c = getchar();} 16 return x * f; 17 } 18 int head[N], dis[N], tot, n, m, s, t;bool vis[N]; 19 struct Edge{int v, w, next;}G[M]; 20 struct Node{ 21 int u, w; 22 bool operator<(const Node & rhs) const{ 23 return w > rhs.w; 24 }
25 }; 26 27 inline void addedge(int u, int v, int w){ 28 G[++tot].v=v;G[tot].w=w;G[tot].next=head[u];head[u]=tot; 29 } 30 inline void Dijkstra(int s){ 31 //memset(dis,127,sizeof(dis)); 32 priority_queue<Node> q; 33 lop(i,1,n)dis[i] = inf; 34 dis[s] = 0; 35 q.push((Node){s,dis[s]}); 36 while(!q.empty()){ 37 Node x = q.top();q.pop(); 38 int u = x.u; 39 if (vis[u]) continue; 40 for(int i = head[u];i;i = G[i].next){ 41 int v = G[i].v,w = G[i].w; 42 if (dis[u] + w < dis[v]){ 43 dis[v] = dis[u] + w; 44 q.push((Node){v, dis[v]}); 45 } 46 } 47 vis[u] = 1; 48 } 49 } 50 51 int main(void){ 52 n = read(), m = read(), s = read(); 53 lop(i,1,m){ 54 int u = read(), v = read(), w = read(); 55 addedge(u, v, w); 56 } 57 Dijkstra(s); 58 lop(i,1,n)printf("%d ", dis[i]); 59 return 0; 60 }
Dijkstra 技術分享圖片
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <queue>
 5 #include <cstring>
 6 using namespace std;
 7 const int N=10005;
 8 const int M=500005;
 9 const int inf=0x7f7f7f7f;
10 struct Node{
11     int v, w;
12 };
13 inline Node Make_Node(int v, int w){
14     Node a; a.v = v; a.w = w;
15     return a;
16 }
17 vector<Node> g[N];
18 int dis[N],n,tot,head[N],inq[N],m,s;
19 queue<int> q;
20 
21 inline int read(){
22     int f=1,x=0;char ch;
23     do{ch=getchar();if(ch==-)f=-1;}while(ch<0||ch>9);
24     do{x=x*10+ch-0;ch=getchar();}while(ch>=0&&ch<=9);
25     return f*x;
26 }
27 inline void addedge(int u,int v,int w){
28    g[u].push_back(Make_Node(v, w));
29 }
30 inline void spfa(int s){
31     memset(dis,127,sizeof(dis));dis[s]=0;
32     q.push(s);inq[s]=1;
33     while(!q.empty()){
34         /*int u=q.front();q.pop();inq[u]=0;
35         for(int i=head[u];i;i=G[i].next){
36             int v=G[i].v;
37             if(dis[u]+G[i].w<dis[v]){
38                dis[v]=dis[u]+G[i].w;
39           //  dis[v] = min(dis[v], dis[u] + G[i].w);
40                 if(!inq[v])q.push(v),inq[v]=1;
41             }
42         }*/
43         int u = q.front();q.pop();inq[u] = 0;
44         for(int i = 0; i < g[u].size(); ++i){
45             int v = g[u][i].v, w = g[u][i].w;
46             if (dis[u]+w< dis[v]){
47                 dis[v] = dis[u] + w;
48                 if (!inq[v])q.push(v),inq[v] = 1;
49             }
50         }
51     }
52 }
53 int main(){
54     n=read();m=read();s=read();
55     for(int i=1;i<=m;i++){
56         int u=read(),v=read(),w=read();
57         addedge(u,v,w);
58     }
59     spfa(s);
60     for(int i=1;i<=n;i++)printf("%d%c",dis[i]==inf?2147483647:dis[i],i==n?\n: );
61     return 0;
62 }
SPFA

[模板]單源最短路徑