基于A*算法的KSP问题求解

这篇博客介绍如何利用A*算法解决KSP问题,提供了4000个点之间的最短路径计算,并详细说明了性能表现。通过Python生成输入数据并用C++进行处理,步骤包括将Python代码输出重定向到in.txt文件,然后编译运行C++代码。
摘要由CSDN通过智能技术生成
#include <bits/stdc++.h>

using namespace std;

constexpr int INF = 1e9;

vector<int> H; // h(x)

void LoadTopo(vector<vector<int>> &topo, int nodeNum)
{
    topo.resize(nodeNum + 1);
    int u, v, w;
    // while (cin >> u >> v >> w) {
    //     topo[u].push_back(v);
    // }
    while (~scanf("%d %d %d", &u, &v, &w)) {
        topo[u].push_back(v);
    }
    return;
}

void ReverseTopo(const vector<vector<int>> &topo, vector<vector<int>> &revTopo)
{
    revTopo.resize(topo.size());
    for (int u = 0; u < topo.size(); ++u) {
        for (int v : topo[u]) {
            revTopo[v].push_back(u);
        }
    }
}

constexpr int TOPO_LINK_DEFAULT_COST  = 1;

void Dijkstra(const vector<vector<int>> &topo, vector<int> &cost, int src)
{
    using P = pair<int, int>;
    priority_queue<P, vector<P>, greater<P>> que;
    cost.resize(topo.size());
    fill(cost.begin(), cost.end(), INF);
    cost[src] = 0;
    que.push((P){cost[src], src});
    while (!que.empty()) {
        P curP = que.top();
        que.pop();
        int u = curP.second;
        if (cost[u] < curP.first) {
            continue;
        }
        for (int v : topo[u]) {
            if (cost[v] > cost[u] + TOPO_LINK_DEFAULT_COST) {
                cost[v] = cost[u] + TOPO_LINK_DEFAULT_COST;
                que.push((P){cost[v], v});
            }
        }
    }
}

void PrintPath(const vector<int> &path)
{
    cout << "path cost
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值