【模板】单源最短路径

单源最短路径,之前用dijkstra+堆优化+邻接表(真·链表)打过了,可是总是不太漂亮。这次是进一步整理了Dijkstra+堆优化的修改版本,SPFA算法,Floyd算法,和链式前向星(真是太妙了)以及打印路径的实现。

结论:
(因为SPFA可能会被卡)Dijkstra(STL优先O2)>Dijkstra(手打)>Dijkstra(STL优先队列不O2)>SPFA
所以能写Dijkstra尽量不写SPFA


Dijkstra

由于堆优化很难打,但是STL确实会慢些,NOIP又不给开O2,所以准备了两个版本。
STL版:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXM=200005;
const int MAXN=100005;
const int INF=2147483647;
struct edge{
int next;
int to;
ll w;
};
edge g[MAXM];
int head[MAXN],nume=-1;
int n,m,s;
inline void adde(int f,int t,int w){
g[++nume].next=head[f];
g[nume].to=t;
g[nume].w=w;
head[f]=nume;
}

ll dist[MAXN];
bool vis[MAXN]={0};
struct node{
int no;
ll dist;
node(int x,ll y){
no = x;
dist = y;
}
bool operator <(const node &s)const{
return s.dist<dist;
}
};

priority_queue<node> q;
void dij(){
int i,j;
node now(0,0);
for(i=1;i<=n;i++)
dist[i]=INF;
dist[s]=0;
q.push(node(s,0));

while(!q.empty()){
now=q.top();
q.pop();
if(!vis[now.no]){
vis[now.no]=1;
for(i=head[now.no];i!=-1;i=g[i].next)
if(g[i].w+now.dist<dist[g[i].to]){
dist[g[i].to]=g[i].w+now.dist;
q.push(node(g[i].to,dist[g[i].to]));
}
}
}
}

int main(){
memset(head,-1,sizeof(head));
scanf("%d%d%d",&n,&m,&s);
int x,y,z;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
adde(x,y,z);
}
dij();
for(int i=1;i<=n;i++)
printf("%lld ",dist[i]);
return 0;
}

手打版:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXM=200005;
const int MAXN=100005;
const int INF=2147483647;
struct edge{
int next;
int to;
ll w;
};
edge g[MAXM];
int head[MAXN],nume=-1;
int n,m,s;
inline void adde(int f,int t,int w){
g[++nume].next=head[f];
g[nume].to=t;
g[nume].w=w;
head[f]=nume;
}

struct node{
int no;
ll dist;
node(){}
node(int x,ll y){
no = x;
dist = y;
}
bool operator <(const node &s)const{
return s.dist<dist;
}
bool operator >(const node &s)const{
return s.dist>dist;
}
};

node q[MAXN];
int siz=0;

void push(node x){
q[++siz]=x;
int now=siz;
while(now){
int nxt=now>>1;
if(q[nxt]<q[now])swap(q[nxt],q[now]);
else break;
now=nxt;
}
return;
}

void pop(){
swap(q[siz],q[1]);siz--;
int now=1;
while((now<<1)<=siz){
int nxt=now<<1;
if(nxt+1<=siz&&q[nxt+1]>q[nxt])nxt++;
if(q[nxt]>q[now])swap(q[now],q[nxt]);
else break;
now=nxt;
}
}

node top(){
return q[1];
}


ll dist[MAXN];
bool vis[MAXN]={0};
void dij(){
int i,j;
node now;

for(i=1;i<=n;i++)
dist[i]=INF;
dist[s]=0;
push(node(s,0));

while(siz>0){
now=top();
pop();
if(!vis[now.no]){
vis[now.no]=1;
for(i=head[now.no];i!=-1;i=g[i].next)
if(g[i].w+now.dist<dist[g[i].to]){
dist[g[i].to]=g[i].w+now.dist;
push(node(g[i].to,dist[g[i].to]));
}
}
}
}

int main(){
memset(head,-1,sizeof(head));
scanf("%d%d%d",&n,&m,&s);
int x,y,z;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
adde(x,y,z);
}
dij();
for(int i=1;i<=n;i++)
printf("%lld ",dist[i]);
return 0;
}

SPFA

STL版:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=2147483647;
const int MAXN=10005;
const int MAXM=500005;
const int MAX=500000;

struct edge{
int next,to;
ll w;
};
struct edge list[MAXM];
int n,m,s,nume=-1;

ll dist[MAXN]={0};
bool vis[MAXN]={0};
int head[MAXM]={0};

inline void adde(int f,int t,int w){
list[++nume].next=head[f];
list[nume].to=t;
list[nume].w=w;
head[f]=nume;
}
queue<ll> q;

void spfa(int s){
int i,u,v;
for(i=1;i<=n;i++)
dist[i]=INF;
q.push(s);
dist[s]=0;
vis[s]=1;
while(!q.empty()){
u=q.front();
q.pop();
vis[u]=0;
for(i=head[u]; i!=-1; i=list[i].next){
v=list[i].to;
if(dist[v]>dist[u]+list[i].w){
dist[v]=dist[u]+list[i].w;
if(!vis[v]){
vis[v]=1;
q.push(v);
}
}
}
}

}

int main(){
memset(head,-1,sizeof(head));
int i,x,y,z;
scanf("%d%d%d",&n,&m,&s);
for(i=1;i<=n;i++)
if(i!=s)dist[i]=INF;

for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
adde(x,y,z);
}

spfa(s);
for(i=1;i<=n;i++)
printf("%lld ",dist[i]);

return 0;
}

手打版(没什么区别):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int INF=2147483647;
const int MAXN=10005;
const int MAXM=500005;
const int MAX=500000;

struct edge{
int next,to;
ll w;
};
struct edge list[MAXM];
int n,m,s,nume=-1;

ll dist[MAXN]={0};
bool vis[MAXN]={0};
int head[MAXM]={0};
int queue[MAXM]={0},tl=1,hd=1;

inline void adde(int f,int t,int w){
list[++nume].next=head[f];
list[nume].to=t;
list[nume].w=w;
head[f]=nume;
}

inline int front(){
return queue[hd];
}

inline void pop(){
hd=hd%MAX+1;
}

inline int empty(){
if((tl+1) %MAX == hd )return 1;
return 0;
}

inline void push(int x){
queue[tl]=x;
tl=tl%MAX+1;
}

void spfa(int s){
int i,u,v;
for(i=1;i<=n;i++)
dist[i]=INF;
push(s);
dist[s]=0;
vis[s]=1;
while(!empty()){
u=front();
pop();
vis[u]=0;
for(i=head[u]; i!=-1; i=list[i].next){
v=list[i].to;
if(dist[v]>dist[u]+list[i].w){
dist[v]=dist[u]+list[i].w;
if(!vis[v]){
vis[v]=1;
push(v);
}
}
}
}

}

int main(){
memset(head,-1,sizeof(head));
int i,x,y,z;
scanf("%d%d%d",&n,&m,&s);
for(i=1;i<=n;i++)
if(i!=s)dist[i]=INF;

for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
adde(x,y,z);
}

spfa(s);
for(i=1;i<=n;i++)
printf("%lld ",dist[i]);

return 0;
}