[BZOJ3894]文理分科 最小割

诶兄弟我正在B站直播写博客

[BZOJ3894]文理分科 最小割_第1张图片

建立网络流模型
对于每个学生,
原点连向他,容量为他选择文科的收益
他连向汇点,容量问他选择理科的收益
对于每个学生,再多创建两个附加点p1,p2,
p1连向他周围的学生容量为INF,周围的学生连向他容量为INF

答案=正权值和-最小割

#include 
#include 
#include 
#include 
#include 
#define INF 2147483647
#define N 200500
#define M 400050
#define lisiaoSB 505

using namespace std;

struct Node {int u,cap,rec;};
vector e[N];
int n,m,p[N],a[M],b[M],v[M],S,T,lim,ans;
int pp[lisiaoSB][lisiaoSB],art[lisiaoSB][lisiaoSB],science[lisiaoSB][lisiaoSB];
int same_art[lisiaoSB][lisiaoSB],same_science[lisiaoSB][lisiaoSB];

const int fx[] = {0,1,0,-1};
const int fy[] = {1,0,-1,0};

int cnt;
void add_Node(int a,int b,int v)
{
    Node tmp;
    tmp.u = b;
    tmp.cap = v;
    tmp.rec = e[b].size();
    e[a].push_back(tmp);

    tmp.u = a;
    tmp.cap = 0;
    tmp.rec = e[a].size() - 1;
    e[b].push_back(tmp);
}

bool BFS() 
{
    bool flag = false;
    memset(p,0,sizeof(p));
    queue<int> q;
    q.push(S);
    p[S] = 1;
    while (!q.empty()) {
        int u = q.front();q.pop();
        if (u == T) flag = true;
        for (int i=0;iint v = e[u][i].u;
            int cp = e[u][i].cap;
            if (p[v] == 0 && cp > 0) {
                q.push(v);
                p[v] = p[u] + 1;
            }
        }
    }
    return flag;
}

int DFS(int u,int flow) {
    if (u == T) return flow;
    int g = 0 , f = flow;
    for (int i=0;iint v = e[u][i].u;
        int cp = e[u][i].cap;
        int tmp = 0;
        if (p[v] == p[u]+1 && cp > 0 && (tmp = DFS(v,min(f,cp))) > 0) {
            g += tmp;
            f -= tmp;
            e[u][i].cap -= tmp;
            int rc = e[u][i].rec;
            e[v][rc].cap += tmp;
        }
    }
    return g;
}

int main()
{
    scanf("%d%d",&n,&m);
    S = ++cnt; T = ++cnt; 
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++) pp[i][j] = ++cnt;

    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++) scanf("%d",&art[i][j]) , ans += art[i][j];
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++) scanf("%d",&science[i][j]) , ans += science[i][j];
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++) scanf("%d",&same_art[i][j]) , ans += same_art[i][j];
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++) scanf("%d",&same_science[i][j]) , ans += same_science[i][j];

    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++) {
            add_Node(S,pp[i][j],art[i][j]);
            add_Node(pp[i][j],T,science[i][j]);
            int p1 = ++cnt , p2 = ++cnt;

            add_Node(S,p1,same_art[i][j]);
            add_Node(p2,T,same_science[i][j]);
            add_Node(p1,pp[i][j],INF);
            add_Node(pp[i][j],p2,INF);

            for (int _=0;_<4;_++) {
                int x = i + fx[_] , y = j + fy[_];
                if (x >= 1 && x <= n && y >= 1 && y <= m) {
                    add_Node(p1,pp[x][y],INF);
                    add_Node(pp[x][y],p2,INF);
                }
            }
        }

    while (BFS()) ans -= DFS(S,INF);
    printf("%d\n",ans);

    return 0;
}

你可能感兴趣的:(网络流,雅礼集训)