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写成,为获得更好的阅读体验和正常的图片.链接,请访问我的 ...
随机推荐
- 夺命雷公狗---DEDECMS----2快速入门之玩转dede四大表之间的关系
比如一个小说网站,网站里面有很多类型让我们的小说网他里面有很多种分类,如: 玄幻....奇幻....仙侠....武侠....文学....异界....都市....军事....历史....灵异....悬疑 ...
- switch结构2016/03/08
Switch 03/08 一.结构 switch(){ case *: ;break;……default: ;brek;} 练习:输入一个日期,判断这一年第几天? Console.Write(&q ...
- Android相对布局实例
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- ILOG的一个基本应用——解决运输问题、转运问题
一.Ilog软件 该软件用来解决优化问题,大部分是线性问题,深一点的其他内容还不清楚.只知道一些基础的应用,网上相关内容很少.接下来就解决一个简单的运输问题 二.运输问题 数学模型 ILOG OPL程 ...
- nginx 反向代理 和lvs负载均衡
nginx反向代理:用户请求nginx代理服务器然后代理服务器将用户请求转为服务器再由nginx代理服务器将服务器的响应反应给用户. lvs负载均衡:用户请求nginx代理服务器然后代理服务器将用户请 ...
- django templates学习使用记录
可以在基本模板中多插入几个black来适应不同的布局
- selenium+phantomJS学习使用记录
背景知识: phantomjs是一个基于webkit的没有界面的浏览器,所以运行起来比完整的浏览器要高效. selenium是一个测试web应用的工具,目前是2.42.1版本,和1版的区别在于2.0+ ...
- java几道简单的面试题目
1. 请问以下程序会输出什么? public class Test { public static void main(String[] args) { Par ...
- Wall Street English
1月23号,报名Wall Street English!
- TEXT、TINYTEXT、MEDIUMTEXT、LONGTEXT选择 和 char varchar varchar2 的区别
TEXT.TINYTEXT.MEDIUMTEXT.LONGTEXT选择: 储存不区分大小写的字符数据 TINYTEXT 最大长度是 255 (2^8 - 1) 个字符. TEXT 最大长度是 6553 ...