onmylove has invented a game on n × m grids. There is one positive integer on each grid. Now you can take the numbers from the grids to make your final score as high as possible. The way to get score is like
the following:
● At the beginning, the score is 0;
● If you take a number which equals to x, the score increase x;
● If there appears two neighboring empty grids after you taken the number, then the score should be decreased by 2(x&y). Here x and y are the values used to existed on these two grids. Please pay attention that “neighboring grids” means there exits and only exits one common border between these two grids.
Since onmylove thinks this problem is too easy, he adds one more rule:
● Before you start the game, you are given some positions and the numbers on these positions must be taken away.
Can you help onmylove to calculate: what’s the highest score onmylove can get in the game?
Input
Multiple input cases. For each case, there are three integers n, m, k in a line.
n and m describing the size of the grids is n ×m. k means there are k positions of which you must take their numbers. Then following n lines, each contains m numbers, representing the numbers on the n×m grids.Then k lines follow. Each line contains two integers, representing the row and column of one position
and you must take the number on this position. Also, the rows and columns are counted start from 1.
Limits: 1 ≤ n, m ≤ 50, 0 ≤ k ≤ n × m, the integer in every gird is not more than 1000.
Output
For each test case, output the highest score on one line.
Sample Input
2 2 1
2 2
2 2
1 1
2 2 1
2 7
4 1
1 1
Sample Output
4
9
Hint
As to the second case in Sample Input, onmylove gan get the highest score when calulating like this:
2 + 7 + 4 - 2 × (2&4) - 2 × (2&7) = 13 - 2 × 0 - 2 × 2 = 9.
上一题的加强版~~~
这题不能用最大独立点权来理解,而是用最小割来理解这个题比较好
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=2505;
const int maxx=20005;
int edge;
int to[maxx],flow[maxx],nex[maxx];
int head[maxn];
void addEdge(int v,int u,int cap)
{
to[edge]=u,flow[edge]=cap,nex[edge]=head[v],head[v]=edge++;
to[edge]=v,flow[edge]=0,nex[edge]=head[u],head[u]=edge++;
}
int vis[maxn];
int pre[maxn];
bool bfs(int s,int e)
{
queue<int> que;
pre[s]=-1;
memset(vis,-1,sizeof(vis));
que.push(s);
vis[s]=0;
while(!que.empty())
{
int u=que.front();
que.pop();
for(int i=head[u];~i;i=nex[i])
{
int v=to[i];
if(vis[v]==-1&&flow[i])
{
vis[v]=vis[u]+1;
if(v==e)
return true;
que.push(v);
}
}
}
return false;
}
int dfs(int s,int t,int f)
{
if(s==t||!f)
return f;
int r=0;
for(int i=head[s];~i;i=nex[i])
{
int v=to[i];
if(vis[v]==vis[s]+1&&flow[i])
{
int d=dfs(v,t,min(f,flow[i]));
if(d>0)
{
flow[i]-=d;
flow[i^1]+=d;
r+=d;
f-=d;
if(!f)
break;
}
}
}
if(!r)
vis[s]=INF;
return r;
}
int maxFlow(int s ,int e)//然后直接调用这个即可
{
int ans=0;
while(bfs(s,e))
ans+=dfs(s,e,INF);
return ans;
}
void init()//记得每次使用前初始化
{
memset(head,-1,sizeof(head));
edge=0;
}
int b[][2]={{0,1},{0,-1},{1,0},{-1,0}};
int a[55][55];
bool flag[55][55];
int main()
{
int n,m,k;
int x,y;
while(scanf("%d%d%d",&n,&m,&k)==3)
{
int total=0;
memset(flag,0,sizeof(flag));
init();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&a[i][j]),total+=a[i][j];
while(k--)
{
scanf("%d%d",&x,&y);
flag[x][y]=true;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if((i+j)&1)
{
addEdge(0,(i-1)*m+j,flag[i][j]==0?a[i][j]:INF);
for(int k=0;k<4;k++)
{
int xx=i+b[k][0];
int yy=j+b[k][1];
if(xx<1||xx>n||yy<1||yy>m)
continue;
addEdge((i-1)*m+j,(xx-1)*m+yy,2*(a[i][j]&a[xx][yy]));
}
}
else
addEdge((i-1)*m+j,n*m+1,flag[i][j]==0?a[i][j]:INF);
}
printf("%d\n",total-maxFlow(0,n*m+1));
}
return 0;
}