MAXFLOW-MINCUT代码出错

时间:2014-12-09 22:25:20

标签: c++ max-flow

我使用edmonds-karp实现编写了一个Max-Flow类。当我尝试获取最大流量的值时,代码似乎正常工作。我在SPOJ TotalFlow上提交了代码,因此我认为实施是正确的。

但是我试图从残差图中获得Min-Cut。这导致了一些错误。任何帮助都会有用。

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class EdmondsKarp
{
private:
    vector<vector<int>> graph;
    vector<vector<int>> rGraph;
    vector<vector<int>> capacities;
    vector<int> parent;
    vector<bool> visited;
    int source, sink;
    int flow;
public:
    EdmondsKarp(vector<vector<int>> graph, vector<vector<int>> capacities, int source, int sink)
    {
        this->graph = graph;
        this->capacities = capacities;
        this->rGraph = capacities;
        this->source = source;
        this->sink = sink;
        parent.resize(rGraph.size());
        visited.resize(rGraph.size());
        makeFlow();
    }
    bool bfs()
    {
        for (int i = 0; i < visited.size(); i++)
        {
            visited[i] = false;
        }

        queue<int> q;
        visited[source] = true;
        parent[source] = -1;
        q.push(source);
        int qf, v;
        while (!q.empty())
        {
            qf = q.front();
            q.pop();
            for (int i = 0; i < graph[qf].size(); i++)
            {
                v = graph[qf][i];
                if (!visited[v] && rGraph[qf][v] > 0)
                {
                    q.push(v);
                    parent[v] = qf;
                    visited[v] = true;
                }
            }
        }
        return visited[sink];
    }

    int makeFlow()
    {
        int INF = 9 + 1e9;
        flow = 0;
        int u, v, path_flow;
        while (bfs())
        {
            path_flow = INF;
            for (v = sink; v != source; v = parent[v])
            {
                u = parent[v];
                path_flow = min(path_flow, rGraph[u][v]);
            }

            for (v = sink; v != source; v = parent[v])
            {
                u = parent[v];
                rGraph[u][v] -= path_flow;
                rGraph[v][u] += path_flow;
            }
            flow += path_flow;
        }
        return 1;
    }

    int getFlow()
    {
        return flow;
    }

    vector<vector<int>> getFlowGraph()
    {
        vector<vector<int>> flowGraph(capacities.size(), vector<int>(capacities.size(), 0));
        for (int i = 0; i < capacities.size(); i++)
        {
            for (int j = 0; j < capacities[i].size(); j++)
            {
                if (capacities[i][j] > 0)
                {
                    flowGraph[i][j] = capacities[i][j] - rGraph[i][j];
                }
            }
        }
        return flowGraph;
    }

    vector<int> getMinCut()
    {
        vector<int> cut;
        bfs();
        int cutsize = 0;
        for (int i = 0; i < visited.size(); i++)
        {
            if (visited[i])
            {
                cut.push_back(i);
                for (int j = 0; j < graph[i].size(); j++)
                {
                    if (!visited[graph[i][j]])
                    {
                        cutsize += capacities[i][graph[i][j]];
                    }
                }
            }
        }
        cout << cutsize << endl;
        return cut;
    }
};

我的主要功能如下

int main()
{
    vector<vector<int>> graph(300, vector<int>(0)), capacities(300, vector<int>(300, 0));
    int m, w;
    char x, y;
    cin >> m;
    while (m--)
    {
        cin >> x >> y >> w;
        graph[x].push_back(y);
        capacities[x][y] += w;
        graph[y].push_back(x);
        capacities[y][x] += w;
    }
    EdmondsKarp edk(graph, capacities, 'A', 'Z');
    edk.getMinCut();
}

1 个答案:

答案 0 :(得分:0)

所以我设法修复了getMinCut函数。不幸的是,我还没有弄清楚原始代码中的错误。我也没有浏览所有可能的边缘组合,并在剪切中添加适当的边缘组合。

vector<int> getMinCut()
{
  vector<int> cut;
  bfs();
  for (int i = 0; i < visited.size(); i++)
  {
    if(visited[i])
    {
      cut.push_back(i);
    }
  }
  int cutsize = 0;
  for (int i = 0; i < graph.size(); i++)
  {
    for (int j = 0; j < graph.size(); j++)
    {
      if(visited[i] && !visited[j])
      {
        cutsize += capacities[i][j];
      }
    }
  }
  cout << cutsize << endl;
  return cut;
}