HDU 1565

方格取数(1)

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5961    Accepted Submission(s): 2268

Problem Description
给你一个n*n的格子的棋盘,每个格子里面有一个非负数。
从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的和最大。
 
Input
包括多个测试实例,每个测试实例包括一个整数n 和n*n个非负数(n<=20)
 
Output
对于每个测试实例,输出可能取得的最大的和
  
Sample Input
3
75 15 21
75 15 28
34 70 5
 
Sample Output
188
 
最大点权独立集
弱弱的EK算法
邻接矩阵实现 124MS
邻接表实现 15MS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define N 510 int n;
int src;
int des;
int sum;
int pre[N];
int mpt[N][N];
int map[N][N];
int dir[][]={,,-,,,,,-};
queue<int> q; bool bfs()
{
while(!q.empty()) q.pop();
memset(pre,-,sizeof(pre));
pre[src]=;
q.push(src);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int v=;v<=n*n+;v++)
{
if(pre[v]==- && mpt[u][v]>)
{
pre[v]=u;
if(v==des) return ;
q.push(v);
}
}
}
return ;
}
int EK()
{
int maxflow=;
while(bfs())
{
int minflow=INF;
for(int i=des;i!=src;i=pre[i])
minflow=min(minflow,mpt[pre[i]][i]);
maxflow+=minflow;
for(int i=des;i!=src;i=pre[i])
{
mpt[pre[i]][i]-=minflow;
mpt[i][pre[i]]+=minflow;
}
}
return maxflow;
}
int main()
{
int i,j,k;
while(scanf("%d",&n)!=EOF)
{
sum=;
memset(mpt,,sizeof(mpt));
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
scanf("%d",&map[i][j]);
sum+=map[i][j];
}
}
src=;
des=n*n+;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if((i+j)%==) mpt[src][(i-)*n+j]=map[i][j];
else mpt[(i-)*n+j][des]=map[i][j];
}
}
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if((i+j)%==)
{
for(k=;k<;k++)
{
int x=i+dir[k][];
int y=j+dir[k][];
if(x>= && x<=n && y>= && y<=n)
{
mpt[(i-)*n+j][(x-)*n+y]=INF;
}
}
}
}
}
printf("%d\n",sum-EK());
}
return ;
}

HDU 1569

和方格取数1数据大小不一样,用上面的会超时,可能是写搓了。

邻接表实现 280ms

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define N 20510 struct Edge
{
int to,next,val;
}edge[N];
int tot;
int head[N]; int n;
int m;
int src;
int des;
int sum;
int pre[N];
int path[N];
int map[N][N];
int dir[][]={,,-,,,,,-};
queue<int> q; void init()
{
tot=;
memset(head,-,sizeof(head));
}
void add(int u,int v,int w)
{
edge[tot].to=v;
edge[tot].val=w;
edge[tot].next=head[u];
head[u]=tot++; edge[tot].to=u;
edge[tot].val=;
edge[tot].next=head[v];
head[v]=tot++; }
bool bfs()
{
while(!q.empty()) q.pop();
memset(pre,-,sizeof(pre));
pre[src]=;
q.push(src);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(pre[v]==- && edge[i].val>)
{
pre[v]=u;
path[v]=i;
if(v==des) return ;
q.push(v);
}
}
}
return ;
}
int EK()
{
int maxflow=;
while(bfs())
{
int minflow=INF;
for(int i=des;i!=src;i=pre[i])
minflow=min(minflow,edge[path[i]].val);
maxflow+=minflow;
for(int i=des;i!=src;i=pre[i])
{
edge[path[i]].val-=minflow;
edge[path[i]^].val+=minflow;
}
}
return maxflow;
}
int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
sum=;
init();
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
scanf("%d",&map[i][j]);
sum+=map[i][j];
}
}
src=;
des=n*m+;
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if((i+j)%==) add(src,(i-)*m+j,map[i][j]);
else add((i-)*m+j,des,map[i][j]);
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if((i+j)%==)
{
for(k=;k<;k++)
{
int x=i+dir[k][];
int y=j+dir[k][];
if(x>= && x<=n && y>= && y<=m)
{
add((i-)*m+j,(x-)*m+y,INF);
}
}
}
}
}
printf("%d\n",sum-EK());
}
return ;
}

[HDU 1565+1569] 方格取数的更多相关文章

  1. HDU 1565 1569 方格取数(最大点权独立集)

    HDU 1565 1569 方格取数(最大点权独立集) 题目链接 题意:中文题 思路:最大点权独立集 = 总权值 - 最小割 = 总权值 - 最大流 那么原图周围不能连边,那么就能够分成黑白棋盘.源点 ...

  2. HDU 1565&1569 方格取数系列(状压DP或者最大流)

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  3. HDU 1565:方格取数(1)(最大点权独立集)***

    http://acm.hdu.edu.cn/showproblem.php?pid=1565 题意:中文. 思路:一个棋盘,要使得相邻的点不能同时选,问最大和是多少,这个问题就是最大点权独立集. 可以 ...

  4. 网络流(最大流) HDU 1565 方格取数(1) HDU 1569 方格取数(2)

      HDU 1565 方格取数(1) 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数的 ...

  5. HDU 1569 方格取数(2)

    方格取数(2) Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 15 ...

  6. HDU 1569 方格取数(2) (最小割)

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. HDU 1569 - 方格取数(2) - [最大点权独立集与最小点权覆盖集]

    嗯,这是关于最大点权独立集与最小点权覆盖集的姿势,很简单对吧,然后开始看题. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1569 Time Limi ...

  8. HDU 1569 方格取数(2)(最大流最小割の最大权独立集)

    Description 给你一个m*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取数所在的2个格子不能相邻,并且取出的数的和最大.   ...

  9. hdu - 1565 方格取数(1) && 1569 方格取数(2) (最大点权独立集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1565 两道题只是数据范围不同,都是求的最大点权独立集. 我们可以把下标之和为奇数的分成一个集合,把下标之和为偶数 ...

随机推荐

  1. Java实战之02Hibernate-03Session中的常用方法

    九.Session中的常用方法 1.save方法 都是临时态————>持久态 2.persist方法 作用: 持久化临时态对象. 与save方法的区别: 开始了事务:persist和save没有 ...

  2. C++学习之路,漫长而遥远

    一.C/C++语言 如果你的基础很差, 建议不要一开始就学C++语言,从C开始学起,对程序有个初步的认识,循序渐进.C语言的书嘛,先买一本 300 页以内的,把书中的每一个例子都通过键盘敲打进去到 V ...

  3. Apache的多路处理模块MPM:Prefork Worker Event

    如何确认当前apache使用哪种模式 通过/etc/init.d/httpd中的来确认系统apache的运行脚本路径 apachectl=/usr/sbin/apachectl httpd=${HTT ...

  4. IOC----LightInject

    开源项目 引入 LightInject.cs 默认服务 new ServiceContainer 注册跟获取获取服务 container.Register<IFoo, Foo>();con ...

  5. PHPCMS如何实现后台访问限制?

    修改phpcms 后台管理路径,可以有效的防止因为程序漏洞,或者是服务器安全问题所带来的 phpcms系统管理权限被黑客获取 文件路径:/caches/config/system.php 修改:'ad ...

  6. 安装Android Studio报failed to find java version for 'C:\windows\system32\java.exe':[2] The system cannot find the specified file.错误的解决方案

    方案很简单,找到SYSTEM32目录下的java.exe文件,重命名为java.exe.orj. 方案出处:http://stackoverflow.com/questions/10339679/an ...

  7. linq query, using int.parse to convert varchar to int while orderby

    var t = from x in context.NewsLetterItem.ToList() //add .ToList at this place where x.APPId == appid ...

  8. node.js 1Million concurrent connections!

    https://github.com/ashtuchkin/node-millenium http://blog.caustik.com/2012/08/19/node-js-w1m-concurre ...

  9. angular2地址栏路由配置

    一步一步route过去可以,地址栏直接写url怎么就找不到了呢? 这关乎于Nodejs的express路由规则(http://hm4123660.iteye.com/blog/2195035) exp ...

  10. 从ng-repeat到NgFor

    看这篇文章基本明白怎么渲染模板,但是我的工程会报错说#号非法,这篇的写法也不好用. angular2.0.0的语法集: Angular for TypeScript 语法快速指南 (基于2.0.0版本 ...