Minimum cost maximum flow problem

Source: Internet
Author: User

Based on the network maximum flow problem, the minimum cost problem, the cost weight and the capacity limit of maximum flow problem are two concepts, in essence, the problem is to find the weighted shortest path of the graph, but to realize it under the precondition of maximum flow. Therefore, the Bellman-ford algorithm is used to find the augmented path while calculating the minimum cost.
Below is the template for the maximum flow of the lowest fee in the Purple Book '

const int MAXN = 2000 + 10;

const int INF = 1000000000;
  struct Edge {int from, to, caps, flow, cost;

Edge (int u, int v, int c, int f, int W): From (U), to (v), Caps (c), Flow (f), Cost (W) {}};
  struct MCMF {int n, m;
  Vector<edge> edges;
  Vector<int> G[MAXN];         int INQ[MAXN];           Whether in the queue int D[MAXN];           Bellman-ford int P[MAXN];           On an arc int A[MAXN];
    An improved amount void init (int n) {this->n = n;
    for (int i = 0; i < n; i++) g[i].clear ();
  Edges.clear ();
    } void Addedge (int from, int to, int caps, int cost) {Edges.push_back (Edge (from, to, caps, 0, cost));
    Edges.push_back (to, from, 0, 0,-cost);
    m = Edges.size ();
    G[from].push_back (m-2);
  G[to].push_back (m-1);  BOOL Bellmanford (int s, int t, int flow_limit, int& flow, int& cost) {for (int i = 0; i < n; i++) D[i]
    = INF;
    memset (inq, 0, sizeof (INQ)); D[s] = 0; Inq[s] = 1; P[s] = 0;

    A[s] = INF;
  Queue<int> Q;  Q.push (s); while (! Q.empty ()) {int u = q.front ();
      Q.pop ();
      Inq[u] = 0;
        for (int i = 0; i < g[u].size (); i++) {edge& e = edges[g[u][i]];
          if (E.cap > E.flow && d[e.to] > D[u] + e.cost) {d[e.to] = D[u] + e.cost;
          P[e.to] = G[u][i];
          A[e.to] = min (A[u], e.cap-e.flow);
        if (!inq[e.to]) {Q.push (e.to); inq[e.to] = 1;}
    }} if (d[t] = = INF) return false;
    Flow + + a[t];
    Cost + + d[t] * a[t];
      for (int u = t; u!= s; u = edges[p[u]].from) {Edges[p[u]].flow + = a[t];
    Edges[p[u]^1].flow-= a[t];
  return true;
    //need to ensure that there is no negative ring int mincostflow (int s, int t, int flow_limit, int& cost) in the initial network {int flow = 0;
    while (Flow < Flow_limit && Bellmanford (S, T, Flow_limit, flow, cost));
  return flow; }

};

There's another problem.
Admiral UVA-1658



Using the split-point method: The 2~v-1 each point is split into I and I ' two, and then I and I ' between a capacity of 1, the cost of 0 side, and finally limit the maximum flow of 2 o'clock the minimum cost can be.

Solution: To point 2~n-1 split into Arc I->i ', the former (node) number is 1~n-2, the latter number is n~2n-3
for (int i = 2; I <= n-1; i++)
T.addedge (I-1, i+n-2, 1, 0);
For flow + = A[t] a[t] to make a limit, maximum traffic can not exceed 2 to ensure that the calculation of the minimum cost is the flow of 2 under the premise of
while (Flow < Flow_limit && Bellmanford (S, T, Flow_limit, flow, cost));
The complete code is as follows:

UVA1658.cpp: Defines the entry point for a console application. #include <iostream> #include <algorithm> #include <queue> #include <string.h> using namespace
Std
#define INF 1000000000 const int MAXN = 2005; struct edge{int from, to, caps, flow, cost;//starting point, end point, capacity, traffic, cost Edge (int u, int v, int c, int f, int W): From (U), to (V
), Cap (c), Flow (f), Cost (W) {}//constructor};
    struct mincmaxf{int N, m;//n represents the number of nodes, M represents the number of edges vector<edge>edges;
    vector<int>g[maxn]; int inq[maxn];//Mark whether the int d[maxn];//expense record in the queue, that is, the shortest path int p[maxn];//in the Bellman-ford algorithm points to the parent side, the upper edge, and the augmented int de after finding a path
        lta[maxn];//record residual network value void init (int n) {this->n = n;
        for (int i = 0; i < n; i++) g[i].clear ();
    Edges.clear ();
        } void Addedge (int from, int to, int cap,int) {edges.push_back (Edge (from, to, caps, 0, cost));
        Edges.push_back (to, from, 0, 0,-cost);
        m = Edges.size ();
        G[from].push_back (m-2);G[to].push_back (m-1); BOOL Bellmanford (int s, int t, int flow_limit, int &flow, long long &cost) {for (int i = 0; I & Lt N
        i++) D[i] = INF;
        memset (inq, 0, sizeof (INQ)); D[s] = 0; Inq[s] = 1; P[s] = 0;

        Delta[s] = INF;
        queue<int>q;
        Q.push (s); while (! Q.empty ()) {int u = q.front ();
            Q.pop (); Inq[u] = 0;//represents u out team for (int i = 0; I<g[u].size (); i++) {Edge &e = edges[g [U]
                [i]]; if (e.cap>e.flow&&d[e.to] > D[e.from] + e.cost) {d[e.to] = D[u] + e.cos
                    t;//update minimum cost p[e.to] = G[u][i];
                        Delta[e.to] = min (Delta[u], e.cap-e.flow);//update residual network value if (!inq[e.to]) {//Prevent backtracking searches from being searched for edges that have already been searched
                        Q.push (e.to);
                    Inq[e.to] = 1;
   }
                }
            }
        }     if (d[t] = = INF) return false;

        This sentence in the subject can not be, to a more rigorous some//if (flow + delta[t] > flow_limit) delta[t] = Flow_limit-flow;
        Flow + + delta[t];
        Cost = = (long long) d[t] * (Long Long) delta[t];//to find the current charge for (int u = t; u!= s; u = edges[p[u]].from)/backtracking
            {Edges[p[u]].flow + = delta[t];
        Edges[p[u]^1].flow-= delta[t];
    return true;
        int Mincostmaxflow (int s, int t, int flow_limit,long long &cost) {int flow = 0;
        Cost = 0;
        while (Bellmanford (S, T, Flow_limit, flow, cost) &&flow <flow_limit);
    return flow;

}
};
    int main () {int n=0, m = 0;
    MINCMAXF T;
    int div[1005];
        while (CIN >> n >> m &&n>0) {int a1, a2, A3;
        int temp = n;
        Long Long mincost;
        memset (Div, 0, sizeof (DIV));
        T.init (n*2-2); for (int i = 2; I <= n-1 i++) T.addedge (i-1, I+ n-2, 1, 0);        
            for (int i = 0; i < m i++) {cin >> a1>>a2 >> A3; 
            if (A1!= 1 && A1!= n) a1 + = n-2;
            else a1--;
            a2--;
        T.addedge (A1, A2, 1, A3);
        } t.mincostmaxflow (0, n-1, 2,mincost);
    cout << mincost << Endl;
return 0;

 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.