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

5
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 1: 0
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的更多相关文章

  1. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  2. ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

    FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. FZU 2151 OOXX Game

    OOXX Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  4. (FZU 2150) Fire Game (bfs)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...

  5. FZU 2150 Fire Game(点火游戏)

    FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description - 题目描述 ...

  6. (广搜)Fire Game -- FZU -- 2150

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS    ...

  7. [LeetCode] Battleships in a Board 平板上的战船

    Given an 2D board, count how many different battleships are in it. The battleships are represented w ...

  8. UP Board 串口使用心得

    前言 原创文章,转载引用务必注明链接. 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我的博客: http://www.cnblogs.com/sjqlwy/p/up_s ...

  9. UP Board 网络设置一本通

    前言 原创文章,转载引用务必注明链接,水平有限,欢迎指正. 本文环境:ubilinux 3.0 on UP Board 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我的 ...

随机推荐

  1. 夺命雷公狗jquery---4内容选择器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 查看linux的出错信息

    先执行:dmesg -c > /dev/null 该命令是把之前的一些信息删除,-c选项表示:Clear the ring buffer after first printing its con ...

  3. TortoiseSVN,排除不想提交文件的方法(转)

    转自:tortoisesvnsubversionfilebuilddialoglist 下面是英文帮助: 利用TortoiseSVN的修改列表 功能可以实现,在新版本中TortoiseSVN特地预置了 ...

  4. Linux修改系统以及pip更新源

    Linux修改系统以及pip更新源 时间:2015-08-01来源:csdn 作者:henulwj 修改系统更新源 你是否跟我一样在刚看时接触Linux时被系统更新源问题搞得晕头转向,不同的Linux ...

  5. 所谓完整的linux系统包括哪些部分呢?【转】

    本文转载自:http://www.eeskill.com/article/index/id/1358.html 简介:三部分:bootloader.linux kernel(linux内核).root ...

  6. jquery plugins

    jQuery官网插件 jQuery自定义滚动条样式插件 jQuery custom content scroller examples Twitter typeahead typeahead.js t ...

  7. POSTGRESQL9.5之pg_rman工具

    pg_rman是一款专门为postgresql设计的在线备份恢复的工具.其支持在线和基于时间点备份方式,还可以通过创建backup catalog来维护DB cluster备份信息. 看起来好像是模仿 ...

  8. Android Studio解决unspecified on project app resolves to an APK archive which is not supported

    出现该问题unspecified on project app resolves to an APK archive which is not supported as a compilation d ...

  9. sass初步认识1

    sass是一种“css预处理器”,同类的还有less等,方法类似.css预处理器的基本思想是,用一种专门的编程语言,进行网页样式设计,然后再编译成正常的css文件. 使用sass需要先暗转RUBY,再 ...

  10. XPath 元素及属性查找

    实例 1 基本的XPath语法类似于在一个文件系统中定位文件,如果路径以斜线 / 开始, 那么该路径就表示到一个元素的绝对路径 /AAA 选择根元素AAA       <AAA>      ...