Dijkstra算法(C/C++)

 

/* 输入 */(有向图)
7 12 0   // 顶点数、边数、源点
0 1 2    // 边的两个顶点、边的权重
0 3 1
1 3 3
1 4 10
2 0 4
2 5 5
3 2 2
3 4 2
3 5 8
3 6 4
4 6 6
6 5 1
/* 输出 */
from 0 to 0: 0
distance: 0
from 0 to 1: 0->1
distance: 2
from 0 to 2: 0->3->2
distance: 3
from 0 to 3: 0->3
distance: 1
from 0 to 4: 0->3->4
distance: 3
from 0 to 5: 0->3->6->5
distance: 6
from 0 to 6: 0->3->6
distance: 5
#include <iostream>
#include <vector>
#include <algorithm>
/**
 * 图的单源最小路径(Dijkstra算法)
 * 存储结构:邻接矩阵
 * 图的性质:有权图、有向图
 **/

std::vector<std::vector<int>> buildGraph(int, int);
std::pair<std::vector<int>, std::vector<int>> Dijkstra(const std::vector<std::vector<int>> &, int);
void printPath(std::vector<int>, std::vector<int>, int);
int main()
{
    int vertexNum, edgeNum, vertex;
    std::cin >> vertexNum >> edgeNum >> vertex;
    std::vector<std::vector<int>> graph {buildGraph(vertexNum, edgeNum)};
    auto shortestPair{Dijkstra(graph, vertex)};

    printPath(shortestPair.first, shortestPair.second, vertex);

    return 0;
}
std::vector<std::vector<int>> buildGraph(int vertexNum, int edgeNum)
{
    std::vector<std::vector<int>> graph(vertexNum, std::vector<int>(vertexNum, __INT_MAX__));
    for (int v1, v2, weight, i = 0; i != edgeNum; ++i)
    {
        std::cin >> v1 >> v2 >> weight;
        graph[v1][v2] = weight;
    }
    for (int i = 0; i != vertexNum; ++i)
        graph[i][i] = 0;
    return graph;
}
std::pair<std::vector<int>, std::vector<int>> 
Dijkstra(const std::vector<std::vector<int>> &graph, int vertex)
{
    std::vector<bool> collected(graph.size(), false);
    std::vector<int> dist{graph[vertex]}, path(graph.size(), -1);
    for (unsigned int i = 0; i != graph.size(); ++i)
        if (dist[i] != __INT_MAX__ && i != vertex)
            path[i] = vertex;
    collected[vertex] = true;
    for ( ; std::find(collected.begin(), collected.end(), false) != collected.end(); )
    {
        vertex = std::find(collected.begin(), collected.end(), false)-collected.begin();
        for (unsigned int i = 0; i != graph.size(); ++i)
            if (!collected[i] && dist[i] < dist[vertex])
                vertex = i;
        collected[vertex] = true;
        for (unsigned int i = 0; i != graph.size(); ++i)
            if (collected[i] == false && graph[vertex][i] != __INT_MAX__)
                if (dist[vertex] + graph[vertex][i] < dist[i])
                {
                    dist[i] = dist[vertex] + graph[vertex][i];
                    path[i] = vertex;
                }
    }
    return std::pair<std::vector<int>, std::vector<int>>{path, dist};
}
void printPath(std::vector<int> path, std::vector<int> dist, int vertex)
{
    std::vector<int> goalPath;
    for (int goal = 0, next = goal; goal != path.size(); ++goal, next = goal)
    {
        while (next != -1)
        {
            goalPath.push_back(next);
            next = path[next];
        }
        std::cout << "from " << vertex << " to " << goal << ": ";
        for (auto riter = goalPath.rbegin(); riter != goalPath.rend(); ++riter)
            riter!=goalPath.rend()-1 ? std::cout << *riter << "->" : std::cout << *riter;
        std::cout << "\ndistance: " << dist[goal] << "\n";
        goalPath.clear();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值