Asa's Chess Problem
一、题目
给定一张 \(n\times n\) 的矩阵,每个点上面有黑棋或者是白棋,给定 \(\frac{n\times n}{2}\) 对可以交换的位置,每对位置一定在同一行 \(/\) 同一列。\(R[i],C[j]\) 分别表示 \(i\) 行 \(j\) 列的黑棋数,要求交换后 \(Rl[i]\leq R[i]\leq Rr[i],Cl[i]\leq C[i]\leq Cr[i]\)
问最小交换次数,如果无解输出 \(-1\)
二、解法
还是比较有意思的,因为这道题的限制在行列上,所以我们把行列拿出来建图。
最好把交换的过程在网络上表示出来,流量代表黑棋,一开始的初始状态有的黑棋就先 把流量给对应的行和列 。然后考虑表示这个限制,连一条上下界为限制的边到汇点 ,问题转化成带上下界的网络流。
最后来表示交换过程,同色的棋子可以忽略。一定要注意每对位置一定是同行或者同列,如果同行,那么把有黑棋的列连向无黑棋的列,如果同列,那么把有黑棋的行连向无黑棋的行。题目的这个条件保证了行和列是相对独立的,所以在我们的建图中并没有行和列的边(这也是一个思维定式,要敢于跳出来)
最后总结一下我们的建图,拿着这个图去跑最小费用可行流就可以了(无特殊说明费用为 \(0\)):
- \(s\) 连行,上下界都是初始黑棋数,行连 \(t\) ,上下界是题目中的要求。
- \(s\) 连列,上下界都是初始黑棋数,列连 \(t\) ,上下界是题目中的要求。
- \((x_1,y_1),(x_2,y_2)\) ,设 \((x_1,y_1)\) 初始有黑棋且不同色,如果 \(x_1=x_2\) ,\(y_1\) 连 \(y_2\) 一条容量为 \(1\) ,费用为 \(1\) 的边,如果 \(y_1=y_2\) ,\(x_1\) 连 \(x_2\) 一条容量为 \(1\) ,费用为 \(1\) 的边。
由于评测机挂了,我的代码只保证能通过样例,但方法是对的,请相信我。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int M = 205;
const int inf = 0x3f3f3f3f;
int read()
{
int x=0,f=1;char c;
while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return x*f;
}
int n,s,t,s1,t1,tot,sum,cost,f[M],in[M],out[M],a[55][55];
int ins[M],dis[M],flow[M],lst[M],pre[M],r[M],c[M];
struct edge
{
int v,f,c,next;
edge(int V=0,int F=0,int C=0,int N=0) :
v(V) , f(F) , c(C) , next(N) {}
}e[M*M];
void add(int u,int v,int c,int fl)
{
e[++tot]=edge(v,fl,c,f[u]),f[u]=tot;
e[++tot]=edge(u,0,-c,f[v]),f[v]=tot;
}
int xez(int u,int v,int a,int b,int c)
{
in[v]+=a;out[u]+=a;
add(u,v,c,b-a);
}
int bfs()
{
queue<int> q;
for(int i=0;i<=t;i++) dis[i]=inf;
flow[s]=inf;dis[s]=0;pre[s]=-1;
q.push(s);
while(!q.empty())
{
int u=q.front();q.pop();
ins[u]=0;
for(int i=f[u];i;i=e[i].next)
{
int v=e[i].v,c=e[i].c;
if(dis[v]>dis[u]+c && e[i].f>0)
{
dis[v]=dis[u]+c;
flow[v]=min(flow[u],e[i].f);
pre[v]=u;lst[v]=i;
if(!ins[v])
{
ins[v]=1;
q.push(v);
}
}
}
}
return dis[t]<inf;
}
void solve()
{
s1=0;t1=2*n+1;s=2*n+2;t=2*n+3;
sum=cost=0;tot=1;
for(int i=0;i<=t;i++) f[i]=in[i]=out[i]=0;
for(int i=1;i<=n;i++) r[i]=c[i]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
a[i][j]=read();
r[i]+=a[i][j];
c[j]+=a[i][j];
}
for(int i=1;i<=n;i++)
{
int a=read(),b=read();
xez(s1,i,r[i],r[i],0);
xez(i,t1,a,b,0);
}
for(int i=1;i<=n;i++)
{
int a=read(),b=read();
xez(s1,i+n,c[i],c[i],0);
xez(i+n,t1,a,b,0);
}
for(int i=1;i<=n*n/2;i++)
{
int u=read(),v=read(),p=read(),q=read();
if(a[u][v]==a[p][q]) continue;
if(a[u][v]==0) swap(u,p),swap(v,q);
if(u==p) add(v+n,q+n,1,1);//同行
else add(u,p,1,1);//同列
}
for(int i=0;i<=t1;i++)
{
if(in[i]>out[i]) add(s,i,0,in[i]-out[i]),sum+=in[i]-out[i];
else add(i,t,0,out[i]-in[i]);
}
add(t1,s1,0,inf);
while(bfs())
{
sum-=flow[t];
cost+=flow[t]*dis[t];
int zy=t;
while(zy!=s)
{
e[lst[zy]].f-=flow[t];
e[lst[zy]^1].f+=flow[t];
zy=pre[zy];
}
}
if(sum>0) puts("-1");
else printf("%d\n",cost);
}
int main()
{
while(~scanf("%d",&n)) solve();
}
Asa's Chess Problem的更多相关文章
- 【hihocoder 1424】 Asa's Chess Problem(有源汇上下界网络流)
UVALive-7670 ICPC北京2016-C题 hihocoder 1424 题意 有个 \(N\times N\) 的棋盘,告诉你每个格子黑色(1)或白色(0),以及每对能相互交换的同行或同列 ...
- [HihoCoder-1424] Asa's Chess Problem
有上下界的费用流 #include <stdio.h> #include <algorithm> #include <queue> #include <cst ...
- UVa 750 - 8 Queens Chess Problem
题目大意:八皇后问题,在一个8*8的棋盘上,放置8个皇后,使得任意两个皇后不在同一行上.不在同一列上.不在同一条对角线上,不过这道题预先给定了一个位置放置一个皇后,让你输出所有可能的答案. 经典的回溯 ...
- [2019HDU多校第二场][HDU 6591][A. Another Chess Problem]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6591 题目大意:二维坐标系上,所有满足\(5|2x+y\)的点都被设为障碍物,无法通过.现给出一对点, ...
- The 2016 ACMICPC Asia Beijing Regional Contest
A. Harmonic Matrix Counter (3/19) B. Binary Tree (1/14) C. Asa's Chess Problem (21/65) [ Problem ] 给 ...
- HDU 5742 Chess SG函数博弈
Chess Problem Description Alice and Bob are playing a special chess game on an n × 20 chessboard. ...
- 2016暑假多校联合---A Simple Chess
2016暑假多校联合---A Simple Chess Problem Description There is a n×m board, a chess want to go to the po ...
- dp - Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess
Gerald and Giant Chess Problem's Link: http://codeforces.com/contest/559/problem/C Mean: 一个n*m的网格,让你 ...
- HDU 4405:Aeroplane chess(概率DP入门)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description Hzz loves ...
随机推荐
- codeforces 1077D Cutting Out 【二分】
题目:戳这里 题意:给n个数的数组,要求找k个数满足,这k个数在数组中出现的次数最多. 解题思路:k个数每个数出现次数都要最大化,可以想到二分下限,主要是正确的二分不好写. 附ac代码: 1 #inc ...
- 记一次 Billu_b0x渗透
目录: 0x01 寻找ip 1.这边我们是使用的nmap来寻找我们的靶机IP地址,开始Ip是1,结束是254,153是我kali的ip,所以158就是我们的靶机的ip地址了. 2. 查看端口服务 这边 ...
- CS144学习(2)TCP协议实现
Lab1-4 分别是完成一个流重组器,TCP接收端,TCP发送端,TCP连接四个部分,将四个部分组合在一起就是一个完整的TCP端了.之后经过包装就可以进行TCP的接收和发送了. 代码全部在github ...
- IDEA插件:快速删除Java代码中的注释
背景 有时,我们需要删除Java源代码中的注释.目前有不少方法,比如: 实现状态机.该方式较为通用,适用于多种语言(取决于状态机支持的注释符号). 正则匹配.该方式容易误判,尤其是容易误删字符串. ...
- VSCode 在 Vue 导入路径中使用 @ 符号后无法正确跳转 bug
VSCode 在 Vue 导入路径中使用 @ 符号后无法正确跳转 bug bug jsconfig.json { // This file is required for VSCode to unde ...
- when I was installing github for windows ,some errors occurred !
1: 2: 3: 4: install.log error messages:
- 1GB === 1000MB & 1GB === 1024MB
1GB === 1000MB & 1GB === 1024MB 字节单位换算 1 Gigabyte = 1000 Megabytes 1 Gibibyte = 1024 Mebibytes 十 ...
- WebSocket All In One
WebSocket All In One WebSocket heartbeat WebSocket 心跳检测 ping pong refs xgqfrms 2012-2020 www.cnblogs ...
- W3C & 弹幕
W3C & 弹幕 弹幕用例规范 Draft Community Group Report 21 August 2020 refs https://w3c.github.io/danmaku/u ...
- Webpack 4.x 默认支持 ES6 语法
Webpack 4.x 默认支持 ES6 语法 Q: 为什么 webpack4 默认支持 ES6 语法的压缩? A: terser 里面实现了 ES6 语法的 AST解析. webpack 4 里使用 ...