zoukankan      html  css  js  c++  java
  • 【bzoj2127】happiness 网络流最小割

    题目描述

    高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友。这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友如果能同时选文科或者理科,那么他们又将收获一些喜悦值。作为计算机竞赛教练的scp大老板,想知道如何分配可以使得全班的喜悦值总和最大。

    输入

    第一行两个正整数n,m。接下来是六个矩阵第一个矩阵为n行m列 此矩阵的第i行第j列的数字表示座位在第i行第j列的同学选择文科获得的喜悦值。第二个矩阵为n行m列 此矩阵的第i行第j列的数字表示座位在第i行第j列的同学选择理科获得的喜悦值。第三个矩阵为n-1行m列 此矩阵的第i行第j列的数字表示座位在第i行第j列的同学与第i+1行第j列的同学同时选择文科获得的额外喜悦值。第四个矩阵为n-1行m列 此矩阵的第i行第j列的数字表示座位在第i行第j列的同学与第i+1行第j列的同学同时选择理科获得的额外喜悦值。第五个矩阵为n行m-1列 此矩阵的第i行第j列的数字表示座位在第i行第j列的同学与第i行第j+1列的同学同时选择文科获得的额外喜悦值。第六个矩阵为n行m-1列 此矩阵的第i行第j列的数字表示座位在第i行第j列的同学与第i行第j+1列的同学同时选择理科获得的额外喜悦值。

    输出

    输出一个整数,表示喜悦值总和的最大值

    样例输入

    1 2 1 1 100 110 1 1000

    样例输出

    1210


    题解

    网络流最小割

    本体貌似有两种建图方法。

    第一种和 bzoj3438 差不多,比较简单且容易理解,所以本蒟蒻采用了这种方法。

    具体建图:

    S->同学,容量为文科收益;同学->T,容量为理科收益;

    S->相邻的两个同学文科组合(在同学的基础上加出来的新点),容量为都选文科的收益;相邻的两个同学文科组合->相邻的两个同学,容量为inf;

    相邻的两个同学->相邻的两个同学理科组合,容量为inf;相邻的两个理科组合->T,容量为都选理科的收益。

    最后求出最小割,答案为所有收益的总和-最小割。

    第二种参见 http://www.cnblogs.com/chenyushuo/p/5144957.html ,有点难理解,实测速度比我的快大概5倍左右。

    #include <cstdio> 
    #include <cstring> 
    #include <queue> 
    #define N 60010 
    #define M 300010 
    #define inf 0x7fffffff 
    #define pos(i , j) (i - 1) * m + j 
    using namespace std; 
    queue<int> q; 
    int head[N] , to[M] , val[M] , next[M] , cnt = 1 , s , t , dis[N]; 
    void add(int x , int y , int z) 
    { 
        to[++cnt] = y , val[cnt] = z , next[cnt] = head[x] , head[x] = cnt; 
        to[++cnt] = x , val[cnt] = 0 , next[cnt] = head[y] , head[y] = cnt; 
    } 
    bool bfs() 
    { 
        int x , i; 
        memset(dis , 0 , sizeof(dis)); 
        while(!q.empty()) q.pop(); 
        dis[s] = 1 , q.push(s); 
        while(!q.empty()) 
        { 
            x = q.front() , q.pop(); 
            for(i = head[x] ; i ; i = next[i]) 
            { 
                if(val[i] && !dis[to[i]]) 
                { 
                    dis[to[i]] = dis[x] + 1; 
                    if(to[i] == t) return 1; 
                    q.push(to[i]); 
                } 
            } 
        } 
        return 0; 
    } 
    int dinic(int x , int low) 
    { 
        if(x == t) return low; 
        int temp = low , i , k; 
        for(i = head[x] ; i ; i = next[i]) 
        { 
            if(val[i] && dis[to[i]] == dis[x] + 1) 
            { 
                k = dinic(to[i] , min(temp , val[i])); 
                if(!k) dis[to[i]] = 0; 
                val[i] -= k , val[i ^ 1] += k; 
                if(!(temp -= k)) break; 
            } 
        } 
        return low - temp; 
    } 
    int main() 
    { 
        int n , m , i , j , x , tot , ans = 0; 
        scanf("%d%d" , &n , &m) , s = 0 , t = tot = n * m + 1; 
        for(i = 1 ; i <= n ; i ++ ) for(j = 1 ; j <= m ; j ++ ) scanf("%d" , &x) , add(s , pos(i , j) , x) , ans += x; 
        for(i = 1 ; i <= n ; i ++ ) for(j = 1 ; j <= m ; j ++ ) scanf("%d" , &x) , add(pos(i , j) , t , x) , ans += x; 
        for(i = 1 ; i < n ; i ++ ) for(j = 1 ; j <= m ; j ++ ) scanf("%d" , &x) , add(s , ++tot , x) , add(tot , pos(i , j) , inf) , add(tot , pos(i + 1 , j) , inf) , ans += x; 
        for(i = 1 ; i < n ; i ++ ) for(j = 1 ; j <= m ; j ++ ) scanf("%d" , &x) , add(pos(i , j) , ++tot , inf) , add(pos(i + 1 , j) , tot , inf) , add(tot , t , x) , ans += x; 
        for(i = 1 ; i <= n ; i ++ ) for(j = 1 ; j < m ; j ++ ) scanf("%d" , &x) , add(s , ++tot , x) , add(tot , pos(i , j) , inf) , add(tot , pos(i , j + 1) , inf) , ans += x; 
        for(i = 1 ; i <= n ; i ++ ) for(j = 1 ; j < m ; j ++ ) scanf("%d" , &x) , add(pos(i , j) , ++tot , inf) , add(pos(i , j + 1) , tot , inf) , add(tot , t , x) , ans += x; 
        while(bfs()) ans -= dinic(s , inf); 
        printf("%d
    " , ans); 
        return 0; 
    }
    
  • 相关阅读:
    Elasticsearch:用户安全设置
    Elasticsearch:significant terms aggregation
    Elastic:Elastic部署架构介绍
    Elasticsearch:Smart Chinese Analysis plugin
    Elasticsearch:ICU分词器介绍
    新版本中的hits.total匹配数说明
    Elasticsearch:fuzzy 搜索 (模糊搜索)
    Elasticsearch:运用search_after来进行深度分页
    Elasticsearch:运用scroll接口对大量数据实现更好的分页
    Elasticsearch:search template
  • 原文地址:https://www.cnblogs.com/GXZlegend/p/6938444.html
Copyright © 2011-2022 走看看