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]再 ...
随机推荐
- ASP.NET Core MVC中的IActionFilter.OnActionExecuting方法,可以获取Controller的Action方法参数值
用过ASP.NET Core MVC中IActionFilter拦截器的开发人员,都知道这是一个非常强大的MVC拦截器.最近才发现IActionFilter的OnActionExecuting方法,甚 ...
- Android Studio com.android.support:percent 导入错误 - 转
看第一行代码(第二版的)书,讲了一个关于PercentFrameLayout和PercentRelativeLayout的部分,书上在build.gradle中导入了com.android.suppo ...
- WPF XML序列化保存数据 支持Datagrid 显示/编辑/添加/删除数据
XML序列化保存数据 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- 2017-2018-2 20155230《网络对抗技术》实验8:Web基础
实践过程记录 1.Web前端HTML 首先用指令sudo apt-get install apache2下载apache,由于实验机已经安装好Apache,这里就不演示了,对于Apache使用的端口我 ...
- WPF应用
代码 private void button1_Click(object sender, RoutedEventArgs e) { calculate sa = new calculate(int.P ...
- Source insight 中 标题栏路径显示完整路径的方法
在source insight 的标题栏中显示完整路径名的方法.Options -> Preferences -> Display -> Trim long path names w ...
- 小内存VPS apache并发控制参数prefork调优
小内存VPS优化(使用wdcp lnamp一键包安装环境的情况下): 1.主要优化perfork模式下几个参数,防止开启过多的httpd进程占用大量内存导致内存满:在wdcp下修改的httpd配置文件 ...
- 微信小程序云开发之云函数创建
云函数 云函数是一段运行在云端的代码,无需管理服务器,在开发工具内编写.一键上传部署即可运行后端代码. 小程序内提供了专门用于云函数调用的 API.开发者可以在云函数内使用 wx-server-sdk ...
- [CF1019C]Sergey's problem[构造]
题意 找出一个集合 \(Q\),使得其中的点两两之间没有连边,且集合中的点可以走不超过两步到达其他所有不在集合中的点.输出任意一组解. \(n\leq 10^6\) 分析 考虑构造,先从 \(1\) ...
- PowerBI开发 第十二篇:钻取
钻取是指沿着层次结构(维度的层次)查看数据,钻取可以变换分析数据的粒度.钻取分为下钻(Drill-down)和上钻(Drill-up),上钻是沿着数据的维度结构向上聚合数据,在更大的粒度上查看数据的统 ...