Matrix1

My Tags   (Edit)
  Source : Classical Problem
  Time limit : 5 sec   Memory limit : 64 M

Submitted : 424, Accepted : 129

Lilu0355 is a thoughtful boy and always plays a classical mathematic game.The game plays like that, there is a n * m matrix, each grid of this matrix is filled with a non-negative number. You should remove some numbers from the matrix and make sure that any two numbers removed are not adjacent in the matrix. What is the biggest sum of those removed numbers? Lilu can always find the answer, can you?

Input

The first line is a integer T indicating the number of test cases.T cases fllows. Each case includs two integers n, m(m ≤ 50,n ≤ 50) and n * m non-negative integers, which is not more than 40000.

Output

For each test case, output the biggest sum.

Sample Input

1
3 2
180 25
210 45
220 100

Sample Output

445

题意:

给出一张n*m的网格图,每个格子有一个非负权值c[i][j],选取一些两两不相邻的格子,使得权值最大...

分析:

最大权值独立集问题,网格图,所以可以黑白染色,S向黑色点连一条容量为格子权值的边,白色点向T连一条容量为格子权值的边,黑色点向相邻的白色点连一条容量为+∞的边,然后求出的最小割就是最小点权覆盖,用总权值减去最小割就是答案了...

代码:

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
#define inf 0x3f3f3f3f
using namespace std; const int maxn=++,maxm=maxn*+; int cas,n,m,mv[][]={,,,-,,,-,},ans;
int S,T,cnt,hd[maxn],fl[maxm],to[maxm],nxt[maxm],pos[maxn]; inline void add(int s,int x,int y){
fl[cnt]=s;to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
fl[cnt]=;to[cnt]=x;nxt[cnt]=hd[y];hd[y]=cnt++;
} inline bool bfs(void){
memset(pos,-,sizeof(pos));
int q[maxn],head=,tail=;
q[]=S,pos[S]=;
while(head<=tail){
int top=q[head++];
for(int i=hd[top];i!=-;i=nxt[i])
if(pos[to[i]]==-&&fl[i])
pos[to[i]]=pos[top]+,q[++tail]=to[i];
}
return pos[T]!=-;
} inline int find(int v,int f){
if(v==T)
return f;
int res=,t;
for(int i=hd[v];i!=-&&f>res;i=nxt[i])
if(pos[to[i]]==pos[v]+&&fl[i])
t=find(to[i],min(f-res,fl[i])),res+=t,fl[i]-=t,fl[i^]+=t;
if(!res)
pos[v]=-;
return res;
} inline int dinic(void){
int res=,t;
while(bfs())
while(t=find(S,inf))
res+=t;
return res;
} signed main(void){
scanf("%d",&cas);
while(cas--){
memset(hd,-,sizeof(hd));
cnt=ans=;scanf("%d%d",&n,&m);S=,T=n*m+;
for(int i=;i<=n;i++)
for(int j=,x;j<=m;j++){
scanf("%d",&x);ans+=x;
if((i+j)&){
add(x,S,(i-)*m+j);
for(int k=;k<;k++){
int xx=i+mv[k][],yy=j+mv[k][];
if(xx>&&xx<=n&&yy>&&yy<=m)
add(inf,(i-)*m+j,(xx-)*m+yy);
}
}
else
add(x,(i-)*m+j,T);
}
ans=ans-dinic();printf("%d\n",ans);
}
return ;
}

by NeighThorn

HOJ 2713 Matrix1的更多相关文章

  1. [ HOJ 2713]Matrix1[网络流] 最大点权独立集问题

    题目大意: 一个 N*M 的网格,每个单元都有一块价值 Cij 的宝石.问最多能取多少价值的宝石且任意两块宝石不相邻.(1 <= N, M <= 50, 0 <= Cij <= ...

  2. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  3. hoj 2662 经典状压dp // MyFirst 状压dp

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2662 1.引言:用dp解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态. ...

  4. HOJ 1797 Red and Black

    传送门  http://acm.hit.edu.cn/hoj/problem/view?id=1797 总体的思路是遍历可以到达的' . ',将其对应的vis数组化为1,然后统计所有为1的vis项; ...

  5. HOJ 1001: A+B; 1002: A+B+C

    两道水题,用来熟悉 HOJ 的提交系统. 1001:输入两个整数 A, B (0 <= A,B <= 10),输出 A+B. #include <iostream> using ...

  6. 【hoj】2651 pie 二分查找

    二分查找是一个非常主要的算法,针对的是有序的数列,通过中间值的大小来推断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找 ...

  7. 【BZOJ 2713】[Violet 2]愚蠢的副官&&【BZOJ1183】[Croatian2008]Umnozak——【数位DP】

    题目链接: 2713传送门 1183传送! 题解: 由于看不懂英文题解(十个单词十一个不认识……),所以只能自己想QAQ. 其实乱搞就好= =. 首先我们发现,各位数字乘积要在1e9以下才可能有用,这 ...

  8. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

  9. POJ 1964&HDU 1505&HOJ 1644 City Game(最大0,1子矩阵和总结)

    最大01子矩阵和,就是一个矩阵的元素不是0就是1,然后求最大的子矩阵,子矩阵里的元素都是相同的. 这个题目,三个oj有不同的要求,hoj的要求是5s,poj是3秒,hdu是1秒.不同的要求就对应不同的 ...

随机推荐

  1. Android通过PHP服务器实现登录

    Android客户端和PHP.MySQL搭建的服务器之间的简单交互,实现登录功能 . 实现原理图: Handler消息机制原理: Handler机制主要包括4个关键对象,分别是Message.Hand ...

  2. Android Studio git 版本回退到最新的版本

    1.场景 1.1 最新三次的提交 分别是:定义了一个变量k = 10 . 定义了一个变量 j = 6  . 定义了一个变量 i = 5 ; 本地仓库 和 远程仓库保持一致 1.2  我添加了一行代码 ...

  3. Android jni开发中的常见错误

    错误1:java.lang.UnsatisfiedLinkError: Native method not found: 本地方法没有找到 1.本地函数名写错 2.忘记加载.so文件 没有调用Syst ...

  4. clang -rewrite-objc的使用点滴

    ➠更多技术干货请戳:听云博客 clang -rewrite-objc的作用是把oc代码转写成c/c++代码,我们常用它来窥探OC的一些秘密. 1.最简单的例子 main.m的代码如下: int mai ...

  5. art.dialog.art 中,将子页面窗口中的值传递给父框架中

    artDialog.open.origin.document.getElementById('父元素ID').value=document.getElementById('子页面元素ID').valu ...

  6. windows下最好的豆瓣fm软件——K.F.storm豆瓣电台,没有之一

    哈哈,发现windows下最好的豆瓣电台啦~~~基本全部功能都能满足哈,绝对没有之一的软件--K.F.storm豆瓣电台. 官方地址: http://www.kfstorm.com/blog/doub ...

  7. redis使用笔记

    mysql---select * from table where xx;insert into table(name1,name2..) values(value1,value2);delete f ...

  8. HTML页面禁止选择、页面禁止复制、页面禁止右键

    HTML页面内容禁止选择.复制.右键刚在一个看一个站点的源代码的的时候发现的,其实原来真的很简单 <body leftmargin=0 topmargin=0 oncontextmenu='re ...

  9. MongoDB查询重复记录并保存到文件csv

    客户1w用户记录,发现里面有小部分重复数据 需要查出,比对哪些信息不同 https://docs.mongodb.org/manual/reference/operator/aggregation/# ...

  10. PMBOK(第五版)学习笔记二-十大知识领域(P87)

    五大项目管理过程组:启动.规划.执行.监控.收尾过程组 十大知识领域是:项目整合管理.项目范围管理.项目时间管理.项目成本管理.项目质量管理.项目人力资源管理.项目沟通管理.项目风险管理.项目采购管理 ...