BZOJ 2668 [cqoi2012]交换棋子 | 最小费用最大流
传送门
题解
同时分别限制流入和流出次数,所以把一个点拆成三个:入点in(x)、中间点mi(x)、出点ou(x)。
如果一个格子x在初始状态是黑点,则连(S, mi(x), 1, 0)
如果x在目标状态是黑点,则连(mi(x), T, 1, 0)
设x的交换次数限制是w
如果x在两种状态中颜色相同,则连(in(x), mi(x), w / 2, 0), (mi(x), ou(x), w / 2, 0)
如果x只在初始状态为黑色,则连(in(x), mi(x), w / 2, 0), (mi(x), ou(x), (w + 1) / 2, 0)
如果x只在目标状态为黑色,则连(in(x), mi(x), (w + 1) / 2, 0), (mi(x), ou(x), w / 2, 0)
每个点x和相邻的点y(八连通),连(ou(x), in(y), INF, 1)
然后最小费用最大流即可。
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#define space putchar(' ')
#define enter putchar('\n')
typedef long long ll;
using namespace std;
template <class T>
void read(T &x){
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
const int N = 1234, M = 1000005, INF = 0x3f3f3f3f;
const int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
int n, m, tot, maxflow, mincost, src = 1, des = 2, id[23][23][3], b1, b2;
int ecnt = 1, adj[N], pre[N], dis[N], nxt[M], go[M], cap[M], cost[M];
char st[23][23], ed[23][23], cp[23][23];
void _add(int u, int v, int w, int c){
go[++ecnt] = v;
nxt[ecnt] = adj[u];
adj[u] = ecnt;
cap[ecnt] = w;
cost[ecnt] = c;
}
void add(int u, int v, int w, int c){
_add(u, v, w, c);
_add(v, u, 0, -c);
}
bool spfa(){
queue <int> que;
static bool inq[N] = {0};
for(int i = 1; i <= tot; i++)
dis[i] = INF, pre[i] = 0;
dis[src] = 0, que.push(src), inq[src] = 1;
while(!que.empty()){
int u = que.front();
que.pop(), inq[u] = 0;
for(int e = adj[u], v; e; e = nxt[e]){
if(cap[e] && dis[u] + cost[e] < dis[v = go[e]]){
dis[v] = dis[u] + cost[e], pre[v] = e;
if(!inq[v]) que.push(v), inq[v] = 1;
}
}
}
return dis[des] < INF;
}
void mcmf(){
while(spfa()){
int delta = INF;
for(int e = pre[des]; e; e = pre[go[e ^ 1]])
delta = min(delta, cap[e]);
for(int e = pre[des]; e; e = pre[go[e ^ 1]])
cap[e] -= delta, cap[e ^ 1] += delta;
maxflow += delta;
mincost += delta * dis[des];
}
}
bool legal(int x, int y){
return x > 0 && y > 0 && x <= n && y <= m;
}
int main(){
read(n), read(m), tot = 3 * n * m + 2;
for(int i = 1, cnt = 2; i <= n; i++)
for(int j = 1; j <= m; j++)
id[i][j][0] = ++cnt, id[i][j][1] = ++cnt, id[i][j][2] = ++cnt;
for(int i = 1; i <= n; i++) scanf("%s", st[i] + 1);
for(int i = 1; i <= n; i++) scanf("%s", ed[i] + 1);
for(int i = 1; i <= n; i++) scanf("%s", cp[i] + 1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++){
int w = cp[i][j] - '0';
if(st[i][j] == '1') b1++, add(src, id[i][j][0], 1, 0);
if(ed[i][j] == '1') b2++, add(id[i][j][0], des, 1, 0);
if(st[i][j] == ed[i][j]){
add(id[i][j][1], id[i][j][0], w / 2, 0);
add(id[i][j][0], id[i][j][2], w / 2, 0);
}
else if(st[i][j] == '1'){
add(id[i][j][1], id[i][j][0], w / 2, 0);
add(id[i][j][0], id[i][j][2], (w + 1) / 2, 0);
}
else if(ed[i][j] == '1'){
add(id[i][j][1], id[i][j][0], (w + 1) / 2, 0);
add(id[i][j][0], id[i][j][2], w / 2, 0);
}
for(int d = 0, x, y; d < 8; d++)
if(legal(x = i + dx[d], y = j + dy[d]))
add(id[i][j][2], id[x][y][1], INF, 1);
}
mcmf();
if(maxflow < max(b1, b2)) puts("-1");
else write(mincost), enter;
return 0;
}
BZOJ 2668 [cqoi2012]交换棋子 | 最小费用最大流的更多相关文章
- BZOJ 2668: [cqoi2012]交换棋子
2668: [cqoi2012]交换棋子 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1112 Solved: 409[Submit][Status ...
- 【BZOJ-2668】交换棋子 最小费用最大流
2668: [cqoi2012]交换棋子 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1055 Solved: 388[Submit][Status ...
- BZOJ.2668.[CQOI2012]交换棋子(费用流zkw)
题目链接 首先黑白棋子的交换等价于黑棋子在白格子图上移动,都到达指定位置. 在这假设我们知道这题用网络流做. 那么黑棋到指定位置就是一条路径,考虑怎么用流模拟出这条路径. 我们发现除了路径的起点和终点 ...
- BZOJ 1061 志愿者招募(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1061 题意:申奥成功后,布布经过不懈努力,终于 成为奥组委下属公司人力资源部门的主管.布 ...
- bzoj 1070 [SCOI2007]修车(最小费用最大流)
1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3515 Solved: 1411[Submit][Status] ...
- BZOJ 1221: [HNOI2001] 软件开发(最小费用最大流)
不知道为什么这么慢.... 费用流,拆点.... --------------------------------------------------------------------------- ...
- bzoj 3171: [Tjoi2013]循环格 最小费用最大流
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3171 题解: 首先我们很容易发现一个结论: 出现完美循环当且仅当所有点的出入度均为1 所 ...
- bzoj 1070: [SCOI2007]修车【最小费用最大流】
一开始从客人角度想的,怎么建都不对 从一个修车工所接待的所有顾客花费的总时间来看,设一共有x个人,那么第一个修的对总时间的贡献是x*w1,第二个是(x-1)*w2-以此类推.所以把第i个修车工拆成n组 ...
- BZOJ 1449 球队收益(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1449 题意: 思路:首先,我们假设后面的M场比赛两方都是输的,即初始时的lose[i]再 ...
随机推荐
- 实验八 Web基础
实验八 Web基础 1.安装apache sudo apt-get install apache2 2.启动apache service apache2 start 3.使用 netstat -tup ...
- Vue 使用细节收集
JSX 中 on 开头的属性名 在用 elementui 中的 el-upload 的时候,他们组件中有一个属性 on-change ,也不知道谁想出来的属性名,太扯淡了,非要 on 开头,我开始的代 ...
- [python]记录Windows下安装matplot的经历
最近学习在看<机器学习实战>一书,第二章的时候要用到Natplotlib画图,于是便开始安装Matplotlib.本文所用到的所有安装包都可以在文末的链接中找到. 首先从Matplotli ...
- 如何完全卸载VS2010(亲自体验过) (转)
1.首先用360卸载,当卸载完成后,提示有残余的话,就强力清除 2,接着,下载IobitUninstaller工具 3.按照下面进行卸载 1.Microsoft .NET Framework 4 框架 ...
- js的各种正则表达式
验证各种手机包括成都"028-"开头的座机号验证 if (!(/^(16[8]|13[0-9]|15[0|3|6|7|8|9]|18[7])\d{8}|(028-)\d{7}$/. ...
- linux中使sqlplus能够上下翻页
安装包链接:https://pan.baidu.com/s/1WsQTeEQClM88aEqIvNi2ag 提取码:s241 rlwrap-0.37-1.el6.x86_64.rpm 和 rlwra ...
- docker 学习笔记(1)--常用命令
docker pull---- 获取image(镜像)docker build---- 创建image(镜像)docker images ----查询所有的image(镜像)docker run--- ...
- SpringMVC 之 @ResponseBody 和 @RequestBody
前后端进行数据交互的时候,规定数据交互的格式,使数据交互规范而统一,是极为重要的事.一般而言,我们会采用 JSON 进行数据交互.本文暂不讨论如何 JSON 的格式规范,而是解析一下如何在 Sprin ...
- mysql学习(3)10045错误,连接不上数据库
mysql8.0默认加密的方式是caching_sha2_password认证方式,当使用navicat 或者程序连接是连接不上, 好吧,那我们修改配置并重启服务可以解决此问题 找到mysql的配置文 ...
- PAT甲题题解-1117. Eddington Number(25)-(大么个大水题~)
如题,大水题...贴个代码完事,就这么任性~~ #include <iostream> #include <cstdio> #include <algorithm> ...