SPFA
Shortest Path Faster Algorithm,即队列优化的Bellman-Ford算法,最坏情况下时间复杂度
O
(
n
m
)
O(nm)
O(nm),好像只能在负权图里用,不然会被卡死。
例题:P3371 【模板】单源最短路径(弱化版)
就是BFS的原理
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 9;
inline int read()
{
int data = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
{
data = (data << 3) + (data << 1) + ch - '0';
ch = getchar();
}
return f * data;
}
int cnt_e;
const int maxm = 5e5 + 9;
struct edge
{
int next, to, dis;
} e[maxm];
int dis[maxn], vis[maxn], head[maxm];
void add(int from, int to, int dis)
{
e[++cnt_e].next = head[from];
e[cnt_e].to = to;
e[cnt_e].dis = dis;
head[from] = cnt_e;
}
int n, m, s;
void spfa()
{
queue<int> q;
for (int i = 1; i <= n; ++i)
{
dis[i] = INF;
vis[i] = 0;
}
q.push(s);
dis[s] = 0, vis[s] = 1;
while (!q.empty())
{
int u = q.front();
q.pop(), 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].dis)
{
dis[v] = dis[u] + e[i].dis;
if (!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("data.txt","w",stdout);
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
n = read(), m = read(), s = read();
while (m--)
{
int a = read(), b = read(), c = read();
add(a, b, c);
}
spfa();
for (int i = 1; i <= n; ++i)
cout << dis[i] << ' ';
//fclose(stdin);
//fclose(stdout);
return 0;
}
链式前向星存的图,原理就是头插法的静态链表。
负环
SPFA判负环,如果存在负环就会一直松弛下去,所以当到某个点的最短路经过了不少于
n
n
n 个点,那说明一定出现了负环。
例题:P3385 【模板】负环
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;
inline int read()
{
int data = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
{
data = (data << 3) + (data << 1) + ch - '0';
ch = getchar();
}
return f * data;
}
const int maxn = 2e3 + 10;
const int maxm = 6e3 + 10;
int n, m, head[maxn], tot, dis[maxn], vis[maxn], cnt[maxn];
struct edge
{
int to, w, next;
} e[maxm];
queue<int> q;
void reset()
{
for (int i = 0; i < maxm; i++)
e[i].next = 0;
for (int i = 0; i < maxn; i++)
head[i] = 0;
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
while (!q.empty())
q.pop();
tot = 0;
}
void add(int u, int v, int w)
{
e[++tot].to = v;
e[tot].w = w;
e[tot].next = head[u];
head[u] = tot;
}
bool spfa()
{
dis[1] = 0, vis[1] = true;
q.push(1);
while (!q.empty())
{
int x = q.front();
q.pop();
vis[x] = false;
for (int i = head[x]; i; i = e[i].next)
{
int y = e[i].to, z = e[i].w;
if (dis[y] > dis[x] + z)
{
dis[y] = dis[x] + z;
cnt[y] = cnt[x] + 1;
if (cnt[y] >= n)
return true;
if (!vis[y])
{
q.push(y);
vis[y] = true;
}
}
}
}
return false;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("data.txt","w",stdout);
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
int T = read();
while (T--)
{
reset();
n = read(), m = read();
while (m--)
{
int u = read(), v = read(), w = read();
add(u, v, w);
if (w >= 0)
add(v, u, w);
}
puts(spfa() ? "YES" : "NO");
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
图论要学的新知识实在是太多了