P4474 王者之剑

题目大意

n*m的带权网格,任意选择起点
开始时刻为0秒。以下操作,每秒按顺序执行

  1. 在第i秒开始的时候,在方格(x,y)上,获得(x,y)的值
  2. 在偶数秒,周围四格的值清零
  3. 每秒可选择不动或走一格

求最大值

易证:选择获得某格上的值时,周围四格就不能获得了------二分图最大点权独立集

网格------奇偶分类

网络流跑最小割

My complete code: 

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<string>
  5. #include<queue>
  6. using namespace std;
  7. typedef long long LL;
  8. const LL maxn=500000;
  9. const LL inf=0x3f3f3f3f;
  10. const LL dx[5]={0,-1,0,1,0};
  11. const LL dy[5]={0,0,1,0,-1};
  12. inline LL read(){
  13. LL x=0,f=1; char c=getchar();
  14. while(c<'0'||c>'9'){
  15. if(c=='-') f=-1; c=getchar();
  16. }
  17. while(c>='0'&&c<='9'){
  18. x=x*10+c-'0'; c=getchar();
  19. }return x*f;
  20. }
  21. struct node{
  22. LL to,next,flow;
  23. }dis[maxn];
  24. LL num=-1,s,t,n,m,ans;
  25. LL head[maxn],dep[maxn];
  26. inline void Add(LL u,LL v,LL flow){
  27. dis[++num]=(node){v,head[u],flow}; head[u]=num;
  28. }
  29. inline bool Bfs(){
  30. memset(dep,0,sizeof(dep));
  31. queue<LL> que;
  32. que.push(s);
  33. dep[s]=1;
  34. while(que.size()){
  35. LL u=que.front();
  36. que.pop();
  37. for(LL i=head[u];i!=-1;i=dis[i].next){
  38. LL v=dis[i].to;
  39. if(!dep[v]&&dis[i].flow){
  40. dep[v]=dep[u]+1;
  41. que.push(v);
  42. }
  43. }
  44. }
  45. return dep[t];
  46. }
  47. LL Dfs(LL u,LL flow){
  48. if(u==t)
  49. return flow;
  50. LL tmp=flow;
  51. for(LL i=head[u];i!=-1;i=dis[i].next){
  52. LL v=dis[i].to;
  53. if(dep[v]==dep[u]+1&&dis[i].flow&&tmp){
  54. LL now=Dfs(v,min(tmp,dis[i].flow));
  55. if(now){
  56. dis[i].flow-=now;
  57. dis[i^1].flow+=now;
  58. tmp-=now;
  59. }else
  60. dep[v]=inf;
  61. }
  62. }
  63. return flow-tmp;
  64. }
  65. inline LL Dinic(){
  66. LL sum=0;
  67. while(Bfs())
  68. sum+=Dfs(s,inf);
  69. return sum;
  70. }
  71. int main(){
  72. n=read(); m=read();
  73. s=0; t=n*m+1;
  74. memset(head,-1,sizeof(head));
  75. for(LL i=1;i<=n;++i)
  76. for(LL j=1;j<=m;++j){
  77. LL val=read();
  78. ans+=val;
  79. if((i+j)&1){
  80. Add(s,(i-1)*m+j,val);
  81. Add((i-1)*m+j,s,0);
  82. }else{
  83. Add((i-1)*m+j,t,val);
  84. Add(t,(i-1)*m+j,0);
  85. }
  86. }
  87.  
  88. for(LL i=1;i<=n;++i)
  89. for(LL j=1;j<=m;++j){
  90. if((i+j)&1){
  91. for(LL k=1;k<=4;++k){
  92. LL nx=i+dx[k],ny=j+dy[k];
  93. if(nx<1||nx>n||ny<1||ny>m)
  94. continue;
  95. Add((i-1)*m+j,(nx-1)*m+ny,inf);
  96. Add((nx-1)*m+ny,(i-1)*m+j,0);
  97. }
  98. }
  99. }
  100. ans-=Dinic();
  101. printf("%lld",ans);
  102. return 0;
  103. }

  

P4474 王者之剑的更多相关文章

  1. 洛咕 P4474 王者之剑

    宝石只能在偶数秒取到,假设有一个宝石在奇数秒取到了,那么上一秒是偶数秒,在上一秒的时候这里的宝石就没了. 相邻的两个宝石不能同时取,很显然,先取一块,那么这是偶数秒,取完了这一块之后相邻的都没了. 只 ...

  2. 【BZOJ-1324】Exca王者之剑 最小割

    1324: Exca王者之剑 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 483  Solved: 248[Submit][Status][Disc ...

  3. BZOJ1324: Exca王者之剑

    1324: Exca王者之剑 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 256  Solved: 131[Submit][Status] Desc ...

  4. 图论(二分图最大权独立点集):COGS 2051. 王者之剑

    2051. 王者之剑 ★★★☆   输入文件:Excalibur.in   输出文件:Excalibur.out   简单对比 时间限制:1 s   内存限制:256 MB [题目描述] 这是在阿尔托 ...

  5. BZOJ 1324: Exca王者之剑

    1324: Exca王者之剑 Description Input 第一行给出数字N,M代表行列数.N,M均小于等于100 下面N行M列用于描述数字矩阵 Output 输出最多可以拿到多少块宝石 Sam ...

  6. [COGS 2051] 王者之剑

    Saber大法吼 2051. 王者之剑 ★★★☆   输入文件:Excalibur.in   输出文件:Excalibur.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述 ...

  7. [bzoj1324]Exca王者之剑_最小割

    Exca王者之剑 bzoj-1324 题目大意:题目链接. 注释:略. 想法: 最小割经典模型. 所有格子向源点连权值为格子权值的边. 将棋盘黑白染色后白点反转源汇. 如果两个格子相邻那么黑点向白点连 ...

  8. 【BZOJ】【1324】王者之剑

    网络流/二分图最大点权独立集 Amber(胡伯涛)论文<最小割模型在信息学竞赛中的应用>中的例题…… 感觉这个好神啊,果然是一切皆为网络流……这转化太神奇了 /************** ...

  9. bzoj 1324 Exca王者之剑(黑白染色,最小割)

    [题意] 两相邻点不能同时选,选一个点集使得权值和最大. 出题人语文好... [思路] 将图进行黑白二染色,然后构建最小割模型. [代码] #include<set> #include&l ...

随机推荐

  1. windows核心编程 DLL技术 【转】

    注:本文章转载于网络,源地址为:http://blog.csdn.net/ithzhang/article/details/7051558 本篇文章将介绍DLL显式链接的过程和模块基地址重定位及模块绑 ...

  2. newlisp HTTP Basic Authentication

    HTTP Basic Authentication原来很easy,參考文档:http://zh.wikipedia.org/wiki/HTTP%E5%9F%BA%E6%9C%AC%E8%AE%A4%E ...

  3. 【Python】使用制表符换行符来添加空白

    在编程中,在打印时,有时候需要显示出来的数据看着舒服一点,那么使用制表符(\t).换行符(\n)即可轻松实现 >>> print('zhangsan')zhangsan 加入制表符后 ...

  4. RelativeLayout布局(仅在RelativeLayout中有效)

    在父亲布局的相对位置 android:layout_alignParentLeft="true"     //在布局左边 android:layout_alignParentRig ...

  5. 用递归法将一个整数n转换成字符串。

    用递归法将一个整数n转换成字符串. 比如,输入483,应输出字符串"483".n的位数不确定,能够是随意位数的整数. #include "stdafx.h" # ...

  6. leetCode 61.Rotate List (旋转链表) 解题思路和方法

    Rotate List  Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...

  7. MVC总结--MVC简单介绍以及和WebForm差别

    什么是MVC MVC(Model-View-Controller.模型-视图-控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Cont ...

  8. OpenCV视频读取播放,视频转换为图片

    转载请注明出处!!! http://blog.csdn.net/zhonghuan1992 OpenCV视频读取播放,视频转换为图片 介绍几个有关视频读取的函数: VideoCapture::Vide ...

  9. HDU1845Jimmy’s Assignment(无向图,最大匹配)

    题意:就是求最大匹配 #include<cstdio> #include<iostream> #include<algorithm> #include<cma ...

  10. MagicalRecord使用教程【转载】

    原文地址:http://www.ithao123.cn/content-96403.html 下面是在xcode5.1下ARC环境中的使用教程 1. 将 MagicalRecord 文件夹拖入到工程文 ...