【bzoj2127】happiness 網路流最小割

來源:互聯網
上載者:User

標籤:最大   選擇   define   scp   turn   sizeof   lan   nic   cpp   

題目描述

高一一班的座位表是個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\n" , ans);     return 0; }

 

【bzoj2127】happiness 網路流最小割

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.