关于SPFA
【图论】—— Bellman-Ford算法和SPFA算法_玄澈_的博客-CSDN博客
负环
给定一张有向图(无向图的每条边可以看做是两条方向相反的有向边,从而按照有向图处理),每条边都有一个权值(长度)。若一条边的长度是负数,则称它是负权边。若存在一个环,环上各边的权值之和是负数,则称这个环为“负环”。
算法名称 | 能否处理负权边 | 时间复杂度 |
---|---|---|
Dijlstra | 不能,负权可能造成当前最小的dist[x]以后不一定最小 | |
Heap-Dijkstra | 不能,该算法仅当每一个点第一次从堆中取出时执行,等价于选最小的dist[x] | |
Bellman-ford | 能,无负环时,最短路包含的边数 < n, n - 1 轮迭代后一定收敛 | |
SPFA | 能,本质是队列优化的Bellman-ford,负环只会增加入队次数 |
如果图中存在负环,那么直观的表现是:无论经历多少轮迭代,总存在有向边 使得 , BL算法和SPFA算法永远都不能结束。
根据抽屉原理,若存在一个 ,从起点1到结点x的最短路包含 条边,则这条路径必然重复经过了某个 节点p 。换言之,这条最短路上存在一个环,环上各点的值都能更新下一个点的 dist 值。 p 绕这条环一圈,最终能更新它自己。因此,这个环的长度是负数。每饶一圈,最短路的长度只会越来越小,不可能收敛到每条边都满足三角不等式的状态。
SPFA判负环
设 表示从1到 x 的最短路径所包含的边数, 。当执行更新 时, 同样更新 。若此时发现 ,则图中有负环。若算法正常结束,则图中没有负环。
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e6 + 10;
int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N],cnt[N];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
bool spfa()
{
memset(dist, 0, sizeof dist);
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
queue<int> q;
for(int i = 1; i <= n; i ++ )
{
q.push(i);
st[i] = true;
}
while(q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if(cnt[j] >= n) return true;
if(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
if(spfa()) puts("Yes");
else puts("No");
return 0;
}
01分数规划
例题:AcWing 361. 观光奶牛
给定一张 LL 个点、PP 条边的有向图,每个点都有一个权值 f[i]f[i],每条边都有一个权值 t[i]t[i]。
求图中的一个环,使“环上各点的权值之和”除以“环上各边的权值之和”最大。
输出这个最大值。
注意:数据保证至少存在一个环。
输入格式
第一行包含两个整数 LL 和 PP。
接下来 LL 行每行一个整数,表示 f[i]f[i]。
再接下来 PP 行,每行三个整数 a,b,t[i]a,b,t[i],表示点 aa 和 bb 之间存在一条边,边的权值为 t[i]t[i]。
输出格式
输出一个数表示结果,保留两位小数。
数据范围
2≤L≤10002≤L≤1000,
2≤P≤50002≤P≤5000,
1≤f[i],t[i]≤10001≤f[i],t[i]≤1000输入样例:
5 7 30 10 10 5 10 1 2 3 2 3 2 3 4 5 3 5 2 4 5 5 5 1 3 5 2 2
输出样例:
6.00
解题思路:
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1010, M = 5010;
int n, m;
int wf[N];
int h[N], e[M], wt[M], ne[M], idx;
double dist[N];
bool st[N];
int cnt[N];
void add(int a, int b, int c)
{
e[idx] = b, ne[idx] = h[a], wt[idx] = c, h[a] = idx ++ ;
}
bool check(double mid)
{
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
queue<int> q;
for(int i = 1; i <= n; i ++ ){
q.push(i);
st[i] = true;
}
while(q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if(dist[j] < dist[t] + wf[t] - mid * wt[i])
{
dist[j] = dist[t] + wf[t] - mid * wt[i];
cnt[j] = cnt[t] + 1;
if(cnt[j] >= n) return true;
if(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
return false;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
for(int i = 1; i <= n; i ++ ) cin >> wf[i];
while(m -- )
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
double l = 0, r = 1010;
while(r - l > 1e-4)
{
double mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
printf("%.2lf", r);
return 0;
}