P2774 network flow problem taking the number of squares review

P2774 grid access issues

Prior to this subject written once and now again, or feel a little difficult, you may not have a thorough understanding before.

After this the number of questions asked to take a number, and these numbers can not be adjacent squares inside and asked to take complete and the maximum number is the number.

The good with the maximum independent set of network flow.

The position of these two numbers is divided into a set of independent, meaning that two separate sets a relationship between the two sets, but nothing set inside,

So are two separate sets.

After divided into separate sets, we will build even edge map, which are doing well, but why is the answer and the number of all - minimum cut 

Because when we run a minimum cut of and not let this figure is not connected, that is, this figure is not China Unicom, so that explanation

Every number and his location is not connected to the adjacent side, is not it meets the requirements of?

I do not want this figure is not communicating with the minimum cost is minimal cut,

I was carried to this figure some point trade-offs, and finally makes this figure is not connected, so the answer is the sum of all numbers - minimum cut.

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 4e3 + 10;
const int mod = 1e9 + 7;
struct edge {
    int u, v, c, f;
    Edge (int U, int V, int C, int F): U (U), V (V), C (C), F (F) {} 
}; 
Vector <Edge> E; 
Vector < int > G [MAXN] ;
 int Level [MAXN]; // the BFS layered, each point represents the number of layers 
int ITER [MAXN]; // current arc optimization 
int m;
 void the init ( int n-) {
     for ( int I = 0 ; I < n-=; I ++ ) G [I] .clear (); 
    e.clear (); 
} 
void addedge ( int U, int V, int C) { 
    e.push_back (Edge (U, V, C, 0 )); 
    e.push_back (Edge (V, U, 0 , 0 )); 
    m = e.size (); 
    G [U]. push_back (m - 2 ); 
    G [V] .push_back (m - . 1 ); 
} 
void BFS ( int S) // pretreatment level an array
 // directly BFS to each point 
{ 
    Memset (level, - . 1 , the sizeof (Level)); 
    Queue < int > Q; 
    Level [S] = 0;
    q.push(s);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int v = 0; v < G[u].size(); v++) {
            edge& now = e[G[u][v]];
            if (now.c > now.f && level[now.v] < 0) {
                level[now.v] = level[u] + 1;
                q.push(now.v);
            }
        }
    }
}
int dfs(int u, int t, intF) // the DFS Looking augmenting path 
{
     IF (U == T) return F; // has reached the source, the return flow F 
    for ( int & ITER V = [U]; V <G [U] .size () ; v ++ )
         // where each point represents the current arc with iter array, which is to prevent a augmenting path when looking for some edge multiple passes
         // each augmenting path to find the time, to an array empty 
    { 
        Edge & now = E [G [U] [V]];
         IF (now.c - now.f> 0 && Level [U] < Level [now.v])
             // now.c - now.f> 0 further indicates the road under
             // Level [U] <Level [now.v] indicates that this is the shortest route, must reach the next level, which is thought Dinic algorithm 
        {
            intDFS = D (now.v, T, min (F, now.c - now.f));
             IF (D> 0 ) { 
                now.f + = D; // forward traffic plus edge D 
                E [G [ U] [V] ^ . 1 ] .F - = D;
                 // reverse side Save d, where when two sides of the reverse side of the storage operation can be found directly by ^ 
                return D; 
            } 
        } 
    } 
    return  0 ; 
} 
int Maxflow ( int S, int T) {
     int Flow = 0 ;
     for (;;) { 
        the BFS (S); 
        IF(Level [T] < 0 ) return Flow; // residual network can not reach t, absence augmenting path 
        Memset (ITER, 0 , the sizeof (ITER)); // clear the current arc array 
        int F; // record by increase the flow passage widely 
        the while ((DFS = F (S, T, INF))> 0 ) { 
            flow + = F; 
        } 
    } 
    return flow; 
} 

int main () 
{ 
    int n-, m; 
    Scanf ( " % % D D " , & n-, & m); 
    LL SUM = 0 ;
     int s = 0, t = n * m + 1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int x;
            scanf("%d", &x);
            sum += x;
            if ((i + j) & 1) addedge((i - 1)*m + j, t, x);
            else addedge(s, (i - 1)*m + j, x);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if ((i + j) & 1) continue;
            if (i != 1) addedge((i - 1)*m + j, (i - 2)*m + j, inf);
            if (i != n) addedge((i - 1)*m + j, i*m + j, inf);
            if (j != 1) addedge((i - 1)*m + j, (i - 1)*m + j - 1, inf);
            if (j != m) addedge((i - 1)*m + j, (i - 1)*m + j + 1, inf);
        }
    }
    int ans = Maxflow(s, t);
    printf("%lld\n", sum - ans);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/EchoZQN/p/11247386.html