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. log4j日志基本配置

    Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使用这三个组件可以轻松 ...

  2. TestNG设置用例循环执行

    曾经做过一需求,需要单个集成测试用例循环执行N次,或许你会说for循环就可以了,这当然是可以的.那有没有逼格更高点的方法,当然也是有的.下面我们就说下使用TestNG注解功能实现用例的循环执行. 1. ...

  3. jQuery setInterval倒计时精确到毫秒

    效果类似于:购物抢购倒计时-->在跳转N多个页面之后,倒计时间仍然正常显示. 思路: 结束时间是固定不变的(endTime),一直在改变的是当下的时间(curTime = new date()) ...

  4. Light 1289 - LCM from 1 to n (位图标记+素数筛选)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1289 题目描述: 给出一个n,求出lcm(1,2,3......n)为多少? ...

  5. 一个包含所有C++头文件的头函数

    #include<bits/stdc++.h> using namespace std; 使用方法和平常的头文件一样,#include<bits/stdc++.h>包含以下头文 ...

  6. STL内存分配方式

    关于STL用的很多比如map, vector, set, queue, stack等等.很少关注它的内存分配情况,但是经常遇到比如使用map,不停的在map中插入了一些很小的对象,然后释放了一些,然后 ...

  7. Android 简单的语音播报

    不解释快上车 Main.class package com.example.myapp; import android.app.AlertDialog;import android.os.Bundle ...

  8. pycharm永久激活(转载)

    转载自CSDN--http://blog.csdn.net/mr_hhh/article/details/79062747 2018-02-2417:30:52 今天再补充一个教程,关于pycharm ...

  9. bower——基本使用

    基本概念 bower可以解决项目的静态文件依赖的问题 bower是用nodejs开发的,所以要现状nodejs 安装nodejs应用程序,网上自行下载 检验是否成功安装,打开电脑cmd,执行node ...

  10. Ubuntu-Python2.7安装 scipy,numpy,matplotlib

    sudo apt-get install python-scipy sudo apt-get install python-numpy sudo apt-get install python-matp ...