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. 1
  2. 3 2
  3. 180 25
  4. 210 45
  5. 220 100

Sample Output

  1. 445

题意:

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

分析:

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

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdio>
  5. //by NeighThorn
  6. #define inf 0x3f3f3f3f
  7. using namespace std;
  8.  
  9. const int maxn=++,maxm=maxn*+;
  10.  
  11. int cas,n,m,mv[][]={,,,-,,,-,},ans;
  12. int S,T,cnt,hd[maxn],fl[maxm],to[maxm],nxt[maxm],pos[maxn];
  13.  
  14. inline void add(int s,int x,int y){
  15. fl[cnt]=s;to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
  16. fl[cnt]=;to[cnt]=x;nxt[cnt]=hd[y];hd[y]=cnt++;
  17. }
  18.  
  19. inline bool bfs(void){
  20. memset(pos,-,sizeof(pos));
  21. int q[maxn],head=,tail=;
  22. q[]=S,pos[S]=;
  23. while(head<=tail){
  24. int top=q[head++];
  25. for(int i=hd[top];i!=-;i=nxt[i])
  26. if(pos[to[i]]==-&&fl[i])
  27. pos[to[i]]=pos[top]+,q[++tail]=to[i];
  28. }
  29. return pos[T]!=-;
  30. }
  31.  
  32. inline int find(int v,int f){
  33. if(v==T)
  34. return f;
  35. int res=,t;
  36. for(int i=hd[v];i!=-&&f>res;i=nxt[i])
  37. if(pos[to[i]]==pos[v]+&&fl[i])
  38. t=find(to[i],min(f-res,fl[i])),res+=t,fl[i]-=t,fl[i^]+=t;
  39. if(!res)
  40. pos[v]=-;
  41. return res;
  42. }
  43.  
  44. inline int dinic(void){
  45. int res=,t;
  46. while(bfs())
  47. while(t=find(S,inf))
  48. res+=t;
  49. return res;
  50. }
  51.  
  52. signed main(void){
  53. scanf("%d",&cas);
  54. while(cas--){
  55. memset(hd,-,sizeof(hd));
  56. cnt=ans=;scanf("%d%d",&n,&m);S=,T=n*m+;
  57. for(int i=;i<=n;i++)
  58. for(int j=,x;j<=m;j++){
  59. scanf("%d",&x);ans+=x;
  60. if((i+j)&){
  61. add(x,S,(i-)*m+j);
  62. for(int k=;k<;k++){
  63. int xx=i+mv[k][],yy=j+mv[k][];
  64. if(xx>&&xx<=n&&yy>&&yy<=m)
  65. add(inf,(i-)*m+j,(xx-)*m+yy);
  66. }
  67. }
  68. else
  69. add(x,(i-)*m+j,T);
  70. }
  71. ans=ans-dinic();printf("%d\n",ans);
  72. }
  73. return ;
  74. }

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. 线程安全、数据同步之 synchronized 与 Lock

    本文Demo下载传送门 写在前面 本篇文章讲的东西都是Android开源网络框架NoHttp的核心点,当然线程.多线程.数据安全这是Java中就有的,为了运行快我们用一个Java项目来讲解. 为什么要 ...

  2. Java中"\t"表示几个空格

    \t是补全当前字符串长度到8的整数倍,最少1个最多8个空格补多少要看你\t前字符串长度 测试程序: 测试结果: 总结:运行到“\t”时,判断当前字符串长度,将当前字符串长度补到8的倍数(不包括0).

  3. drawable以及Bitmap的基本操作

    一.drawable  图形对象,可以转载常用格式的图像,可能是(位图)Bitmapdrawable,或者shapedrawable(图形),还可能是多种其他图片格式GIF,PNG,JEPG 二.Bi ...

  4. 接口测试中三种传参请求(Map、request、Integer)解析

    注册企业接口传入的是一个request,查询企业接口传入的是一个integer:根据名称和国家名称模糊匹配接口传入的是一个Map:针对三种不同的传参我怎么作接口测试呢? 1 package com.w ...

  5. MVC中使用SignaIR入门教程

    一.前言:每次写总要说一点最近的感想 进入工作快半年了,昨天是最郁闷的一天,我怀疑我是不是得了"星期一综合征",每个星期一很没有状态.全身都有点酸痛,这个可能一个星期只有周末才打一 ...

  6. Linux下防火墙开启相关端口及查看已开启端口

    最近利用Apache Mina实现了一个http服务,发布到linux下发现无法访问,通过HttpClient来发送http请求时,报如下错误: Exception in thread "m ...

  7. mysql5.6主从参数详解

    mysql5.6的主从相当的不错,增加了不少参数,提升了主从同步的安全和效率,以下是mysql5.6主从参数详解. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  8. C#正则表达式开源工具,为.net开源绵尽薄力

    先交代一下背景,最近工作中经常用到正则表达式,而正则表达式这个东西我个人觉得很鸡肋,不用吧,有些功能实现起来会很麻烦.用吧,又不是说工作中经常用到,只是有时候有些需要求用到而已.但是正则表达式只要一段 ...

  9. InnoDB源码分析--缓冲池(二)

    转载请附原文链接:http://www.cnblogs.com/wingsless/p/5578727.html 上一篇中我简单的分析了一下InnoDB缓冲池LRU算法的相关源码,其实说不上是分析,应 ...

  10. spark mllib配置pom.xml错误 Multiple markers at this line Could not transfer artifact net.sf.opencsv:opencsv:jar:2.3 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org

    刚刚spark mllib,在maven repository网站http://mvnrepository.com/中查询mllib后得到相关库的最新dependence为: <dependen ...