Board Game

Accept: 54    Submit: 151
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

解题:转自http://blog.csdn.net/henryascend/article/details/38663589

建图什么的太不懂了

最小费用流
每次操作只更改相邻的两个数,可看作棋盘模型,黑白两色
对于a[i,j]  ,  对答案的贡献为
a[i,j]^2 - 2*a[i,j]*b[i,j] + b[i,j]^2   
b[i,j]^2 为常项   ,  对于a[i,j] ,由选p-1到p时,  答案增加了 2p - 1 -  2*b
对于所有白色点 ,添加源点至白点的K条边,花费为 2p-1-2*b, 流量为1,
对于所有黑色点 ,添加黑点至汇点的K条边,花费为 2p-1-2*b, 流量为1,
任意相邻的黑白点之间添加花费为0,流量无穷的一条边,
在图上跑最小费用流,当d[T]>=0时,对答案已无影响,即可退出
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
struct Edge {
int from, to , cap, flow, cost , next;
};
const int inf = 0x3f3f3f3f;
int n,m,K;
Edge edge[];
int head[];
int dx[]= {,,-,};
int dy[]= {-,,,};
int map[][];
int d[],a[],p[];
bool vis[];
int ans,cnt;
void add(int from, int to , int cap, int cost) {
edge[cnt].from = from;
edge[cnt].to = to;
edge[cnt].cap =cap;
edge[cnt].flow = ;
edge[cnt].cost = cost;
edge[cnt].next = head[from];
head[from] = cnt ++; edge[cnt].from = to;
edge[cnt].to = from;
edge[cnt].cap = ;
edge[cnt].flow = ;
edge[cnt].cost = -cost;
edge[cnt].next = head[to];
head[to] = cnt ++;
} bool bound(int x,int y) {
return ( x>= && x<=n && y>= && y<=m );
} bool spfa(int S, int T, int &flow, int &cost) {
memset(d,,sizeof(d));
memset(vis, , sizeof(vis));
queue <int> q;
d[S] = ;
a[S] = inf;
vis[S] = ;
p[S] = ;
q.push(S);
while (!q.empty()) {
int u = q.front();
q.pop();
int i=head[u];
while (i!=-) {
int v= edge[i].to;
if (edge[i].cap>edge[i].flow && d[v]>d[u]+edge[i].cost) {
d[v] = d[u] +edge[i].cost;
p[v] = i;
a[v] = min( a[u], edge[i].cap - edge[i].flow);
if (!vis[v]) {
q.push(v);
vis[v] =;
}
}
i = edge[i].next;
}
vis[u]=;
}
if (d[T]>=) return false;
flow += a[T];
cost += d[T] * a[T];
int u= T;
while ( u!=S ) {
edge[ p[u] ] .flow +=a[T];
edge[ p[u]^ ].flow -=a[T];
u=edge[ p[u] ].from;
}
return true;
} void Mincost (int S, int T) {
int flow = , cost = ;
while ( spfa(S, T, flow, cost) );
ans += cost;
}
int main() {
int T,cas=;
scanf("%d",&T);
while (T--) {
memset(head,-,sizeof(head));
cnt =ans = ;
scanf("%d%d%d",&n,&m,&K);
int idx=, x;
int st= , en = n*m+;
for (int i=; i<=n; i++)
for (int j=; j<=m; j++) {
scanf("%d",&x);
ans += x * x;
map[i][j]= ++idx;
for (int k=; k<=K; k++)
if ( i % == j % )
add( st, map[i][j], , *k - - *x );
else
add( map[i][j],en , , *k - - *x );
}
for (int i=; i<=n; i++)
for (int j=; j<=m; j++) {
if ( i % == j % )
for (int k=; k<; k++) {
int tx= i + dx[k];
int ty= j + dy[k];
if (!bound( tx,ty)) continue;
add(map[i][j], map[tx][ty],inf, );
}
}
Mincost(st, en);
printf("Case %d: %d\n",++cas,ans);
}
return ;
}

fzu2143 Board Game的更多相关文章

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

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

  2. UP Board 串口使用心得

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

  3. UP Board 网络设置一本通

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

  4. UP Board USB无线网卡一贴通

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

  5. 在UP Board 上搭建M——L服务器

    前言 原创文章,转载引用务必注明链接,水平有限,欢迎指正. 本文环境:ubilinux 3.0 on UP Board 初识免流 所谓免流,就是免除手机访问网络产生的流量费用.其原理在乌云网上有过报道 ...

  6. UP Board 妄图启动ubilinux失败

    前言 原创文章,转载引用务必注明链接. 经历了上次的上电开机失败,我们终于发现需要手动为UP板安装系统,因为没有显示器的Headless模式时,使用Linux比较方便,另外熟悉Debian系的,所以选 ...

  7. UP Board 人若有大胆,板子就很惨:首次上电开机失败

    前言 原创文章,转载引用务必注明链接. 注意:拍照自带抖动功能,画质大家凑合着看.冬日天气干燥,手触摸板子前建议流水洗手或者握持大体积导电体将静电放走. 本文使用Markdown写成,为获得更好的阅读 ...

  8. UP Board 超详细开箱评测

    前言 原创文章,转载引用务必注明链接. 江浙沪就是好,昨天发货今天收到.另外爱板太省了,外包装小纸箱还是6s钢化膜的重复利用. 注意:拍照自带抖动功能,画质大家凑合着看.冬日天气干燥,手触摸板子前建议 ...

  9. UP board 漫谈(1)——从Atom到UP Board

    title: UP board 漫谈(1)--从Atom到UP Board date: 2016-12-26 12:33:03 tags: UP board categories: 开发板 perma ...

随机推荐

  1. 短链接及关键字过滤ac自动机设计思路

    =============:短链接设计思路:核心:将长字符转为短字符串并建立映射关系,存储redis中.1.使用crc32转换为Long 2.hashids将long encode为最短字符串.作为短 ...

  2. Extjs grid禁用头部点击三角下拉菜单

    表格头部的三角在点击的时候禁止出现下拉菜单,给每一列添加属性menuDisabled:true xtype:'grid', enableColumnResize:false, columns:[ {t ...

  3. javascript检测基本类型值或引用类型值的类型方法

    首先javascript的数据类型分为两种数据类型:基本数据数据类型和引用数据类型 基本数据类型:Number,String,Boolean,Undefined,Null.原始值,是简单的数据段,可按 ...

  4. 数组去重----es6&es5&数组对象去重

    es6方法: 普通数组: 1.使用Array.from(new Set(arr)); /* * @param oldArr 带有重复项的旧数组 * @param newArr 去除重复项之后的新数组 ...

  5. Python之列表生成式、生成器

    列表生成式 ——可以快速生成list,可以通过一个list推导出另一个list,而代码却十分简洁: >>> [x * x for x in range(1, 11)] [1, 4, ...

  6. BFS POJ 3126 Prime Path

    题目传送门 /* 题意:从一个数到另外一个数,每次改变一个数字,且每次是素数 BFS:先预处理1000到9999的素数,简单BFS一下.我没输出Impossible都AC,数据有点弱 */ /**** ...

  7. 13 继续C#中的方法,带返回值的方法介绍

    在这一个练习中,我们要使用带返回值的方法.如果一个方法带返回值,那么它的形式是这样的. 定义一个带返回值的C#方法 static 返回类型 方法名字 (参数类型 参数1的名字,参数类型 参数2的名字) ...

  8. 判断IE浏览器的类型以及提示信息(低版本浏览器不予显示)

    //浏览器IE版本判断(function(window) {    var theUA = window.navigator.userAgent.toLowerCase();    if ((theU ...

  9. 解决opencv在pycharm中无代码自动提示的bug

    2018-03-0422:19:39 首先,估计这不是bug 可能是我自己误操作导致的,但是让我搞了好久才搞定,实在是苦恼 如图已实现功能,百度里有很多朋友出现了,这个无代码提示的问题 大概是这样的, ...

  10. 【转载】自制4412底板自动进入SD卡更新模块

    转载自迅为论坛:http://www.topeetboard.com参考平台:迅为iTOP-4412开发板 问题如下:在自制的底板上,当SD卡插在板子上开机时,会自动进入Updating模式,如果SD ...