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 ...
随机推荐
- vue中子组件向父组件传值
1.子组件$emit()触发,父组件$on()监听 子组件:<template> <div class="hello"> <button v-on:c ...
- bzoj4987: Tree(树形dp)
Description 从前有棵树. 找出K个点A1,A2,…,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. Input 第一行两个正整数n,k,表示数的顶点数和 ...
- 【原创】Eclipse实现图形化界面插件-vs4e
vs4e插件下载地址:http://visualswing4eclipse.googlecode.com/files/vs4e_0.9.12.I20090527-2200.zip 下载完成后,解压,然 ...
- JavaScript--DOM删除节点removeChild()
删除节点removeChild() removeChild() 方法从子节点列表中删除某个节点.如删除成功,此方法可返回被删除的节点,如失败,则返回 NULL. 语法: nodeObject.remo ...
- 239 Sliding Window Maximum 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位.例如,给定 nums = [1,3,-1,-3, ...
- 一个JavaScript贷款计算器
通过本案例,将会学到: . 如何在文档中查找元素 . 如何通过表单input元素来获取用户的输入数据 . 如何通过文档元素来设置HTML内容 . 如何将数据存储在浏览器中 . 如何使用脚本发起HTTP ...
- duilib入门问题集
问:如何把资源放入zip?答: 先SetResourcePath设置资源目录,再SetResourceZip设置压缩资源文件名 问:如何设置窗体的初始化大小?答:设置XML文件的Window标签的si ...
- WordPress强制跳转https教程
在互联网火热的今天,安全问题显得越来越重要,为了用户信息安全,很多热门网站都启用了https 有小伙伴就问:我启用了https,为什么访问的时候显示的还是http呢? 其实,有时候并不是因为我们ssl ...
- 微信小程序资料总结
http://blog.csdn.net/ZCLengendary/article/details/54312030 --添加html标签与处理 https://www.cnblogs.com/HD ...
- Appium环境部署
Appium 是一个开源.跨平台的自动化测试工具,用于测试原生和轻量移动应用,支持 iOS, Android平台. 需要部署的软件:python环境.nodejs..net framework4.5. ...