Dijkstra

朴素Dijkstra--O(n2)

题目链接
https://www.acwing.com/problem/content/851/

Ac代码

点击查看代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 510;

int g[N][N];
int n, m;
int dist[N];
bool st[N];

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] && (dist[t] > dist[j] || t == -1)){
                t = j;
            }
        }
		//用这个点更新其他点的距离
        for(int j = 1; j <= n; j ++){
            if(dist[j] > dist[t] +g[t][j]){
                dist[j] = dist[t] + g[t][j];
            }
        }
		//将这个点加入集合
        st[t] = true;
    }
    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    scanf("%d%d", &n, &m);
    memset(g, 0x3f, sizeof g);
    while(m --){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        g[a][b] = min(g[a][b], c);
    }
    printf("%d\n", dijkstra());
    
    return 0;
}


堆优化版的Dijkstra--O(mlogn)

题目链接
https://www.acwing.com/problem/content/852/

Ac代码

点击查看代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> PII;

const int N = 2e5 + 10;

int n, m;
int h[N], e[N], ne[N], w[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.empty()){
        PII t = heap.top();
        heap.pop();
        
		//找到不在集合中的最近的点,将它加入集合
		//由于PII默认按照第一维排序,所以PII的第一维存distance
        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] > distance + w[i]){
                dist[j] = distance + 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);
    }
    printf("%d\n", dijkstra());
    
    return 0;
}


势能Dijkstra

势能Dijkstra用来处理有负权边的最短路,注意也可以处理最长路,因为最长路问题可以将边权取负后转化为最短路,放两篇比较好的题解:
https://zhuanlan.zhihu.com/p/470948347
https://www.cnblogs.com/miraclepbc/p/16007565.html
重点在于边权的转化方法:
image

ABC 237E
题目链接
https://atcoder.jp/contests/abc237/tasks/abc237_e

解析

  • 将最长路问题取负变成最短路问题
  • 对负后的图添加势能得到势能图
  • 验证势能图的边权非负
  • 跑dijkstra,并统计答案
    在做题的时候遇到了一些问题:
  • 没有初始化memset(h, -1, sizeof h)
  • 双向边,数组没有开二倍
  • 还把堆优化版的dijkstra写错了,开pii是为了第一维是distance,每次O(1)地取出最近的点

Ac代码

点击查看代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int N = 2e5 + 10, M = 2 * N;

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

ll calc(int x, int y){
    if(hh[x] >= hh[y]) return 0;
    return hh[y] - hh[x];
}

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

void 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] > distance + w[i]){
                dist[j] = distance + w[i];
                heap.push({dist[j], j});
            }
        }
    }
    return;
}

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i ++) scanf("%lld", &hh[i]);
    memset(h, -1, sizeof h);
    while(m --)
    {
        int x, y, z;
        scanf("%d%d", &x, &y);
        add(x, y, calc(x, y));
        add(y, x, calc(y, x));
    }

    dijkstra();

    ll ans = -0x3f3f3f3f;
    for(int i = 1; i <= n; i ++){
        if(dist[i] == 0x3f3f3f3f) continue;
        ans = max(ans, hh[1] - hh[i] - dist[i]);
    }
    printf("%lld\n", ans);

    return 0;
}

posted @   小菜珠的成长之路  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示