★ 方格取数3 二分图的最大点权独立集
2014-09-03 19:23
309 查看
题目描述 Description
«问题描述:在一个有m*n 个方格的棋盘中,每个方格中有一个正整数。现要从方格中取数,使任
意2 个数所在方格没有公共边,且取出的数的总和最大。试设计一个满足要求的取数算法。
«编程任务:
对于给定的方格棋盘,按照取数要求编程找出总和最大的数。
输入描述 Input Description
第1 行有2 个正整数m和n,分别表示棋盘的行数和列数。接下来的m行,每行有n个正整数,表示棋盘方格中的数。
输出描述 Output Description
将取数的最大总和输出
样例输入 Sample Input
3 31 2 3
3 2 3
2 3 1
样例输出 Sample Output
11
数据范围及提示 Data Size & Hint
n,m<=30题目可以在这里提交:http://wikioi.com/homework/23/
分析:二分图的最大点权独立集模型,将矩阵里面每个点拆成两个点,i和i+n*m,然后S向每个i建边,权值为该点的值,然后每个i+n*m向汇点T建边,权值为该点的值,然后每个点向它周围四个点建边,权值为无穷大,跑最大流即可。
代码:
//Isap算法,复杂度O(n^2m) #pragma comment(linker,"/STACK:102400000,102400000") #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <vector> #include <string> #include <math.h> #include <queue> #include <stack> #include <map> #include <set> using namespace std; typedef long long ll; //记得必要的时候改成无符号 const int maxn=2000; const int maxm=1000005; const int INF=1000000000; struct EdgeNode { int from; int to; int cost; int next; }edge[maxm]; int head[maxn],cnt; void add(int x,int y,int z) { edge[cnt].from=x;edge[cnt].to=y;edge[cnt].cost=z;edge[cnt].next=head[x];head[x]=cnt++; edge[cnt].from=y;edge[cnt].to=x;edge[cnt].cost=0;edge[cnt].next=head[y];head[y]=cnt++; } void init() { cnt=0; memset(head,-1,sizeof(head)); } int S,T,n,m; int d[maxn],gap[maxn],curedge[maxn],pre[maxn]; //curedge[]为当前弧数组,pre为前驱数组 int sap(int S,int T,int n) //n为点数 { int cur_flow,flow_ans=0,u,tmp,neck,i; memset(d,0,sizeof(d)); memset(gap,0,sizeof(gap)); memset(pre,-1,sizeof(pre)); for(i=0;i<=n;i++)curedge[i]=head[i]; //初始化当前弧为第一条邻接表 gap[0]=n; u=S; while(d[S]<n) //当d[S]>=n时,网络中肯定出现了断层 { if(u==T) { cur_flow=INF; for(i=S;i!=T;i=edge[curedge[i]].to) { //增广成功,寻找瓶颈边 if(cur_flow>edge[curedge[i]].cost) { neck=i; cur_flow=edge[curedge[i]].cost; } } for(i=S;i!=T;i=edge[curedge[i]].to) { //修改路径上的边容量 tmp=curedge[i]; edge[tmp].cost-=cur_flow; edge[tmp^1].cost+=cur_flow; } flow_ans+=cur_flow; u=neck; //下次增广从瓶颈边开始 } for(i=curedge[u];i!=-1;i=edge[i].next) if(edge[i].cost&&d[u]==d[edge[i].to]+1) break; if(i!=-1) { curedge[u]=i; pre[edge[i].to]=u; u=edge[i].to; } else { if(0==--gap[d[u]])break; //gap优化 curedge[u]=head[u]; for(tmp=n,i=head[u];i!=-1;i=edge[i].next) if(edge[i].cost) tmp=min(tmp,d[edge[i].to]); d[u]=tmp+1; ++gap[d[u]]; if(u!=S)u=pre[u]; //重标号并且从当前点前驱重新增广 } } return flow_ans; } int mp[35][35]; int dis[5][3]={{0,1},{0,-1},{1,0},{-1,0}}; int yj(int x,int y) { if(x<1||x>n||y<1||y>m)return 1; return 0; } void build() { int i,j,k,x,y,t=n*m,a,b; for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ a=(i-1)*m+j; add(S,a,mp[i][j]); add(a+t,T,mp[i][j]); } } for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ for(k=0;k<4;k++){ x=i+dis[k][0]; y=j+dis[k][1]; if(!yj(x,y)){ a=(i-1)*m+j; b=(x-1)*m+y; add(a,b+t,INF); } } } } } int main() { int ans,i,j; while(~scanf("%d%d",&n,&m)) { init(); S=0; T=n*m*2+1; ans=0; for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&mp[i][j]),ans+=mp[i][j]; build(); printf("%d\n",ans-sap(S,T,T+1)/2); } return 0; }
相关文章推荐
- 【codevs1907】【方格取数3】二分图最大带权独立集
- LOJ6007 「网络流 24 题 - 8」 方格取数 二分图最大点权独立集
- 【codevs1907】方格取数3 最大点权独立集
- nefu 482 方格取数问题 二分图最大点权独立集
- nefu 482 方格取数问题 二分图最大点权独立集
- hdu1569 方格取数(2) 二分图最大点权独立集
- hdu 1569 方格取数——最大点权独立集
- 【网络流24题】方格取数(二分图染色+最大权独立点集+最小割)
- HDU 1565 方格取数(1) 二分图最大点权独立集
- HDU 1565 方格取数(1)(最大独立点集)
- bzoj3175 [ TJOI2013 ] -- 二分图最大点独立集
- hdu 1569 方格取数(2) 最大点权独立集
- hdu 4619 二分图最大匹配 ——最大独立集
- hdu1565 方格取数 最大流(二分图极大点权独立集) 或状态压缩dp
- zoj - 1137 - Girls and Boys(二分图最大独立点集)
- 独立最小【网络流第五弹】最大点权独立集 ——HDU 1569 方格取数(2)
- 二分图----最大匹配,最小点覆盖,最大点独立集
- [hdu1569]方格取数(2) 最大点权独立集
- hdu 1569 方格取数(2) 网络流 最大点权独立集
- BZOJ 1475: 方格取数 最大点权独立集