Difference between revisions of "Network Flow"

From EOJ Wiki
Jump to navigation Jump to search
(Created page with "== Basic Dinic == <syntaxhighlight lang='cpp'> const int INF = 1e9 const int maxn = 3000; struct Edge { Edge() {} Edge(int from, int to, int cap, int flow) : from(f...")
 
 
Line 2: Line 2:
  
 
<syntaxhighlight lang='cpp'>
 
<syntaxhighlight lang='cpp'>
const int INF = 1e9
 
const int maxn = 3000;
 
 
 
struct Edge {
 
struct Edge {
 
     Edge() {}
 
     Edge() {}
 
 
     Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
 
     Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
 
 
     int from, to, cap, flow;
 
     int from, to, cap, flow;
 
};
 
};
Line 16: Line 11:
 
     int n, m, s, t;
 
     int n, m, s, t;
 
     vector <Edge> edges;
 
     vector <Edge> edges;
     vector<int> G[maxn];
+
     vector<int> G[N];
     bool vis[maxn];
+
     bool vis[N];
     int d[maxn];
+
     int d[N];
     int cur[maxn];
+
     int cur[N];
  
 
     void init(int n, int s, int t) {
 
     void init(int n, int s, int t) {
Line 27: Line 22:
 
     }
 
     }
  
     void AddEdge(int from, int to, int cap) {
+
     int add_edge(int from, int to, int cap) {
 
         edges.push_back(Edge(from, to, cap, 0));
 
         edges.push_back(Edge(from, to, cap, 0));
 
         edges.push_back(Edge(to, from, 0, 0));
 
         edges.push_back(Edge(to, from, 0, 0));
Line 33: Line 28:
 
         G[from].push_back(m - 2);
 
         G[from].push_back(m - 2);
 
         G[to].push_back(m - 1);
 
         G[to].push_back(m - 1);
 +
        return m - 2;
 
     }
 
     }
  
Line 72: Line 68:
 
     }
 
     }
  
     int Maxflow() {
+
     int max_flow() {
 
         int flow = 0;
 
         int flow = 0;
 
         while (BFS()) {
 
         while (BFS()) {
Line 80: Line 76:
 
         return flow;
 
         return flow;
 
     }
 
     }
} DC;
+
} f;
 
 
int main() {
 
    int T;
 
    scanf("%d", &T);
 
    for (int kase = 1; kase <= T; ++kase) {
 
        int n, m;
 
        scanf("%d%d", &n, &m);
 
        DC.init(n, 1, n);
 
        while (m--) {
 
            int u, v, w;
 
            scanf("%d%d%d", &u, &v, &w);
 
            DC.AddEdge(u, v, w);
 
        }
 
        printf("Case %d: %d\n", kase, DC.Maxflow());
 
    }
 
    return 0;
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 103: Line 82:
  
 
<syntaxhighlight lang='cpp'>
 
<syntaxhighlight lang='cpp'>
const int INF = 1e9;
 
const int maxn = 200 + 10;
 
 
 
struct Edge {
 
struct Edge {
 
     int from, to, cap, flow, cost;
 
     int from, to, cap, flow, cost;
 
 
     Edge() {}
 
     Edge() {}
 
 
     Edge(int f, int t, int c, int fl, int co) : from(f), to(t), cap(c), flow(fl), cost(co) {}
 
     Edge(int f, int t, int c, int fl, int co) : from(f), to(t), cap(c), flow(fl), cost(co) {}
 
};
 
};
Line 117: Line 91:
 
     int n, m, s, t;
 
     int n, m, s, t;
 
     vector<Edge> edges;
 
     vector<Edge> edges;
     vector<int> G[maxn];
+
     vector<int> G[N];
     bool inq[maxn];
+
     bool inq[N];
     int d[maxn];
+
     int d[N];
     int p[maxn];
+
     int p[N];
     int a[maxn];
+
     int a[N];
  
 
     void init(int n, int s, int t) {
 
     void init(int n, int s, int t) {
Line 129: Line 103:
 
     }
 
     }
  
     void AddEdge(int from, int to, int cap, int cost) {
+
     int add_edge(int from, int to, int cap, int cost) {
 
         edges.push_back(Edge(from, to, cap, 0, cost));
 
         edges.push_back(Edge(from, to, cap, 0, cost));
 
         edges.push_back(Edge(to, from, 0, 0, -cost));
 
         edges.push_back(Edge(to, from, 0, 0, -cost));
Line 135: Line 109:
 
         G[from].push_back(m - 2);
 
         G[from].push_back(m - 2);
 
         G[to].push_back(m - 1);
 
         G[to].push_back(m - 1);
 +
        return m - 2;
 
     }
 
     }
  
Line 172: Line 147:
 
     }
 
     }
  
     int Min_cost() {
+
     int min_cost() {
 
         int flow = 0, cost = 0;
 
         int flow = 0, cost = 0;
 
         while (BellmanFord(flow, cost));
 
         while (BellmanFord(flow, cost));
 
         return cost;
 
         return cost;
 
     }
 
     }
} MM;
+
} mf;
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Latest revision as of 10:34, 31 May 2018

Basic Dinic

struct Edge {
    Edge() {}
    Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow) {}
    int from, to, cap, flow;
};

struct Dinic {
    int n, m, s, t;
    vector <Edge> edges;
    vector<int> G[N];
    bool vis[N];
    int d[N];
    int cur[N];

    void init(int n, int s, int t) {
        this->n = n, this->s = s, this->t = t;
        for (int i = 0; i <= n; i++) G[i].clear();
        edges.clear();
    }

    int add_edge(int from, int to, int cap) {
        edges.push_back(Edge(from, to, cap, 0));
        edges.push_back(Edge(to, from, 0, 0));
        m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
        return m - 2;
    }

    bool BFS() {
        memset(vis, 0, sizeof(vis));
        queue<int> Q;
        Q.push(s);
        d[s] = 0;
        vis[s] = true;
        while (!Q.empty()) {
            int x = Q.front();
            Q.pop();
            for (int i = 0; i < (int) G[x].size(); i++) {
                Edge &e = edges[G[x][i]];
                if (!vis[e.to] && e.cap > e.flow) {
                    vis[e.to] = true;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x, int a) {
        if (x == t || a == 0)return a;
        int flow = 0, f;
        for (int &i = cur[x]; i < (int) G[x].size(); i++) {
            Edge &e = edges[G[x][i]];
            if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
                e.flow += f;
                edges[G[x][i] ^ 1].flow -= f;
                flow += f;
                a -= f;
                if (a == 0) break;
            }
        }
        return flow;
    }

    int max_flow() {
        int flow = 0;
        while (BFS()) {
            memset(cur, 0, sizeof(cur));
            flow += DFS(s, INF);
        }
        return flow;
    }
} f;

MCMF (Bellman-Ford)

struct Edge {
    int from, to, cap, flow, cost;
    Edge() {}
    Edge(int f, int t, int c, int fl, int co) : from(f), to(t), cap(c), flow(fl), cost(co) {}
};

struct MCMF {
    int n, m, s, t;
    vector<Edge> edges;
    vector<int> G[N];
    bool inq[N];
    int d[N];
    int p[N];
    int a[N];

    void init(int n, int s, int t) {
        this->n = n, this->s = s, this->t = t;
        edges.clear();
        for (int i = 0; i < n; ++i) G[i].clear();
    }

    int add_edge(int from, int to, int cap, int cost) {
        edges.push_back(Edge(from, to, cap, 0, cost));
        edges.push_back(Edge(to, from, 0, 0, -cost));
        m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
        return m - 2;
    }

    bool BellmanFord(int &flow, int &cost) {
        for (int i = 0; i < n; ++i) d[i] = INF;
        memset(inq, 0, sizeof(inq));
        d[s] = 0, a[s] = INF, inq[s] = true, p[s] = 0;
        queue<int> Q;
        Q.push(s);
        while (!Q.empty()) {
            int u = Q.front();
            Q.pop();
            inq[u] = false;
            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] = true;
                    }
                }
            }
        }
        if (d[t] == INF) return false;
        flow += a[t];
        cost += a[t] * d[t];
        int u = t;
        while (u != s) {
            edges[p[u]].flow += a[t];
            edges[p[u] ^ 1].flow -= a[t];
            u = edges[p[u]].from;
        }
        return true;
    }

    int min_cost() {
        int flow = 0, cost = 0;
        while (BellmanFord(flow, cost));
        return cost;
    }
} mf;

Applications

Maximum-weight closed subgraph

int main() {
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
        cin >> b[i];
    DC.init(n + m + 10, n + m + 1, n + m + 2);
    for (int i = 1; i <= m; ++i)
        DC.AddEdge(n + i, n + m + 2, b[i]);
    for (int i = 1; i <= n; ++i) {
        cin >> a >> k;
        ans += a;
        DC.AddEdge(n + m + 1, i, a);
        for (int j = 1; j <= k; ++j) {
            cin >> c[j];
            DC.AddEdge(i, n + c[j], INF);
        }
    }
    ans -= DC.Maxflow();
    cout << ans << endl;
}