FZU 2143 Board Game

题意思路:见这个,写的很详细了,题解,主要难在建图和流量大于等于0的时候就停止增广


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define LL long long
const int maxn = 2005;
#define INF 1e9
struct Edge
{
	int from,to,cap,flow,cost;
	Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};
struct MCMF
{
	int n,m;
	vector<Edge>edges;
	vector<int>G[maxn];
	int inq[maxn];
	int d[maxn];
	int p[maxn];
	int a[maxn];
	void init(int n)
	{
		this->n=n;
		for (int i =0;i<n;i++)
			G[i].clear();
		edges.clear();
	}
	void AddEdge(int from,int to,int cap,int cost)
	{
		edges.push_back(Edge(from,to,cap,0,cost));
		edges.push_back(Edge(to,from,0,0,-cost));
		m = edges.size();
		G[from].push_back(m-2);
		G[to].push_back(m-1);
	}
	bool BellmanFord(int s,int t,int &flow,LL &cost)
	{
        for (int i = 0;i<n;i++)
			d[i]=INF;
		memset(inq,0,sizeof(inq));
		d[s]=0;
		inq[s]=1;
		p[s]=0;
		a[s]=INF;
		queue<int>q;
		q.push(s);
		while (!q.empty())
		{
			int u = q.front();
			q.pop();
			inq[u]=0;
			for (int i =0;i<G[u].size();i++){
				Edge&e = edges[G[u][i]];
				if (e.cap>e.flow&&d[e.to]>d[u]+e.cost){
					d[e.to]=d[u]+e.cost;
					p[e.to]=G[u][i];
					a[e.to]=min(a[u],e.cap-e.flow);
					if (!inq[e.to])
					{
						q.push(e.to);
						inq[e.to]=1;
					}
				}
			}
		}
		if (d[t]>=0)
			return false;
		if (d[t]==INF)
			return false;
		flow+=a[t];
		cost+=(LL)d[t]*(LL)a[t];
		for (int u = t;u!=s;u=edges[p[u]].from){
			edges[p[u]].flow+=a[t];
			edges[p[u]^1].flow-=a[t];
		}
		return true;
	}
	int MincostMaxflow(int s,int t,LL &cost)
	{
		int flow = 0;
		cost = 0;
		while (BellmanFord(s,t,flow,cost));
		return flow;
	}
}mc;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int main()
{
	int T,cas=1;
	scanf("%d",&T);
	while (T--)
	{
		int n,m,k;
		scanf("%d%d%d",&n,&m,&k);
		mc.init(n*m+2);
		int s = n*m;
		int t = n*m+1;
		int ans = 0;
		for (int i = 0;i<n;i++)
			for (int j = 0;j<m;j++)
			{
				int temp;
				scanf("%d",&temp);
				ans+=temp*temp;
				if ((i+j)%2==0)
				{
					for (int kk = 1;kk<=k;kk++)
						mc.AddEdge(s,i*m+j,1,2*kk-1-2*temp);
					for (int kk = 0;kk<4;kk++)
					{
						int dx = i + dir[kk][0];
						int dy = j + dir[kk][1];
						if (dx>=0 && dx<n&&dy>=0&&dy<m)
							mc.AddEdge(i*m+j,dx*m+dy,INF,0);
					}
				}
				else
				{
					for (int kk = 1;kk<=k;kk++)
						mc.AddEdge(i*m+j,t,1,2*kk-1-2*temp);
				}
			}
		LL cost=0;
		mc.MincostMaxflow(s,t,cost);
		printf("Case %d: %d\n",cas++,cost+ans);
	}
}


Description

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of the board which is own by Fat brother is consisting of an integer 0. At each turn, he can choose two adjacent grids and add both the integer inside them by 1. But due to some unknown reason, the number of each grid can not be large than a given integer K. Also, Maze has already drown an N*M board with N*M integers inside each grid. What Fat brother would like to do is adding his board to be as same as Maze’s. Now we define the different value of two boards A and B as:

Now your task is to help Fat brother the minimal value of S he can get.

Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains three integers N, M and K which are mention above. Then N lines with M integers describe the board.

1 <= T <= 100, 1 <= N, M, K <= 9

0 <= the integers in the given board <= 9

Output

For each case, output the case number first, then output the minimal value of S Fat brother can get.

Sample Input

52 2 93 42 31 3 94 6 41 1 993 3 51 2 34 5 67 8 93 3 91 2 34 5 67 8 9

Sample Output

Case 1: 0Case 2: 2Case 3: 81Case 4: 33Case 5: 5





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值