FZU 2143 Board Game
Accept: 95 Submit: 246
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem 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
2 2 9
3 4
2 3
1 3 9
4 6 4
1 1 9
9
3 3 5
1 2 3
4 5 6
7 8 9
3 3 9
1 2 3
4 5 6
7 8 9
Sample Output
Case 2: 2
Case 3: 81
Case 4: 33
Case 5: 5
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std; const int E = ;
const int oo = 0x7fffffff;
const int inf = ;
const int maxn = ; struct edge
{
int next,v,flow,cost;
} e[E]; struct MCMF
{
int head[maxn];
queue<int> q;
int cnt, S, T;
void init(int __S,int __T)
{
S = __S;
T = __T;
memset(head,-,sizeof(head));
cnt = ;
}
void add(int u,int v,int flow,int cost)
{
e[cnt].v = v;
e[cnt].flow = flow;
e[cnt].cost = cost;
e[cnt].next = head[u];
head[u] = cnt ++;
} void AddEdge(int u,int v,int flow,int cost)
{
add(u,v,flow,cost);
add(v,u,, -cost);
} int dis[maxn],cc[maxn],visit[maxn],pre[maxn],dd[maxn]; int spfa()
{
fill(dis,dis + T + , oo);
dis[S] = ;
pre[S] = -;
q.push(S);
while(!q.empty())
{
int u = q.front();
q.pop();
visit[u] = ;
for(int i = head[u]; i != -; i = e[i].next)
{
if(e[i].flow > && dis[e[i].v] > dis[u] + e[i].cost)
{
dis[e[i].v] = dis[u] + e[i].cost;
pre[e[i].v] = u;
cc[e[i].v] = i;
dd[e[i].v] = e[i].cost;
if(!visit[e[i].v])
{
q.push(e[i].v);
visit[e[i].v] = ;
}
}
}
}
return dis[T] < ;
} int argument()
{
int aug = oo;
int u,v;
int ans = ;
for(u = pre[v = T]; v != S; v = u, u = pre[v])
if(e[cc[v]].flow < aug) aug = e[cc[v]].flow;
for(u = pre[v = T]; v != S; v = u, u = pre[v])
{
e[cc[v]].flow -= aug;
e[cc[v] ^ ].flow += aug;
ans += dd[v] * aug;
}
return ans;
} int mcmf()
{
int res = ;
memset(visit,,sizeof(visit));
while(spfa()) res += argument();
return res;
}
} MC; int N,M,K;
int b[][]; int main()
{
int cas, cast = ;
scanf("%d",&cas);
while (cas--)
{
scanf("%d%d%d",&N,&M,&K);
for (int i=;i<=N;i++)
{
for (int j=;j<=M;j++)
{
scanf("%d",&b[i][j]);
}
} int nn = N * M + , s = , t = N*M + ;
MC.init(s,t);
int ans = ;
for (int i=;i<=N;i++)
for (int j=;j<=M;j++)
{
ans += b[i][j] * b[i][j];
int p = i*M - M + j;
if ((i+j)%==){
for (int k=;k<=K;k++)
{
int tmp = * k - - * b[i][j];
MC.AddEdge(s,p, ,tmp);
}
int pp = ;
if (i<N)
{
pp = p + M;
MC.AddEdge(p,pp,inf,);
}
if (i>)
{
pp = p - M;
MC.AddEdge(p,pp,inf,);
}
if (j<M)
{
pp = p + ;
MC.AddEdge(p,pp,inf,);
}
if (j>)
{
pp = p - ;
MC.AddEdge(p,pp,inf,);
}
}else{
for (int k=;k<=K;k++)
{
int tmp = * k - - * b[i][j];
MC.AddEdge(p,t, ,tmp);
}
}
}
// MC.AddEdge(s,t,INF,0);
printf("Case %d: %d\n",++cast, ans + MC.mcmf());
}
return ;
}
FZU 2143 Board Game的更多相关文章
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- FZU 2151 OOXX Game
OOXX Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- (FZU 2150) Fire Game (bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
- (广搜)Fire Game -- FZU -- 2150
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS ...
- [LeetCode] Battleships in a Board 平板上的战船
Given an 2D board, count how many different battleships are in it. The battleships are represented w ...
- UP Board 串口使用心得
前言 原创文章,转载引用务必注明链接. 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我的博客: http://www.cnblogs.com/sjqlwy/p/up_s ...
- UP Board 网络设置一本通
前言 原创文章,转载引用务必注明链接,水平有限,欢迎指正. 本文环境:ubilinux 3.0 on UP Board 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我的 ...
随机推荐
- 二项分布 多项分布 伽马函数 Beta分布
http://blog.csdn.net/shuimu12345678/article/details/30773929 0-1分布: 在一次试验中,要么为0要么为1的分布,叫0-1分布. 二项分布: ...
- Silverlight ModelView中调用UI进程
Silverlihgt: Deployment.Current.Dispatcher.BeginInvoke wpf: App.Current.Dispatcher.Invoke
- php 获取mac地址
<?php /** * 获取机器网卡的物理(MAC)地址* 目前支持WIN/LINUX系统 * 编辑: www.jbxue.com**/ class MacAddInfo { ...
- PHP自动生成后台导航网址的最佳方法
'http://www.jbxue.com'=> '脚本学堂首页', </script>
- win32 treeview
// 1.create treeview DWORD dwStryle = WS_VISIBLE | WS_CHILD | TVS_HASLINES|TVS_SHOWSELALWAYS/*|TVS_L ...
- [转]AngularJS的$resource
转自:http://blog.csdn.net/violet_day/article/details/17403207 $http $http服务是基于$q服务的,提供了promise封装,它接受一个 ...
- SQLServer学习笔记<>相关子查询及复杂查询
二.查询缺少值的查询 在这里我们加入要查询2008年每一天的订单有多少?首先我们可以查询下订单表的订单日期在2008年的所有订单信息. 1 select distinct orderdate,coun ...
- css中transition的使用以及:before:after的使用(小样式)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ACM题目————A simple problem
Description Zty很痴迷数学问题..一天,yifenfei出了个数学题想难倒他,让他回答1 / n.但Zty却回答不了^_^. 请大家编程帮助他. Input 第一行整数T,表示测试组 ...
- miniUI 可编辑datagrid获取值的问题
<div id="variableGrid" class="mini-datagrid" borderStyle="border-right:0 ...