12.3 负环、负边权变正边权总结

· · 个人记录

前置知识

正负环

spfa 能在最长路判正环,最短路判负环

如果这张图里有负环,最短路 spfa 会出现什么情况? 对,会死循环,一直停在那几个形成负环的点上。
那我们就给更新一个限度,如果一个点被更新的次数超过了这个次数,就判为有负环。但多少合适呢?
是 n 次,我们可以把每一层松弛看作在原路线的基础上多加了一个点,最多有 n 个点,超过 n 个点就说明他至少到一个点两次,即形成了一个环。这个环的边权总和一定小于 0 ,不然其重复更新的那个点不可能会变得更小。

Code

bool spfa(){
    queue<ll> q;
    for(int i = 1;i <= n;i++){
        dis[i] = 1e18;
        vis[i] = 0;
        in[i] = 0;
    }
    q.push(s);
    dis[s] = 0;
    vis[s] = 1;
    while(q.size() > 0){
        ll x = q.front();
        q.pop();
        vis[x] = 0;
        for(auto nx:G[x]){
            if(dis[nx.e] > dis[x] + nx.x){
                dis[nx.e] = dis[x] + nx.x;
                if(vis[nx.e] == 0){
                    vis[nx.e] = 1;
                    in[nx.e]++;
                    if(in[nx.e] == n) return 1;
                    q.push(nx.e);
                }
            }
        }
    }
    return 0;
}

Johnson

一种全源最短路算法。
参考:

Johnson 算法是一种能够在 O(nm log m) 的时间内求出全员最短路的算法,在稀疏图中比 Floyd 快。

我们写过一道题,叫做 公路通行税 我们就可以用 n 遍 Dij 过掉。 Johnson 算法两个核心第二个就是用 n 次 Dij 来求出全源最短路。

但是某些毒瘤题会给你负边权,这时候又该怎么办呢?我们就想办法把边权变为正的同时不影响答案。

如果直接加数的话,可以看看下面: 我们用一个超级原点 0 ,链接所有的点。接着用 spfa 求出从 0 到其他所有点的最短路。从 0 到 i 点的最短路设为 h[i]
若一条边边权原本为 w 起点和终点分别为 s 和 e,现在把它改为 w + h[s] - h[e]
因为 h[i] 是从 0 到 i 点的最短路,所以一定有 h[s] + w >= h[e] ,将 h[e] 移到左边去,得到 w + h[s] - h[e] >= 0 ,即新边权非负 。
接下来可以放心大胆的跑 Dij 了,注意跑完之后的结果还要加上 h[e] - h[s]

P10110 [GESP202312 七级] 商品交易 - 洛谷 | 计算机科学教育新生态

典型的负边权变正边权。

首先,很明显,这一题如果用 SPFA ,就很有可能超时( O(nm) 会 TLE )。那么只能用 Dij 或者某种奇奇怪怪的算法。
但是 Dij 无法处理负边权啊。这个时候就要分析一下题目。

假设我要拿 i 换 j :

若我的交换过程是这样的: A[1]->A[2]->A[3]->A[4]->...->A[n]
则我亏了:

 (A[2] - A[1] + 1) + (A[3] - A[2] + 1) + ... + (A[n] - A[n - 1] + 1)
= A[n] - A[1] + (n - 1)

最后我亏的钱中,A[n] - A[1] 是固定的,只有 n - 1 不固定,所以只要交换的次数最少,我亏的钱就越少。
此时跑一遍 Dij/BFS 即可

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
struct node{
    ll id,x;
    bool operator < (const node& a) const{
        return a.x < x;
    }
};
ll n,m,a,b,v[100005],dis[100005];
vector<ll> G[100005];
bool vis[100005];

void dij(){
    for(int i = 1;i <= n;i++){
        dis[i] = 1e18;
        vis[i] = 0;
    }
    priority_queue<node> pq;
    pq.push({a,0});
    dis[a] = 0;
    while(pq.size() > 0){
        node x = pq.top();
        pq.pop();
        if(vis[x.id] == 1) continue;
        vis[x.id] = 1;
        for(auto nx:G[x.id]){
            if(vis[nx] == 0 && dis[nx] > dis[x.id] + 1){
                dis[nx] = dis[x.id] + 1;
                pq.push({nx,dis[nx]});
            }
        }
    }
    return ;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin >> n >> m >> a >> b;
    a++,b++;
    for(int i = 1;i <= n;i++) cin >> v[i];
    for(int i = 1;i <= m;i++){
        ll s,e;
        cin >> s >> e;
        s++,e++;
        G[s].push_back(e);
    }
    dij();
    if(dis[b] == 1e18) cout << "No solution";
    else cout << dis[b] + v[b] - v[a];
    return 0;
}

主要是想办法把边权抵消,抵消到答案是一个定值加上一个变值(变值的求法能够使用时间复杂度可以过的算法)

[ABC237E] Skiing - 洛谷 | 计算机科学教育新生态

这一题和上一题一样的,直接跑 SPFA 会炸......

继续分析一下,虽然说这一题也有减法,但是不能够像上次一样消消乐。
那我们能不能构造出来一个和上一题差不多的?还真能! 我们对所有边权减去一个 (H_X - H_Y) ,边权变成下面了:

- H_X > H_Y 时,幸福感会增加 0
- H_X < H_Y 时,幸福感会增加 H_X - H_Y
- H_X < H_Y 时,幸福感会增加 -(H_X - H_Y) , 即 0

这样这个边权就可以抵消了,减去的 (H_X - H_Y) 仍然可以抵消。
问题是这个边权仍然是负的啊。没关系,我们要求的幸福值要求最大,那么这些负的就要最大,他们的绝对值就要最小!我们把它转化为正的跑 Dij 最短路就可以了。

至于减去的,后面需要加上,会发现这是个定值 H[起点] - H[终点]

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
struct node{
    ll id,x;
    bool operator < (const node& a) const{
        return a.x < x;
    }
};
struct edge{
    ll e,x;
};
ll n,m,h[200005],dis[200005];
vector<edge> G[200005];
bool vis[200005];

void dij(){
    for(int i = 1;i <= n;i++){
        dis[i] = 1e18;
        vis[i] = 0;
    }
    priority_queue<node> pq;
    pq.push({1,0});
    dis[1] = 0;
    while(pq.size() > 0){
        node x = pq.top();
        pq.pop();
        if(vis[x.id] == 1) continue;
        vis[x.id] = 1;
        for(auto nx:G[x.id]){
            if(vis[nx.e] == 0 && dis[nx.e] > dis[x.id] + nx.x){
                dis[nx.e] = dis[x.id] + nx.x;
                pq.push({nx.e,dis[nx.e]});
            }
        }
    }
    return ;
}

int main(){
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        cin >> h[i];
    }
    for(int i = 1;i <= m;i++){
        ll s,e;
        cin >> s >> e;
        if(h[s] > h[e]){
            G[s].push_back({e,0});
            G[e].push_back({s,h[s] - h[e]});
        }else{
            G[s].push_back({e,h[e] - h[s]});
            G[e].push_back({s,0});
        }
    }
    dij();
    ll ans = -1e18;
    for(int i = 1;i <= n;i++){
        ans = max(ans,h[1] - h[i] - dis[i]);
    }
    cout << ans;
    return 0;
}

当题目没给时,想办法自己构造。

P3385 【模板】负环 - 洛谷 | 计算机科学教育新生态

负环模板,不多讲
多组数据,记得清空

Code


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
struct node{
ll e,x;
};
ll n,m,s,dis[2005],in[2005];
vector<node> G[2005];
bool vis[2005];

bool spfa(){ queue<ll> q; for(int i = 1;i <= n;i++){ dis[i] = 1e18; vis[i] = 0; in[i] = 0; } q.push(s); dis[s] = 0; vis[s] = 1; while(q.size() > 0){ ll x = q.front(); q.pop(); vis[x] = 0; for(auto nx:G[x]){ if(dis[nx.e] > dis[x] + nx.x){ dis[nx.e] = dis[x] + nx.x; if(vis[nx.e] == 0){ vis[nx.e] = 1; in[nx.e]++; if(in[nx.e] == n) return 1; q.push(nx.e); } } } } return 0; }

int main(){ ll T; cin >> T; while(T--){ cin >> n >> m; s = 1; for(int i = 1;i <= m;i++){ ll s,e,x; cin >> s >> e >> x; if(x < 0) G[s].push_back({e,x}); else G[s].push_back({e,x}),G[e].push_back({s,x}); } if(spfa()){ cout << "YES\n"; }else{ cout << "NO\n"; } for(int i = 1;i <= n;i++){ G[i].clear(); } } return 0; }

## [P1931 套利 - 洛谷 | 计算机科学教育新生态](https://www.luogu.com.cn/problem/P1931)
暴力枚举每一个货币,尝试从他开始有没有正环(乘法)  
多组数据,记得清空
### Code
```cpp
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <map>
using namespace std;
typedef long long ll;
struct edge{
    ll e;
    double x;
};
ll n,m,cnt,in[35];
double dis[35];
map<string,ll> id;
vector<edge> G[35];
bool vis[35];

bool spfa(ll s){
    for(int i = 1;i <= n;i++){
        dis[i] = 0;
        vis[i] = 0;
        in[i] = 0;
    }
    queue<ll> q;
    q.push(s);
    dis[s] = 1;
    vis[s] = 1;
    while(q.size() > 0){
        ll x = q.front();
        q.pop();
        vis[x] = 0;
        for(auto nx:G[x]){
            if(dis[nx.e] < dis[x] * nx.x){
                dis[nx.e] = dis[x] * nx.x;
                // cout << nx.e << " " << dis[nx.e] << " " << x << "\n";
                if(vis[nx.e] == 0){
                    vis[nx.e] = 1;
                    q.push(nx.e);
                    in[nx.e]++;
                    if(in[nx.e] == n) return 1;
                }
            }
        }
    }
    return 0;
}

int main(){
    ll T = 0;
    while(1){
        T++;
        cnt = 0;
        id.clear();
        cin >> n;
        if(n == 0) return 0;
        for(int i = 1;i <= n;i++){
            string x;
            cin >> x;
            id[x] = ++cnt;
            G[i].clear();
        }
        cin >> m;
        for(int i = 1;i <= m;i++){
            string S,E;
            ll s,e;
            double x;
            cin >> S >> x >> E;
            s = id[S],e = id[E];
            G[s].push_back({e,x});
        }
        bool f = 1;
        for(int i = 1;i <= n;i++){
            if(spfa(i)){
                cout << "Case "<< T << ": " << "Yes\n";
                f = 0;
                break;
            }
        }
        if(f) cout << "Case "<< T << ": " << "No\n";
    }
    return 0;
}

P5905 【模板】全源最短路(Johnson) - 洛谷 | 计算机科学教育新生态

模板 Johnson ,只不过在跑 spfa 时还要判下负环。

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
struct edge{
    ll e,x;
};
struct node{
    ll id,x;
    bool operator < (const node& a) const{
        return a.x < x;
    }
};
ll n,m,h[3005],dis[3005][3005],in[3005];
vector<edge> G[3005];
bool vis[3005];

bool spfa(){
    for(ll i = 1;i <= n;i++){
        h[i] = 1e9;
    }
    queue<ll> q;
    q.push(0);
    h[0] = 0;
    vis[0] = 1;
    while(q.size() > 0){
        ll x = q.front();
        q.pop();
        vis[x] = 0;
        for(auto nx:G[x]){
            if(h[nx.e] > h[x] + nx.x){
                h[nx.e] = h[x] + nx.x;
                if(vis[nx.e] == 0){
                    vis[nx.e] = 1;
                    q.push(nx.e);
                    in[nx.e]++;
                    if(in[nx.e] == n) return 1;
                }
            }
        }
    }
    return 0;
}

void dij(ll s){
    for(ll i = 1;i <= n;i++){
        dis[s][i] = 1e18;
        vis[i] = 0;
    }
    priority_queue<node> pq;
    pq.push({s,0});
    dis[s][s] = 0;
    while(pq.size() > 0){
        node x = pq.top();
        pq.pop();
        if(vis[x.id] == 1) continue;
        vis[x.id] = 1;
        for(auto nx:G[x.id]){
            if(vis[nx.e] == 0 && dis[s][nx.e] > dis[s][x.id] + nx.x){
                dis[s][nx.e] = dis[s][x.id] + nx.x;
                pq.push({nx.e,dis[s][nx.e]});
            }
        }
    }
    return ;
}

int main(){
    cin >> n >> m;
    for(ll i = 1;i <= m;i++){
        ll s,e,x;
        cin >> s >> e >> x;
        G[s].push_back({e,x});
        // G[e].push_back({s,x});
    }
    for(ll i = 1;i <= n;i++){
        G[0].push_back({i,0});
        // G[i].push_back({0,0});
    }
    if(spfa()){
        cout << "-1";
        return 0;
    }
    for(ll i = 1;i <= n;i++){
        for(auto &nx:G[i]){
            nx.x += (h[i] - h[nx.e]);
        }
    }
    for(ll i = 1;i <= n;i++){
        dij(i);
    }
    for(ll i = 1;i <= n;i++){
        ll ans = 0;
        for(ll j = 1;j <= n;j++){
            if(dis[i][j] == 1e18){
                ans += j * ll(1e9);
            }else{
                ans += j * ll(dis[i][j] + h[j] - h[i]);
            }
        }
        cout << ans << "\n";
    }
    return 0;
}