Processing math: 100%
My Algorithm : kopricky アルゴリズムライブラリ

kopricky アルゴリズムライブラリ

Minimum Cost Flow Algorithm on DAG

コードについての説明

最小費用流を求めるアルゴリズムのグラフが DAG の場合の改良版.
グラフが DAG である場合, 負辺が含まれていても初期ポテンシャルの計算がトポロジカル順序に沿って DP を行うことで求めることができるので愚直に Bellman Ford 法を用いる場合(時間計算量 O(nm))よりも高速になる.

(関数)
solve(s,t,f) : s から t へ流量 f 流すのにかかる最小費用

時間計算量: O(|F|mlogn) (流量を |F| とする)

コード

  1. template<typename CapType, typename CostType> class MinCostFlowDAG {
  2. public:
  3. using Cat = CapType;
  4. using Cot = CostType;
  5. using pti = pair<Cot, int>;
  6. struct edge {
  7. int to, rev;
  8. Cat cap;
  9. Cot cost;
  10. };
  11. const int V;
  12. const Cot inf;
  13. vector<vector<edge> > G;
  14. vector<Cot> h, dist;
  15. vector<int> deg, ord, prevv, preve;
  16. MinCostFlowDAG(const int node_size) : V(node_size), inf(numeric_limits<Cot>::max()),
  17. G(V), h(V, inf), dist(V), deg(V, 0), prevv(V), preve(V){}
  18. void add_edge(const int from, const int to, const Cat cap, const Cot cost){
  19. if(cap == 0) return;
  20. G[from].push_back((edge){to, (int)G[to].size(), cap, cost});
  21. G[to].push_back((edge){from, (int)G[from].size() - 1, 0, -cost});
  22. ++deg[to];
  23. }
  24. bool tsort(){
  25. queue<int> que;
  26. for(int i = 0; i < V; ++i){
  27. if(deg[i] == 0) que.push(i);
  28. }
  29. while(!que.empty()){
  30. const int p = que.front();
  31. que.pop();
  32. ord.push_back(p);
  33. for(auto& e : G[p]){
  34. if(e.cap > 0 && --deg[e.to] == 0) que.push(e.to);
  35. }
  36. }
  37. return (*max_element(deg.begin(), deg.end()) == 0);
  38. }
  39. void calc_potential(const int s){
  40. h[s] = 0;
  41. for(const int v : ord){
  42. if(h[v] == inf) continue;
  43. for(const edge& e : G[v]){
  44. if(e.cap > 0) h[e.to] = min(h[e.to], h[v] + e.cost);
  45. }
  46. }
  47. }
  48. void Dijkstra(const int s){
  49. priority_queue<pti,vector<pti>,greater<pti> > que;
  50. fill(dist.begin(), dist.end(), inf);
  51. dist[s] = 0;
  52. que.push(pti(0, s));
  53. while(!que.empty()){
  54. pti p = que.top();
  55. que.pop();
  56. const int v = p.second;
  57. if(dist[v] < p.first) continue;
  58. for(int i = 0; i < (int)G[v].size(); ++i){
  59. edge& e = G[v][i];
  60. if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
  61. dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
  62. prevv[e.to] = v, preve[e.to] = i;
  63. que.push(pti(dist[e.to], e.to));
  64. }
  65. }
  66. }
  67. }
  68. void update(const int s, const int t, Cat& f, Cot& res){
  69. for(int i = 0; i < V; i++){
  70. if(dist[i] != inf) h[i] += dist[i];
  71. }
  72. Cat d = f;
  73. for(int v = t; v != s; v = prevv[v]){
  74. d = min(d, G[prevv[v]][preve[v]].cap);
  75. }
  76. f -= d;
  77. res += h[t] * d;
  78. for(int v = t; v != s; v = prevv[v]){
  79. edge& e = G[prevv[v]][preve[v]];
  80. e.cap -= d;
  81. G[v][e.rev].cap += d;
  82. }
  83. }
  84. Cot solve(const int s, const int t, Cat f){
  85. if(!tsort()) assert(false); // not DAG
  86. calc_potential(s);
  87. Cot res = 0;
  88. while(f > 0){
  89. Dijkstra(s);
  90. if(dist[t] == inf) return -inf;
  91. update(s, t, f, res);
  92. }
  93. return res;
  94. }
  95. };

verify 用の問題

Atcoder : グラフ 提出コード(非想定解)