算法——图论——最短路径(多边权)

原题

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

using namespace std;
typedef pair<int, int> PII;

vector<int> cityIndex(100, 0);
vector<PII> graph[100];
vector<int> Dist(100, -1);
vector<bool> State(100, false);
vector<int> lastNode[100];

struct pqNode {
    int destination;
    int dist;
    int lastNode;

    pqNode(int destination_, int dist_, int lastNode_) : destination(destination_), dist(dist_), lastNode(lastNode_) {}
};

struct compare {
    bool operator()(pqNode lhs, pqNode rhs) {
        return lhs.dist > rhs.dist;
    }
};

vector<vector<int>> Paths;
vector<int> tmpPath;

void dfs(int s, int t) {
    if (s == t) {
        tmpPath.push_back(s);
        Paths.push_back(tmpPath);
        tmpPath.pop_back();
        return;
    }
    for (int last: lastNode[t]) {
        tmpPath.push_back(t);
        dfs(s, last);
        tmpPath.pop_back();
    }
}

void Dijkstra(int s) {
    Dist[s] = 0;
    priority_queue<pqNode, vector<pqNode>, compare> pq;
    pq.push(pqNode(s, 0, s));

    while (!pq.empty()) {
        auto cur = pq.top();
        pq.pop();

        if (State[cur.destination]) {
            if (cur.dist == Dist[cur.destination])
                lastNode[cur.destination].push_back(cur.lastNode);
            continue;
        } else {
            State[cur.destination] = true;
            lastNode[cur.destination].push_back(cur.lastNode);
        }

        for (auto neighbor: graph[cur.destination]) {
            if (Dist[neighbor.first] >= Dist[cur.destination] + neighbor.second || Dist[neighbor.first] == -1) {
                Dist[neighbor.first] = Dist[cur.destination] + neighbor.second;
                pq.push(pqNode(neighbor.first, Dist[neighbor.first], cur.destination));
            }
        }
    }
}

bool pathCompare(vector<int> lhs, vector<int> rhs) {
    float l = 0, r = 0;
    for (int city: lhs) {
        l += (float)cityIndex[city];
    }
    for (int city: rhs) {
        r += (float )cityIndex[city];
    }
    l = l / (float )lhs.size();
    r = r / (float )rhs.size();
    return l <= r;
}

int main() {

    int n, m, s, t;
    cin >> n >> m >> s >> t;

    for (int i = 0; i < n; ++i) {
        cin >> cityIndex[i];
    }
    // 建图
    for (int i = 0; i < m; ++i) {
        int u, v, d;
        cin >> u >> v >> d;
        graph[u].push_back({v, d});
        graph[v].push_back({u, d});
    }

    Dijkstra(s);

    dfs(s, t);

    sort(Paths.begin(), Paths.end(), pathCompare);
    vector<int> result = Paths.front();
    reverse(result.begin(), result.end());

    cout << Dist[t] << " ";
    for (int i = 0; i < result.size(); ++i) {
        if (i != result.size() - 1) {
            cout << result[i] << "->";
        }else{
            cout << result[i] << endl;
        }
    }
    return 0;
}
相关推荐
葫三生1 小时前
如何评价《论三生原理》在科技界的地位?
人工智能·算法·机器学习·数学建模·量子计算
pipip.1 小时前
UDP————套接字socket
linux·网络·c++·网络协议·udp
拓端研究室3 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
wwer1425263633 小时前
数学建模_图论
数学建模·图论
随缘而动,随遇而安5 小时前
第八十八篇 大数据中的递归算法:从俄罗斯套娃到分布式计算的奇妙之旅
大数据·数据结构·算法
孞㐑¥5 小时前
Linux之Socket 编程 UDP
linux·服务器·c++·经验分享·笔记·网络协议·udp
IT古董5 小时前
【第二章:机器学习与神经网络概述】03.类算法理论与实践-(3)决策树分类器
神经网络·算法·机器学习
水木兰亭8 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
Jess079 小时前
插入排序的简单介绍
数据结构·算法·排序算法
老一岁9 小时前
选择排序算法详解
数据结构·算法·排序算法