fzu2143 Board Game
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
建图什么的太不懂了
#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的更多相关文章
- [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写成,为获得更好的阅读体验和正常的图片.链接,请访问我的 ...
- UP Board USB无线网卡一贴通
前言 原创文章,转载引用务必注明链接,水平有限,欢迎指正. 本文环境:ubilinux 3.0 kernel 4.4.0 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我 ...
- 在UP Board 上搭建M——L服务器
前言 原创文章,转载引用务必注明链接,水平有限,欢迎指正. 本文环境:ubilinux 3.0 on UP Board 初识免流 所谓免流,就是免除手机访问网络产生的流量费用.其原理在乌云网上有过报道 ...
- UP Board 妄图启动ubilinux失败
前言 原创文章,转载引用务必注明链接. 经历了上次的上电开机失败,我们终于发现需要手动为UP板安装系统,因为没有显示器的Headless模式时,使用Linux比较方便,另外熟悉Debian系的,所以选 ...
- UP Board 人若有大胆,板子就很惨:首次上电开机失败
前言 原创文章,转载引用务必注明链接. 注意:拍照自带抖动功能,画质大家凑合着看.冬日天气干燥,手触摸板子前建议流水洗手或者握持大体积导电体将静电放走. 本文使用Markdown写成,为获得更好的阅读 ...
- UP Board 超详细开箱评测
前言 原创文章,转载引用务必注明链接. 江浙沪就是好,昨天发货今天收到.另外爱板太省了,外包装小纸箱还是6s钢化膜的重复利用. 注意:拍照自带抖动功能,画质大家凑合着看.冬日天气干燥,手触摸板子前建议 ...
- 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 ...
随机推荐
- SP2916 GSS5 - Can you answer these queries V
给定一个序列.查询左端点在$[x_1, y_1]$之间,且右端点在$[x_2, y_2]$之间的最大子段和,数据保证$x_1\leq x_2,y_1\leq y_2$,但是不保证端点所在的区间不重合 ...
- win10家庭版添加本地账户方法
1.正常情况下,windows的使用者都习惯于用本地用户及用户组工具来创建新用户,如下图所示,在win10的开始窗口右侧的空白处,输入lusrmgr.msc 打开本地用户管理控制台. 2.在win10 ...
- SimpleDataFormat详解
[转]SimpleDateFormat使用详解 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方 ...
- sublime 3 最新注册码
http://9iphp.com/web/html/sublime-text-3-license-key.html
- SCRIPT70: 没有权限
主要原因:iframe安全而引发的问题,浏览器中js是没有垮域访问的权限的.如果用到iframe首先确保不垮域,或者不用iframe以绕开这个问题. 另外在jquery的早期版本中如:jquery-1 ...
- JavaScript相关技术学习
百度前端技术学院 http://ife.baidu.com/task/all 框架 JQuery 简单Web论坛 https://github.com/huanshen/web-bbs http:/ ...
- [ POI 2010 ] Antisymmetry
\(\\\) \(Description\) 给出一个长度为 \(N\) 的二进制串,定义一个子串是优秀的,当且仅当其正着看,和倒着按位取反后看结果是一样的,求整个串有多少个优秀的子串. \(N\le ...
- [ CodeForces 1065 B ] Vasya and Isolated Vertices
\(\\\) \(Description\) 求一个\(N\)个点\(M\)条边的无向图,点度为 \(0\) 的点最多和最少的数量. \(N\le 10^5,M\le \frac {N\times ( ...
- 61配置nanopim1plus的HDMI为1080p输出
61配置nanopim1plus的HDMI为1080p输出 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2018/4/4 10:21 版本:V1.1 开 ...
- android cmd adb命令安装和删除apk应用
copy自http://blog.csdn.net/xpsharp/article/details/7289910 1. 安装Android应用程序 1) 启动Android模拟器 2) adb in ...