图论和搜索

图论和搜索

第二课

算法总览

  1. 朴素版的Dijkstra算法,针对n(点)和m(边)的平方大小差不多的情况,这个时候利用邻接矩阵去存储Acwing 849

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4VvppV6g-1602509388606)(C:\Users\dj\Desktop\1602503227(1)].png)

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

using namespace std;

const int N = 510;

int n, m;

int dist[N];
int g[N][N];
bool st[N];

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    
    memset(g, 0x3f, sizeof g);
    while (m --){
        int a, b, c;
        cin >> a >> b >> c;
        g[a][b] = min (g[a][b], c);
    }
    
    int t = dijkstra();
    
    cout << t << endl;
    
    return 0;
}
  1. 堆优化版的Dijkstra算法,这里n和m差不多,稀疏图用邻接表存储 Acwing 850

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E5T40FTi-1602509388607)(C:\Users\dj\Desktop\1602506551(1)].png)

#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];
bool st[N];

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

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 != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[ver] + w[i])
            {
                dist[j] = dist[ver] + w[i];
                heap.push({dist[j], j});
            }
        }
    }

    if (dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

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

    cout << dijkstra() << endl;

    return 0;
}
  1. Bellman-Fords算法
    在这里插入图片描述
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 510, M = 10010;

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

struct Edge{
    int a, b, w;
}edges[M];

void bellman_ford()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    for (int i = 0; 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.w);
        }
    }
}


int main()
{
    scanf ("%d%d%d", &n, &m, &k);
    
    for (int i = 0; i < m; i ++)
    {
        int a, b, w;
        scanf("%d%d%d", &a, &b, &w);
        edges[i] = {a, b, w};
    }
    bellman_ford();
    
    if (dist[n] > 0x3f3f3f3f / 2) puts("impossible");
    else cout << dist[n] << endl;
    return 0;
}

  1. spfa求最短路
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>

using namespace std;

const int N = 2e5 + 10;

typedef pair<int, int> PII;

int h[N], e[N], ne[N], w[N], idx;
int n, m;
int dist[N];
bool st[N];

void add (int a, int b, int c){
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}

int spfa()
{
    queue <int> q;
    memset (dist, 0x3f, sizeof dist);
    dist[1] = 0;
    q.push(1);
    st[1] = true;
    
    while (q.size()){
        auto t = q.front();
        q.pop();
        
        st[t] = false;
        
        for (int i = h[t]; i != -1; i = ne[i]){
            auto j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                if (!st[j]){
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
    
    if (dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}


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);
    }
    
    int t = spfa();
    if (t == -1) puts("impossible");
    else printf ("%d", t);
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值