acwing算法提高之图论--单源最短路的建图方式

目录

  • [1 介绍](#1 介绍)
  • [2 训练](#2 训练)

1 介绍

本博客用来记录使用dijkstra算法或spfa算法求解最短路问题的题目。

2 训练

题目11129热浪

C++代码如下,

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int N = 2510;
int n, m;
vector<vector<pair<int,int>>> g; //first表示next_node,second表示w
int dist[N];
bool st[N];
int snode, enode;

void dijkstra() {
    memset(dist, 0x3f, sizeof dist);
    dist[snode] = 0;
    
    priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>>> h;
    h.push(make_pair(0, snode));
    
    while (!h.empty()) {
        //确定当前结点中,不在集合s且距离结点snode最近的结点。记作cnode
        auto t = h.top();
        h.pop();
        int cdist = t.first, cnode = t.second;
        if (st[cnode]) continue; //如果cnode已经被确定是最小路径上的结点了,则跳过
        
        st[cnode] = true; //将它加入到集合中
        for (auto [next_node, w] : g[cnode]) {
            if (dist[next_node] > cdist + w) {
                dist[next_node] = cdist + w;
                h.push(make_pair(dist[next_node], next_node));
            }
        }
    }
    
    return;
}

int main() {
    cin >> n >> m >> snode >> enode;
    g.resize(n + 10);
    
    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    
    //求snode到enode的最短距离
    dijkstra();
    
    cout << dist[enode] << endl;
    
    return 0;
}

题目21128信使

C++代码如下,

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

const int N = 110;
int n, m;
int d[N];
bool st[N];
vector<vector<pair<int,int>>> g;

void dijkstra() {
    memset(d, 0x3f, sizeof d);
    
    d[1] = 0;
    
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> hp; //小根堆
    hp.push(make_pair(0, 1)); //first表示距离,second表示结点
    
    while (!hp.empty()) {
        
        auto t = hp.top(); //找到未在集合中,距离最小的结点
        hp.pop();
        
        int a = t.second;
        
        if (st[a]) continue; //已经用d[a]更新过了,将它放入集合中
        
        st[a] = true;
        for (auto [b, w] : g[a]) {
            if (d[b] > d[a] +w) { //d[b]此时比较大,用一个更小值来更新它。
                d[b] = d[a] + w;
                hp.push(make_pair(d[b], b));
            }
        }
    }
    
    return;
}

int main() {
    cin >> n >> m;
    g.resize(n + 10);
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    
    dijkstra();
    
    int res = 0; //求最大值
    for (int i = 1; i <= n; ++i) res = max(res, d[i]);
    if (res == 0x3f3f3f3f) {
        res = -1;
    }
    cout << res << endl;
    
    return 0;
}

题目31127香甜的黄油

C++代码如下,

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>

using namespace std;

const int N = 810;
int cow, n, m;
int cnt[N];
int d[N];
bool st[N];
vector<vector<pair<int,int>>> g;

void spfa(int start) {
    //起点为start
    
    memset(d, 0x3f, sizeof d);
    memset(st, 0, sizeof st);
    
    d[start] = 0;
    
    queue<int> q;
    q.push(start);
    st[start] = true; //结点start在队列中
    
    while (!q.empty()) {
        int t = q.front();
        q.pop();
        st[t] = false; //结点t不在队列中了
        
        for (auto [b, w] : g[t]) {
            if (d[b] > d[t] + w) {
                d[b] = d[t] + w;
                if (!st[b]) {
                    q.push(b);
                    st[b] = true;
                }
            }
        }
    }
    
    return;
}

int main() {
    cin >> cow >> n >> m;
    g.resize(n + 10);
    for (int i = 1; i <= cow; ++i) {
        int a;
        cin >> a; //每头牛所在的牧场
        cnt[a]++;
    }
    
    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    
    
    //spfa()算法 //o(m)时间复杂度,不会被超时
    long long res = INT_MAX;
    for (int i = 1; i <= n; ++i) {
        //第i个牧场作为放糖点
        
        spfa(i);
        long long t = 0; 
        for (int j = 1; j <= n; ++j) t += cnt[j] * d[j];
        res = min(res, t);
    }
    
    cout << res << endl;
    
    return 0;
}

题目41126最小花费

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

const int N = 2010;
int n, m;
int snode, enode;
double d[N]; //求最大距离,最大利率
bool st[N]; //是否使用它来更新过
vector<vector<pair<int,int>>> g;

void dijkstra() {
    //d的初始化
    for (int i = 1; i <= n; ++i) d[i] = 0.0; //初始成0.0
    
    memset(st, 0, sizeof st);
    
    d[snode] = 1.0;
    
    priority_queue<pair<double,int>> hp; //大根堆
    hp.push(make_pair(1.0, snode));
    
    while (!hp.empty()) {
        auto t = hp.top();
        hp.pop();
        
        int a = t.second;
        
        if (st[a]) continue;
        
        st[a] = true;
        for (auto [b, w] : g[a]) {
            if (d[b] < d[a] * 0.01 * (100 - w)) {
                d[b] = d[a] * 0.01 * (100 - w);
                hp.push(make_pair(d[b], b));
            }
        }
    }
    
    return;
}

int main() {
    cin >> n >> m;
    g.resize(n + 10);
    
    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    
    cin >> snode >> enode;
    
    dijkstra();
    
    double res = 100.0 / d[enode];
    printf("%.8f\n", res);
    
    return 0;
}

题目5

相关推荐
冷白白11 分钟前
【C++】C++对象初探及友元
c语言·开发语言·c++·算法
鹤上听雷19 分钟前
【AGC005D】~K Perm Counting(计数抽象成图)
算法
一叶祇秋32 分钟前
Leetcode - 周赛417
算法·leetcode·职场和发展
武昌库里写JAVA36 分钟前
【Java】Java面试题笔试
c语言·开发语言·数据结构·算法·二维数组
ya888g37 分钟前
GESP C++四级样题卷
java·c++·算法
Funny_AI_LAB1 小时前
MetaAI最新开源Llama3.2亮点及使用指南
算法·计算机视觉·语言模型·llama·facebook
NuyoahC1 小时前
算法笔记(十一)——优先级队列(堆)
c++·笔记·算法·优先级队列
jk_1011 小时前
MATLAB中decomposition函数用法
开发语言·算法·matlab
penguin_bark2 小时前
69. x 的平方根
算法
这可就有点麻烦了2 小时前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习