本来想写道水题....结果调了这么久!就是一个 define 里面少加了个括号 !

二分图最大点权独立集...黑白染色一下 , 然后建图 :

S -> black_node , white_node -> T , 流量都为点权 . 然后 black_node -> white_node ( 两点有公共边 ) , 流量为 +oo , 然后 answer = ∑ w( i ) ( i ∈ V ) - maxflow

-------------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
  
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define id( x , y ) ( ( x ) * n + y + 1 )
  
using namespace std;
  
const int inf = 0x7fffffff;
const int maxn = 1000;
  
struct edge {
int to , cap;
edge *next , *rev;
} E[ maxn << 3 ] , *pt = E , *head[ maxn ];
  
inline void add_edge( int u , int v , int w ) {
pt -> to = v;
pt -> cap = w;
pt -> next = head[ u ];
head[ u ] = pt++;
pt -> to = u;
pt -> cap = 0;
pt -> next = head[ v ];
head[ v ] = pt++;
head[ u ] -> rev = head[ v ];
head[ v ] -> rev = head[ u ];
}
  
int h[ maxn ] , cnt[ maxn ] , S , T , N;
edge *cur[ maxn ] , *p[ maxn ];
  
int maxFlow() {
clr( h , 0 ) , clr( cnt , 0 );
cnt[ 0 ] = N;
rep( i , N ) cur[ i ] = head[ i ];
int flow = 0 , A = inf;
edge* e;
for( int x = S ; h[ S ] < N ; ) {
for( e = cur[ x ] ; e ; e = e -> next ) 
   if( h[ e -> to ] + 1 == h[ x ] && e -> cap ) break;
if( e ) {
A = min( A , e -> cap );
p[ e -> to ] = cur[ x ] = e;
if( ( x = e -> to ) == T ) {
   for( ; x != S ; x = p[ x ] -> rev -> to ) {
   p[ x ] -> cap -= A;
   p[ x ] -> rev -> cap += A;
   }
   flow += A;
   A = inf;
}
} else {
if( ! --cnt[ h[ x ] ] ) break;
h[ x ] = N;
for( e = head[ x ] ; e ; e = e -> next )
   if( e -> cap && h[ e -> to ] + 1 < h[ x ] ) {
    h[ x ] = h[ e -> to ] + 1;
    cur[ x ] = e;
   }
cnt[ h[ x ] ]++;
if( x != S ) x = p[ x ] -> rev -> to;
}
}
return flow;
}
  
int main(){
freopen( "test.in" , "r" , stdin );
clr( head , 0 );
int ans = 0 , n;
cin >> n;
S = 0 , T = n * n + 1 , N = T + 1;
rep( i , n ) 
   rep( j , n ) {
    int w;
    scanf( "%d" , &w );
    ans += w;
    if( ( i + j ) & 1 ) {
       add_edge( S , id( i , j ) , w );
       if( i ) add_edge( id( i , j ) , id( i - 1 , j ) , inf );
       if( j ) add_edge( id( i , j ) , id( i , j - 1 ) , inf );
       if( i + 1 < n ) add_edge( id( i , j ) , id( i + 1 , j ) , inf );
       if( j + 1 < n ) add_edge( id( i , j ) , id( i , j + 1 ) , inf );
    } else
       add_edge( id( i , j ) , T , w );
   }
cout << ans - maxFlow() << "\n";
return 0;

-------------------------------------------------------------------------------------------------

1475: 方格取数

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 616  Solved: 323
[Submit][Status][Discuss]

Description

在一个n*n的方格里,每个格子里都有一个正整数。从中取出若干数,使得任意两个取出的数所在格子没有公共边,且取出的数的总和尽量大。

Input

第一行一个数n;(n<=30) 接下来n行每行n个数描述一个方阵

Output

仅一个数,即最大和

Sample Input

2
1 2
3 5

Sample Output

6

HINT

Source

BZOJ 1475: 方格取数( 网络流 )的更多相关文章

  1. BZOJ 1475 方格取数(二分图最大点权独立集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1475 [题目大意] 给出一个n*n的方格,从中取一些不相邻的数字,使得和最大 [题解] ...

  2. [BZOJ 1475] 方格取数

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1475 [算法] 首先将方格黑白染色 , 也就是说 , 如果(i + j)为奇数 , ...

  3. [BZOJ1475]方格取数 网络流 最小割

    1475: 方格取数 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1025  Solved: 512[Submit][Status][Discuss] ...

  4. P2774 方格取数(网络流)

    https://www.luogu.com.cn/problem/P2774 在一个有 m×n 个方格的棋盘中,每个方格中有一个正整数. 现要从方格中取数,使任意2个数所在方格没有公共边,且取出的数的 ...

  5. 【bzoj1475】方格取数 网络流最小割

    题目描述 在一个n*n的方格里,每个格子里都有一个正整数.从中取出若干数,使得任意两个取出的数所在格子没有公共边,且取出的数的总和尽量大. 输入 第一行一个数n:(n<=30) 接下来n行每行n ...

  6. bzoj P2045 方格取数加强版【最大费用最大流】

    今天脑子不太清醒,把数据范围看小了一直TTTTLE-- 最大费用最大流,每个格子拆成两个(x,y),(x,y)',(x,y)向(x,y)'连一条费用a[x][y]流量1的边表示选的一次,再连一条费用0 ...

  7. [网络流24题#9] [cogs734] 方格取数 [网络流,最大流最小割]

    将网格分为两部分,方法是黑白染色,即判断(i+j)&1即可,分开后从白色格子向黑色格子连边,每个点需要四条(边界点可能更少),也就是每个格子周围的四个方向.之后将源点和汇点分别于黑白格子连边, ...

  8. LibreOJ #6007. 「网络流 24 题」方格取数 最小割 最大点权独立集 最大流

    #6007. 「网络流 24 题」方格取数 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  9. Libre 6007 「网络流 24 题」方格取数 / Luogu 2774 方格取数问题 (网络流,最大流)

    Libre 6007 「网络流 24 题」方格取数 / Luogu 2774 方格取数问题 (网络流,最大流) Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从 ...

随机推荐

  1. android api 中文 (74)—— AdapterView.AdapterContextMenuInfo

    前言 本章内容是android.widget.AdapterView.AdapterContextMenuInfo,版本为Android 2.3 r1,翻译来自"cnmahj",欢 ...

  2. URL中含有+号,出现错误“请求筛选模块被配置为拒绝包含双重转义序列的请求”的解决方法

    搜索关键词中含空格,提交后被自动转成了“+”号,报如下错误: HTTP 错误 404.11 - Not Found 请求筛选模块被配置为拒绝包含双重转义序列的请求. 解决方法: 在web.config ...

  3. C#中类的运用(Eighth day)

    又到了总结知识的时刻了,今天在云和学院学习了类的运用,下面是今天所学知识的总结: 理论: 类的定义语法: [访问修饰符] class 类名 { 成员;  // 1.通过字段来描述类别信息的变量 ... ...

  4. java多线程——同步块synchronized详解

    Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本文介绍以下内容: Java同步关键字(synchronzied) 实例方法同步 静 ...

  5. easyui好例子,值得借鉴

    http://www.cnblogs.com/wuhuacong/p/3317223.html

  6. jQuery(二)

    table 全选.反选.清除 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  7. Go与Docker的几本书的作者

    http://thenewstack.io/ebookseries/http://thenewstack.io/building-a-web-server-in-go/

  8. Spring Boot 属性配置和使用(转)

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  9. poj 1068 Parencodings(栈)

    题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...

  10. XMPP--- error : linker command failed with exit code 1

    error: linker command failed with exit code 1 (use -v to see invocation) 错误原因:libidn.a文件没添加上去 解决方法:l ...