Dijkstra算法;最高成本

时间:2013-04-20 19:02:25

标签: dijkstra

我想用它来寻找最大的成本。什么以及如何重写以使其有效?

此代码找到最低成本,我不知道该怎么做。 我在互联网上找到了它,但它可能包含错误。

#include <iostream>
#include <vector>
#include <string>
#include <list>

#include <limits> // for numeric_limits

#include <set>
#include <utility> // for pair
#include <algorithm>
#include <iterator>


typedef int vertex_t;
typedef double weight_t;

const weight_t max_weight = std::numeric_limits<double>::infinity();

struct neighbor {
vertex_t target;
weight_t weight;
neighbor(vertex_t arg_target, weight_t arg_weight)
    : target(arg_target), weight(arg_weight) { }
};

typedef std::vector<std::vector<neighbor> > adjacency_list_t;


void DijkstraComputePaths(vertex_t source,
                      const adjacency_list_t &adjacency_list,
                      std::vector<weight_t> &min_distance,
                      std::vector<vertex_t> &previous)
{
int n = adjacency_list.size();
min_distance.clear();
min_distance.resize(n, max_weight);
min_distance[source] = 0;
previous.clear();
previous.resize(n, -1);
std::set<std::pair<weight_t, vertex_t> > vertex_queue;
vertex_queue.insert(std::make_pair(min_distance[source], source));

while (!vertex_queue.empty())
{
    weight_t dist = vertex_queue.begin()->first;
    vertex_t u = vertex_queue.begin()->second;
    vertex_queue.erase(vertex_queue.begin());

    // Visit each edge exiting u
const std::vector<neighbor> &neighbors = adjacency_list[u];
    for (std::vector<neighbor>::const_iterator neighbor_iter = neighbors.begin();
         neighbor_iter != neighbors.end();
         neighbor_iter++)
    {
        vertex_t v = neighbor_iter->target;
        weight_t weight = neighbor_iter->weight;
        weight_t distance_through_u = dist + weight;
    if (distance_through_u < min_distance[v]) {
        vertex_queue.erase(std::make_pair(min_distance[v], v));

        min_distance[v] = distance_through_u;
        previous[v] = u;
        vertex_queue.insert(std::make_pair(min_distance[v], v));

    }

    }
}
}


std::list<vertex_t> DijkstraGetShortestPathTo(
vertex_t vertex, const std::vector<vertex_t> &previous)
{
std::list<vertex_t> path;
for ( ; vertex != -1; vertex = previous[vertex])
    path.push_front(vertex);
return path;
}


int main()
{
// remember to insert edges both ways for an undirected graph
adjacency_list_t adjacency_list(6);
// 0 = a
adjacency_list[0].push_back(neighbor(1, 7));
adjacency_list[0].push_back(neighbor(2, 9));
adjacency_list[0].push_back(neighbor(5, 14));
adjacency_list[0].push_back(neighbor(4, 14));
adjacency_list[0].push_back(neighbor(4, 914));
// 1 = b
adjacency_list[1].push_back(neighbor(0, 7));
adjacency_list[1].push_back(neighbor(2, 10));
adjacency_list[1].push_back(neighbor(3, 15));
// 2 = c
adjacency_list[2].push_back(neighbor(0, 9));
adjacency_list[2].push_back(neighbor(1, 10));
adjacency_list[2].push_back(neighbor(3, 11));
adjacency_list[2].push_back(neighbor(5, 2));
// 3 = d
adjacency_list[3].push_back(neighbor(1, 15));
adjacency_list[3].push_back(neighbor(2, 11));
adjacency_list[3].push_back(neighbor(4, 6));
// 4 = e
adjacency_list[4].push_back(neighbor(3, 6));
adjacency_list[4].push_back(neighbor(5, 9));
// 5 = f
adjacency_list[5].push_back(neighbor(0, 14));
adjacency_list[5].push_back(neighbor(2, 2));
adjacency_list[5].push_back(neighbor(4, 9));

std::vector<weight_t> min_distance;
std::vector<vertex_t> previous;
DijkstraComputePaths(0, adjacency_list, min_distance, previous);
std::cout << "Distance from 0 to 4: " << min_distance[4] << std::endl;
std::list<vertex_t> path = DijkstraGetShortestPathTo(4, previous);
std::cout << "Path : ";
std::copy(path.begin(), path.end(), std::ostream_iterator<vertex_t>(std::cout, " "));
std::cout << std::endl;

return 0;
}

1 个答案:

答案 0 :(得分:1)

而不是跟踪到目前为止发现的最低成本的方式跟踪具有最大值的那个:

if (distance_through_u > min_distance[v]) {

现在必须使用最小允许距离初始化距离矢量,所以

min_distance.resize(n, -max_weight);   // not a C++ expert, should be -inf

请记住,现在min_distance存储最大距离,因此请更改其名称以及所有其他涉及的var的名称,以免混淆谁读取您的代码;)