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

123 篇文章 1 订阅
本文介绍了如何使用C++中的Dijkstra和SPFA算法分别解决两个最短路问题,包括代码示例和应用场景,展示了在图论中寻找两点间最短路径的方法。
摘要由CSDN通过智能技术生成

1 介绍

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

2 训练

题目11129热浪

C++代码如下,

#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++代码如下,

#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++代码如下,

#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最小花费

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

题目5920最优乘车

C++代码如下,

#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <vector>
#include <queue>

using namespace std;

const int N = 510;
int n, m;
vector<vector<int>> g;
bool st[N];
int dist[N];

void bfs() {
    memset(dist, 0x3f, sizeof dist);
    
    queue<int> q;
    q.push(1);
    dist[1] = 0;
    st[1] = true;
    
    while (!q.empty()) {
        auto t = q.front();
        q.pop();
        
        //t可以走到哪儿
        for (auto b : g[t]) {
            if (!st[b]) {
                dist[b] = dist[t] + 1;
                
                q.push(b);
                st[b] = true;
            }
        }
    }
    
    return;
}

int main() {
    cin >> m >> n;
    
    g.resize(n + 10);
    
    string line;
    getline(cin, line);
    for (int i = 0; i < m; ++i) {
        getline(cin, line);
        stringstream ssin(line);
        
        vector<int> nodes;
        int node = -1;
        while (ssin >> node) {
            nodes.emplace_back(node);
        }
        
        for (int i = 0; i < nodes.size(); ++i) {
            for (int j = i + 1; j < nodes.size(); ++j) {
                g[nodes[i]].emplace_back(nodes[j]);
            }
        }
    }
    
    bfs();
    
    if (dist[n] == 0x3f3f3f3f) puts("NO");
    else cout << max(dist[n] - 1, 0) << endl;
    
    return 0;
}

题目6903昂贵的聘礼

C++代码如下,

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

using namespace std;

const int N = 110, INF = 0x3f3f3f3f;
int n, m;
int w[N][N], level[N];
int dist[N];
bool st[N];

int dijkstra(int down, int up) {
    memset(dist, 0x3f, sizeof dist);
    memset(st, 0, sizeof st);
    
    dist[0] = 0;
    for (int i = 1; i <= n + 1; ++i) {
        int t = -1;
        for (int j = 0; j <= n; ++j) {
            if (!st[j] && (t == -1 || dist[t] > dist[j])) {
                t = j;
            }
        }
        
        st[t] = true;
        for (int j = 1; j <= n; ++j) {
            if (level[j] >= down && level[j] <= up) {
                dist[j] = min(dist[j], dist[t] + w[t][j]);
            }
        }
    }
    
    return dist[1];
}

int main() {
    cin >> m >> n;
    
    memset(w, 0x3f, sizeof w);
    for (int i = 1; i <= n; ++i) w[i][i] = 0;
    
    for (int i = 1; i <= n; ++i) {
        int price, cnt;
        cin >> price >> level[i] >> cnt;
        w[0][i] = min(price, w[0][i]);
        while (cnt--) {
            int id, cost;
            cin >> id >> cost;
            w[id][i] = min(w[id][i], cost);
        }
    }
    
    int res = INF;
    for (int i = level[1] - m; i <= level[1]; ++i) res = min(res, dijkstra(i, i + m));
    
    cout << res << endl;
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值