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写成,为获得更好的阅读体验和正常的图片.链接,请访问我的 ...
随机推荐
- PTPX的average power analysis
在average power analysis中,switching activity被分解为toggle rate和static probabilities两部分. annotation的sourc ...
- ASP.NET MVC(三)
ASP.NET Routing 模块的责任是将传入的浏览器请求映射为特有的MVC controller actions. 请求 URL 当我们不使用路由的时候 请求 http://server/app ...
- vim下正则表达式的非贪婪匹配
贪婪模式是: .* 非贪婪模式是: .\{-}
- 常用的DC插头公头的尺寸
2.0*0.6mm:这种应该是用在诺基亚黑白屏那种手机上的充电插头 2.5*0.7mm:这种不知用在哪里 3.5*1.35mm:应该是以前那种小型的磁带机放音机上用的 4.0*1.7mm:已知 ora ...
- RMB转换人民币大小金额
MXS&Vincene ─╄OvЁ &0000015 ─╄OvЁ MXS&Vincene MXS&Vincene ─╄OvЁ:今天很残酷,明天更残酷,后天很美好 ...
- Oracle DB SQL 性能分析器
• 确定使用SQL 性能分析器的优点 • 描述SQL 性能分析器工作流阶段 • 使用SQL 性能分析器确定数据库更改所带来的性能改进 SQL 性能分析器:概览 • 11g 的新增功能 • 目标用户:D ...
- Tomcat端口被占用快速解决方案
在dos下,输入 netstat -ano|findstr 8080 //说明:查看占用8080端口的进程 显示占用端口的进程 taskkill /pid 6856 /f //说明,运行 ...
- IOS5基础教程之一-----如何创建XCode项目
一.IOS的基础知识 1.只有一个应用程序正在运行.在IOS上,每一段时间内只能激活一个应用程序并在屏幕上显示. 2.只有一个窗口.只允许应用程序操作的一个窗口. 3.访问受限.只能在IOS为应用程序 ...
- iOS:UITableView 方法 属性
参考:https://developer.apple.com/library/iOS/documentation/UIKit/Reference/UITableView_Class/Reference ...
- php函数:PHP pathinfo() 函数
pathinfo() 函数以数组的形式返回文件路径的信息. 语法 pathinfo(path,options) 参数 描述 path 必需.规定要检查的路径. process_sections 可选. ...