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. poj 2154 Color【polya定理+欧拉函数】

    根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...

  2. HTML和JSP的不同及优缺点

    HTML(Hypertext Markup Language)文本标记语言,它是静态页面,和JavaScript一样解释性语言,为什么说是解释性语言呢?因为,只要你有一个浏览器那么它就可以正常显示出来 ...

  3. Odoo免费开源企业信息化平台助力企业成功

    企业信息化变革之路 信息孤岛的真实由来 打开百度App,看更多图片 左边为当下企业现状,右边为Odoo的整体 企业信息孤岛的严重性,来自于企业的自身高速发展,企业以销售为生命主题围绕着客户会搭建一系列 ...

  4. HDU4947GCD Array(莫比乌斯反演+树状数组)

    题面 传送门 题解 orz ljz 相当于每一个数要加上 \[v\times [\gcd(i,n)=d]=v\times [\gcd(i/d,n/d)=1]=v\times \sum_{p|{i\ov ...

  5. Spring-Aop的两种代理方式

    Spring-Aop两种代理方式: 1.JDK动态代理:用于目标类实现了接口: 2.Cglib动态代理:用于目标类没有实现接口: spring会依据目标类是否实现接口来选择使用哪种代理方式(目标类:相 ...

  6. python manage.py syncdb报错:No module named MySQLdb

    今天同步数据时出现这个错误: 解决方法: 1.先下载mysql-python 支持1.2.3-2.7版本 MySQL-python 1.2.3 for Windows and Python 2.7, ...

  7. 呼啦圈(keyframes和transform结合)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. POJ_2255_Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12342   Accepted: 7712 De ...

  9. Jmeter之JDBC请求参数化(一)

    一.环境准备 a.jmeter5.1.1版本最新版本,可以去网页下载:https://jmeter.apache.org/download_jmeter.cgi b.jdbc驱动:链接:https:/ ...

  10. 并发编程学习笔记(13)----ConcurrentLinkedQueue(非阻塞队列)和BlockingQueue(阻塞队列)原理

    · 在并发编程中,我们有时候会需要使用到线程安全的队列,而在Java中如果我们需要实现队列可以有两种方式,一种是阻塞式队列.另一种是非阻塞式的队列,阻塞式队列采用锁来实现,而非阻塞式队列则是采用cas ...