最短路

dijkstra

特点
单源最短路,全是正权边。时间复杂度O(m logn)。

代码

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
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define endl "\n"
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define debug(a) cout<<"\tdebug:"<<a<<endl
#define for1(i, a, b) for(int i = a; i <= b; i++)
#define for2(i, b, a) for(int i = b; i >= a; i--)
const double PI = acos(-1.0);
const int mod = 998244353;
const int eps = 1e-8;
const int N = 10 + 1e5;

void slove();
template<typename T>void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-') f *= -1; ch = getchar();}
while(isdigit(ch)){x = x*10+ch-48; ch = getchar();} x *= f;
}
template<typename T>void print(T x)
{
if(x < 0) putchar('-'), x = -x;
if(x >= 10) print(x/10);
putchar(x % 10 + '0');
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
IOS;
slove();
return 0;
}

int n, m, tot, s, t;
int vis[N], head[N], dis[N];

struct edge
{
int to, next, val;
}e[N];

struct node
{
int to, val;
bool operator < (const node&b) const
{
return val > b.val;
}
};

void add(int u, int v, int val)
{
e[tot].to = v; e[tot].val = val; e[tot].next = head[u]; head[u] = tot++;
}

void dijkstra()
{
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
priority_queue<node> q;
q.push(node{s, 1});

while(!q.empty())
{
node x = q.top(); q.pop();
int u = x.to;
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if(dis[v] > dis[u] + e[i].val)
{
dis[v] = dis[u]+e[i].val;
q.push(node{v, dis[v]});
}
}
}
}

void slove()
{
memset(head, -1, sizeof head);
cin>>n>>m>>s>>t;
while(m--)
{
int u, v, w; cin>>u>>v>>w;
add(u, v, w);
add(v, u, w);
}
dijkstra();
cout<<dis[t]<<endl;
}

SPFA

特点
单源最短路,可以有负权边,不能有负环。时间复杂度:最好O(m) 最坏O(mn)。

代码

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
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define endl "\n"
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define debug(a) cout<<"\tdebug:"<<a<<endl
#define for1(i, a, b) for(int i = a; i <= b; i++)
#define for2(i, b, a) for(int i = b; i >= a; i--)
const double PI = acos(-1.0);
const int mod = 998244353;
const int eps = 1e-8;
const int N = 10 + 1e6;

void slove();
template<typename T>void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-') f *= -1; ch = getchar();}
while(isdigit(ch)){x = x*10+ch-48; ch = getchar();} x *= f;
}
template<typename T>void print(T x)
{
if(x < 0) putchar('-'), x = -x;
if(x >= 10) print(x/10);
putchar(x % 10 + '0');
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
IOS;
slove();
return 0;
}

int n, m, tot, s, t;
int vis[N], head[N], dis[N];

struct edge
{
int to, next, val;
}e[N];


void add(int u, int v, int val)
{
e[tot].to = v; e[tot].val = val; e[tot].next = head[u]; head[u] = tot++;
}

void spfa()
{
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;

queue<pii> q;
q.push(pii{s, 0});
vis[s] = 1;

while(!q.empty())
{
pii x = q.front(); q.pop();
int u = x.first;
vis[u] = 0;
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if(dis[v] > dis[u] + e[i].val)
{
dis[v] = dis[u]+e[i].val;
if(!vis[v]) vis[v] = 1, q.push(pii{v, dis[v]});
}
}
}
if(dis[t] == inf) cout<<"impossible"<<endl;
else cout<<dis[t]<<endl;
}

void slove()
{
memset(head, -1, sizeof head);
cin>>n>>m;
s = 1;
t = n;
while(m--)
{
int u, v, w; cin>>u>>v>>w;
add(u, v, w);
}
spfa();
}

负环

SPFA

特点
单源最短路,可存在负权,时间复杂度:最优O(m) 最坏O(mn)

代码

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
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define endl "\n"
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define debug(a) cout<<"\tdebug:"<<a<<endl
#define for1(i, a, b) for(int i = a; i <= b; i++)
#define for2(i, b, a) for(int i = b; i >= a; i--)
const double PI = acos(-1.0);
const int mod = 998244353;
const int eps = 1e-8;
const int N = 10 + 1e6;

void slove();
template<typename T>void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-') f *= -1; ch = getchar();}
while(isdigit(ch)){x = x*10+ch-48; ch = getchar();} x *= f;
}
template<typename T>void print(T x)
{
if(x < 0) putchar('-'), x = -x;
if(x >= 10) print(x/10);
putchar(x % 10 + '0');
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
IOS;
slove();
return 0;
}

int n, m, tot, s, t;
int vis[N], head[N], dis[N], cnt[N];

struct edge
{
int to, next, val;
}e[N];


void add(int u, int v, int val)
{
e[tot].to = v; e[tot].val = val; e[tot].next = head[u]; head[u] = tot++;
}

int spfa()
{
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;

queue<pii> q; // second一般没用
for(int i = 1; i <= n; i++)
{
vis[i] = 1;
q.push(pii{i, 0});
}

while(!q.empty())
{
pii x = q.front(); q.pop();
int u = x.first;
vis[u] = 0;
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if(dis[v] > dis[u] + e[i].val)
{
dis[v] = dis[u]+e[i].val;
cnt[v] = cnt[u]+1;
if(cnt[v] >= n) return 1; // 有负环
if(!vis[v]) vis[v] = 1, q.push(pii{v, dis[v]});
}
}
}
return 0;
}

void slove()
{
memset(head, -1, sizeof head);
cin>>n>>m;
s = 1;
t = n;
while(m--)
{
int u, v, w; cin>>u>>v>>w;
add(u, v, w);
}
int ans = spfa();
if(ans) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}