1658-admiral (split + minimum charge flow)

Source: Internet
Author: User

The split-point method in this problem is a general method to solve several capacity. Because only the capacity limit is still unable to satisfy each node access only once this limit, the reason is very simple, we draw a diagram to know, assume that there are two paths from the starting point to the same node 2, and then all to the end of N, although they meet the traffic limit but through the same node.

So how do we solve this problem? The answer is: the Split method.

A node is split into two nodes, with a true junction with a capacity of 1 to 0 of the edge to the false junction, so that when we add edge, the other starting node is a false node, the end point is the true node. This implicitly adds a capacity attribute to this node. Of course, since we have to go through the starting point and the last point two times, so we can only 2~n-1 node, 1 and N to special treatment. If 1 or n is the starting point of the edge, then do not use false junction.

See the code for details:

#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int INF = 100000000;const int maxn = 3*1000+5;    int n,m,u,v,c,t,p[maxn],a[maxn],inq[maxn],d[maxn];struct Edge {int from, to, cap, flow, cost; Edge (int u,int v,int c,int f,int W): From (U), to (v), Cap (c), Flow (f), Cost (W) {}};vector<edge> Edges;vector<int    > G[maxn];void init () {for (int i=0;i<maxn;i++) g[i].clear (); Edges.clear ();}    void Addedge (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));    t = edges.size ();    G[from].push_back (t-2); G[to].push_back (t-1);}    BOOL Bellmanford (int s,int t,int& flow, ll& cost) {for (int i=0;i<maxn;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 + = (LL) d[t] * (LL) 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;    int mincostmaxflow (int s,int t, ll& cost) {int flow = 0; cost = 0;    while (Bellmanford (s,t,flow,cost)); return flow;}        int main () {while (~scanf ("%d%d", &n,&m) &&n) {init ();   for (int i=2;i<=n-1;i++) {Addedge (i,i+n,1,0);            The Split Method} for (int i=1;i<=m;i++) {scanf ("%d%d%d", &u,&v,&c);//The True node is connected by false nodes.            if (u! = 1 && u! = N) addedge (u+n,v,1,c); ElseAddedge (U,V,1,C);        } ll ans;        Addedge (0,1,2,0);        Addedge (n,2*n+1,2,0);        Mincostmaxflow (0,2*n+1,ans); printf ("%lld\n", ans);//i64d PE} return 0 on UVA;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1658-admiral (split + minimum charge flow)

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.