BZOJ3894: Science and Arts Division

Source: Internet
Author: User

https://blog.csdn.net/PoPoQQQ/article/details/43968017

Total revenue-minimum loss, minimum cut.
For the direct selection of arts and sciences is very simple, directly to the point of the S,t S, the T s,t capacity of the arts and humanities pleasure is good.
Then how to solve the same problem adjacent to each other. We can consider the idea of offsetting. The cut is not the selected section. What is the residual network of direct selection of arts and sciences?
is to choose that branch more out of the proceeds. If these gains do not add up to a large capacity side of the other set, then the additional set will definitely have a residual amount, and the set will have no residuals. That is, all the edges in this set are not selected. is to select a whole other family.
So we create a new point for each point, which means that the point is the same as the profit from the neighboring point, even to T.
s the same as the smallest cut to find the answer.

#include <bits/stdc++.h> using namespace std;
const int maxn=8e5+5;

const int INF=2E9; struct edge{int to,next,w;}

e[maxn<<1];
int head[maxn],cur[maxn],cnt=1;
    inline void Add (int u,int v,int W) {e[++cnt]= (edge) {v,head[u],w},head[u]=cnt;
E[++cnt]= (Edge) {u,head[v],0},head[v]=cnt;
} queue<int>q;

int dep[maxn],s,t,n,m;

inline int get (int x,int y) {return (x-1) *m+y;}
    BOOL BFs () {memset (dep,0,sizeof (DEP));
    Dep[s]=1;q.push (s);
        while (Q.size ()) {int U=q.front (); Q.pop ();
            for (int i=head[u];i;i=e[i].next) {int v=e[i].to,w=e[i].w;
                if (!dep[v]&&w) {dep[v]=dep[u]+1;
            Q.push (v);
    }}} if (Dep[t]) return 1;
return 0; } int dfs (int u,int flow) {if (u==t| |
    flow==0) return flow;
        for (int &i=cur[u];i;i=e[i].next) {int v=e[i].to,w=e[i].w;
         if (w&&dep[v]==dep[u]+1) {int Tem=dfs (v,min (E[i].w,flow));   if (TEM) {e[i].w-=tem;
                E[i^1].w+=tem;
            return tem;
}}} return 0;
    } int dinic () {int ans=0;
        while (BFS ()) {for (int i=s;i<=t;i++) cur[i]=head[i];
    while (int D=dfs (s,inf)) Ans+=d;
} return ans;

} int output=0,ar[105][105],sc[105][105],sar[105][105],sac[105][105];
    int main () {scanf ("%d%d", &n,&m);
    s=0,t=n*m*3+1;
            for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {scanf ("%d", &ar[i][j]);
            OUTPUT+=AR[I][J];
        Add (S,get (I,J), ar[i][j]);
            }} for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {scanf ("%d", &sc[i][j]);
            OUTPUT+=SC[I][J];
        Add (Get (I,J), t,sc[i][j]);
            }} for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {scanf ("%d", &sar[i][j]);
            OUTPUT+=SAR[I][J];
          Add (S,n*m+get (I,J), sar[i][j]);  Add (N*m+get (I,J), get (I,J), INF);
            if (i>1) Add (N*m+get (I,J), get (I-1,J), INF);
            if (i<n) Add (N*m+get (I,J), get (I+1,J), INF);
            if (j>1) Add (N*m+get (I,J), get (i,j-1), INF);
        if (j<m) Add (N*m+get (I,J), get (i,j+1), INF);
            }} for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {scanf ("%d", &sac[i][j]);
            OUTPUT+=SAC[I][J];
            Add (N*m*2+get (I,J), t,sac[i][j]);
            Add (Get (I,J), N*m*2+get (i,j), INF);
            if (i>1) Add (Get (I-1,J), N*m*2+get (i,j), INF);
            if (i<n) Add (Get (I+1,J), N*m*2+get (i,j), INF);
            if (j>1) Add (Get (i,j-1), N*m*2+get (i,j), INF);
        if (j<m) Add (Get (i,j+1), N*m*2+get (i,j), INF);
    }} printf ("%d\n", Output-dinic ());
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.