Graph theory template (topological sort, shortest path)

One, topological sort

Title

Output topological sort, if there is no output -1.

Code

void topsort()
{
    
    
    queue<int> que;
    for(int i=1;i<=n;i++)
        if(!din[i]) que.push(i), ans[++num] = i;
    
    while(que.size()){
    
    
        int t = que.front();
        que.pop();
        for(int i=h[t];~i;i=ne[i]){
    
    
            int j = e[i];
            if(--din[j]==0) que.push(j), ans[++num] = j;
        }
    }
    if(num<n){
    
    
        cout << -1;
        return;
    }
    for(int i=1;i<=num;i++) cout << ans[i] << ' ';
}

Two, Dijkstra

Title

Find the single source shortest path (there is no negative side weight), if there is no negative side weight, return -1

Code 1 (Naive Version)

int dijkstra()
{
    
    
    memset(dist,0x3f,sizeof(dist));
    dist[1] = 0;
    for(int i=1;i<=n;i++){
    
    
        int t = -1;
        for(int j=1;j<=n;j++){
    
    
            if(!st[j]&&(t==-1||dist[t]>dist[j])){
    
    
                t = j;
            }
        }
        for(int j=1;j<=n;j++){
    
    
            dist[j] = min(dist[j],dist[t]+g[t][j]);
        }
        st[t] = true;
    }
    if(dist[n]==0x3f3f3f3f) return -1;
    return dist[n];
}

Code 2 (heap optimized version)

int dijkstra()
{
    
    
    memset(dist,0x3f,sizeof(dist));
    dist[1] = 0;
    priority_queue<pii,vector<pii>,greater<pii>> heap;
    heap.push({
    
    0,1});
    while(heap.size()){
    
    
        auto t = heap.top();
        heap.pop();
        int ver = t.second, distance = t.first;
        if(st[ver]) continue;
        st[ver] = true;
        for(int i=h[ver];~i;i=ne[i]){
    
    
            int j = e[i];
            if(dist[j]>distance+w[i]){
    
    
                dist[j] = distance + w[i];
                heap.push({
    
    dist[j],j});
            }
        }
    }
    if(dist[n]==0x3f3f3f3f) return -1;
    return dist[n];
}

Three, Bellman-Ford

Title

Graphs with negative edge weights, the number of edges does not exceed kkshortest path of k

Code

int n,m,k;
int dist[N];
int last[N];
struct Edge
{
    
    
    int a,b,c;
}edges[M];

void bellman_ford()
{
    
    
    memset(dist,0x3f,sizeof(dist));
    dist[1] = 0;
    for(int i=1;i<=k;i++){
    
    
        memcpy(last,dist,sizeof(dist));
        for(int j=0;j<m;j++){
    
    
            auto e = edges[j];
            dist[e.b] = min(dist[e.b], last[e.a] + e.c);
        }
    }
    if(dist[n]>0x3f3f3f3f/2) puts("impossible");
    else cout << dist[n] << endl;
}

Four, spfa

Question 1 (shortest path)

Find the shortest path with negative edge weight

Code 1

int spfa()
{
    
    
    memset(dist,0x3f,sizeof(dist));
    dist[1] = 0;
    queue<int> que;
    que.push(1);
    st[1] = true;
    while(que.size()){
    
    
        int t = que.front();
        que.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];
                if(!st[j]){
    
    
                    que.push(j);
                    st[j] = true;
                }
            }
        }
    }
    return dist[n];
}

Question 2 (negative ring)

Negative ring

Code 2

bool spfa()
{
    
    
    queue<int> que;
    for(int i=1;i<=n;i++){
    
    
        que.push(i);
        st[i] = true;
    }
    while(que.size()){
    
    
        int t = que.front();
        que.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]){
    
    
                    que.push(j);
                    st[j] = true;
                }
            }
        }
    }
    return false;
}

Five, Floyd

Title

Multi-source sink shortest path

Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 210, inf = 0x3f3f3f3f;

int n,m,k;
int dist[N][N];

void floyd()
{
    
    
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
}

int main()
{
    
    
    cin >> n >> m >> k;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
    
    
            if(i==j) dist[i][j] = 0;
            else dist[i][j] = inf;
        }
    while(m--){
    
    
        int a,b,c;
        cin >> a >> b >> c;
        dist[a][b] = min(dist[a][b], c);
    }
    floyd();
    while(k--){
    
    
        int a,b;
        cin >> a >> b;
        int t = dist[a][b];
        if(t>inf/2) puts("impossible");
        else cout << t << endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43634220/article/details/108512670