c++ - 优化 dijkstra 实现

标签 c++ algorithm matching priority-queue dijkstra

问题已编辑,现在我只想知道是否可以使用队列来改进算法。

我发现了混合成本最大流算法的实现,它使用 dijkstra:http://www.stanford.edu/~liszt90/acm/notebook.html#file2

将其粘贴到此处,以防它在互联网上丢失:

// Implementation of min cost max flow algorithm using adjacency
// matrix (Edmonds and Karp 1972).  This implementation keeps track of
// forward and reverse edges separately (so you can set cap[i][j] !=
// cap[j][i]).  For a regular max flow, set all edge costs to 0.
//
// Running time, O(|V|^2) cost per augmentation
//     max flow:           O(|V|^3) augmentations
//     min cost max flow:  O(|V|^4 * MAX_EDGE_COST) augmentations
//     
// INPUT: 
//     - graph, constructed using AddEdge()
//     - source
//     - sink
//
// OUTPUT:
//     - (maximum flow value, minimum cost value)
//     - To obtain the actual flow, look at positive values only.

#include <cmath>
#include <vector>
#include <iostream>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long L;
typedef vector<L> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

const L INF = numeric_limits<L>::max() / 4;

struct MinCostMaxFlow {
  int N;
  VVL cap, flow, cost;
  VI found;
  VL dist, pi, width;
  VPII dad;

  MinCostMaxFlow(int N) : 
    N(N), cap(N, VL(N)), flow(N, VL(N)), cost(N, VL(N)), 
    found(N), dist(N), pi(N), width(N), dad(N) {}

  void AddEdge(int from, int to, L cap, L cost) {
    this->cap[from][to] = cap;
    this->cost[from][to] = cost;
  }

  void Relax(int s, int k, L cap, L cost, int dir) {
    L val = dist[s] + pi[s] - pi[k] + cost;
    if (cap && val < dist[k]) {
      dist[k] = val;
      dad[k] = make_pair(s, dir);
      width[k] = min(cap, width[s]);
    }
  }

  L Dijkstra(int s, int t) {
    fill(found.begin(), found.end(), false);
    fill(dist.begin(), dist.end(), INF);
    fill(width.begin(), width.end(), 0);
    dist[s] = 0;
    width[s] = INF;

    while (s != -1) {
      int best = -1;
      found[s] = true;
      for (int k = 0; k < N; k++) {
        if (found[k]) continue;
        Relax(s, k, cap[s][k] - flow[s][k], cost[s][k], 1);
        Relax(s, k, flow[k][s], -cost[k][s], -1);
        if (best == -1 || dist[k] < dist[best]) best = k;
      }
      s = best;
    }

    for (int k = 0; k < N; k++)
      pi[k] = min(pi[k] + dist[k], INF);
    return width[t];
  }

  pair<L, L> GetMaxFlow(int s, int t) {
    L totflow = 0, totcost = 0;
    while (L amt = Dijkstra(s, t)) {
      totflow += amt;
      for (int x = t; x != s; x = dad[x].first) {
        if (dad[x].second == 1) {
          flow[dad[x].first][x] += amt;
          totcost += amt * cost[dad[x].first][x];
        } else {
          flow[x][dad[x].first] -= amt;
          totcost -= amt * cost[x][dad[x].first];
        }
      }
    }
    return make_pair(totflow, totcost);
  }
};

我的问题是是否可以通过在 Dijkstra() 内部使用优先级队列来改进它。我尝试过,但无法让它正常工作。 实际上我怀疑在 Dijkstra 中它应该循环相邻节点,而不是所有节点......

非常感谢。

最佳答案

Dijkstra 算法当然可以通过使用 minheap 来改进。将一个顶点放入最短路径树并处理(即标记)所有相邻顶点后,下一步是选择尚未在树中的具有最小标记的顶点。
这就是 minheap 出现的地方。我们不是按顺序扫描所有顶点,而是从堆中提取最小元素并对其进行重组,这需要 O(logn) 时间与 O(n) 时间。请注意,堆将仅保留那些尚未在最短路径树中的顶点。然而,如果我们更新它们的标签,我们应该能够以某种方式修改堆中的顶点。

关于c++ - 优化 dijkstra 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13545173/

相关文章:

javascript - 检查某个位置的位是否为1

java - 用于过滤/匹配 URL 的基本开源 java 包是什么?

c++ - 线程阻止创建窗口

javascript - collat​​z 序列的最大长度 - 优化

c++ - VTK如何平滑一条线

algorithm - 有效地排序排列

java - 用 Java 实现的最佳模式匹配算法

java - 室友匹配算法

c++ - 在不加载 DLL 的情况下使用函数?

c++ - 带有 OpenGL 的 Visual C++ 中的拉格朗日多项式