Plantilla de teoría de grafos (clasificación topológica, ruta más corta)

Uno, tipo topológico

Título

Ordenación topológica de salida, si no hay salida -1.

Código

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] << ' ';
}

Dos, Dijkstra

Título

Encuentre la ruta más corta de una sola fuente (no hay peso del lado negativo), si no hay un peso del lado negativo, devuelva -1

Código 1 (versión ingenua)

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];
}

Código 2 (versión optimizada del montón)

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];
}

Tres, Bellman-Ford

Título

Gráficos con pesos de aristas negativos, el número de aristas no excede kkcamino más corto de k

Código

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;
}

Cuatro, spfa

Pregunta 1 (camino más corto)

Encuentre el camino más corto con un peso de borde negativo

Codigo 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];
}

Pregunta 2 (anillo negativo)

Anillo negativo

Codigo 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;
}

Cinco, Floyd

Título

El camino más corto del sumidero de múltiples fuentes

Código

#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;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_43634220/article/details/108512670
Recomendado
Clasificación