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. 搞不定linux下的无线网卡驱动的权宜之计

    毕竟windows用了这么些年了,对windows下的一些东西也比较熟悉,还有就是windows的软件方式比较傻瓜. 在linux下搞不定无线网卡啊,幸亏有甲骨文的virtualbox,咱虚拟一个xp ...

  2. 【pyQuery分析实例】分析体育网冠军联盟比赛成绩

    目标地址:http://www.espncricinfo.com/champions-league-twenty20-2012/engine/match/574265.html liz@nb-liz: ...

  3. repeater做删除前弹窗询问

    前台 <asp:LinkButton ID="delLinkButton" runat="server" OnClientClick='return co ...

  4. 【海岛帝国系列赛】No.4 海岛帝国:LYF的太空运输站

    50212228海岛帝国:LYF的太空运输站 [试题描述] 最近,“购物券”WHT在“药师傅”帝国资源大会上提出了“SSTS”太空运输站计划.由于恐怖分子前些日子刚猖狂完,炸毁高楼无数,YSF不得不执 ...

  5. Spark实战2:Zeppelin的安装和SparkSQL使用总结

    zeppelin是spark的web版本notebook编辑器,相当于ipython的notebook编辑器. 一Zeppelin安装 (前提是spark已经安装好) 1 下载https://zepp ...

  6. android 项目学习随笔十四(WebView)

    1.布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

  7. 读 《JavaScript: The Good Parts》 有感

    提炼出一门语言或技术的 Good Parts, 使用该子集去构造健壮稳固的应用. 我们总是倾向于去学习和使用所有的语言特性,好像凡是新的,凡是提供了的, 就有必要去使用: 这本书告诉我们, 要有选择性 ...

  8. <mvc:annotation-driven />与<context:annotation-config />

    Spring家族的配置中这两个配置的意义,说具体点其实根据标签的shecma就能看出来,mvc,主要就是为了Spring MVC来用的,提供Controller请求转发,json自动转换等功能,而co ...

  9. C#: 获取执行程序所在路径和启动资源管理器

    一. 获取执行程序所在路径 1.获取和设置当前目录的完全限定路径. string str = System.Environment.CurrentDirectory;  //获取的是主程序目录,线程启 ...

  10. [算法][包围盒]AABB简单类

    头文件: #pragma once #include <iostream> //一个假的点类型 struct Vector3 { float x; float y; float z; }; ...