图论模板(拓扑排序、最短路)

零、链式前向星

下面很多模板都是默认用链式前向星存图的。

代码

const int N = 100010, M = 1000010; //点数和边数
int h[N], e[M], w[M], ne[M], idx; //头节点,节点编号,边权,下一个节点

void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
//初始化
memset(h, -1, sizeof h);
idx = 0;

一、拓扑排序

题意

输出拓扑排序,如果不存在输出-1.

代码

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

二、Dijkstra

题意

求单源最短路(不存在负边权),不存在返回-1

代码1(朴素版)

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

代码2(堆优化版)

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

三、Bellman-Ford

题意

存在负边权的图,边数不超过 k k k的最短路

代码

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

四、spfa

题意1(最短路)

求带负边权的最短路

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

题意2(判负环)

判负环

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

五、Floyd

题意

多源汇最短路

代码

#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;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值