一、题目

给定一张 \(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的更多相关文章

  1. 【hihocoder 1424】 Asa's Chess Problem(有源汇上下界网络流)

    UVALive-7670 ICPC北京2016-C题 hihocoder 1424 题意 有个 \(N\times N\) 的棋盘,告诉你每个格子黑色(1)或白色(0),以及每对能相互交换的同行或同列 ...

  2. [HihoCoder-1424] Asa's Chess Problem

    有上下界的费用流 #include <stdio.h> #include <algorithm> #include <queue> #include <cst ...

  3. UVa 750 - 8 Queens Chess Problem

    题目大意:八皇后问题,在一个8*8的棋盘上,放置8个皇后,使得任意两个皇后不在同一行上.不在同一列上.不在同一条对角线上,不过这道题预先给定了一个位置放置一个皇后,让你输出所有可能的答案. 经典的回溯 ...

  4. [2019HDU多校第二场][HDU 6591][A. Another Chess Problem]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6591 题目大意:二维坐标系上,所有满足\(5|2x+y\)的点都被设为障碍物,无法通过.现给出一对点, ...

  5. 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 ] 给 ...

  6. HDU 5742 Chess SG函数博弈

    Chess Problem Description   Alice and Bob are playing a special chess game on an n × 20 chessboard. ...

  7. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  8. 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的网格,让你 ...

  9. HDU 4405:Aeroplane chess(概率DP入门)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description   Hzz loves ...

随机推荐

  1. Python_小程序

    一.开发前的准备工作 1.申请AppID:一个账号对应一个小程序,个人/个体只能申请5个小程序 2.下载开发工具 二.小程序的文件结构 三. 1.数据绑定 1.1数据的设置 Page( data:{ ...

  2. NFS 共享存储

    目录 环境准备 NFS服务端 NFS客户端 部署时常见报错 httpd服务 NFS 共享存储的坑 环境准备 主机名 WanIP(Wide Area Network) LanIP(Local Area ...

  3. Redis内存管理中的LRU算法

    在讨论Redis内存管理中的LRU算法之前,先简单说一下LRU算法: LRU算法:即Least Recently Used,表示最近最少使用页面置换算法.是为虚拟页式存储管理服务的,是根据页面调入内存 ...

  4. Tomcat基本原理

    思考 :怎样让Tomcat具备Web服务的功能呢? 在服务端用HTTP来监听,协议不好写,不妨用Java封装好的Socket作为监听. class MyTomcat{ ServerSocket ser ...

  5. 微服务架构Day04-SpringBoot之web开发

    引入项目 把html页面放在模板引擎文件夹templates下,这样能使用模板引擎的功能. 登录页面国际化 国际化:编写国际化配置文件 1.编写国际化配置文件,抽取页面需要显示的国际化消息 2.Spr ...

  6. JavaScript 的 7 种设计模式

    原文地址:Understanding Design Patterns in JavaScript 原文作者:Sukhjinder Arora 译者:HelloGitHub-Robert 当启动一个新的 ...

  7. Next.js SSR Tutorials

    Next.js SSR Tutorials https://codesandbox.io/s/nextjs-demo-h49zt cli $ npx create-next-app ssr-demo- ...

  8. 你所不知道的 JS: null , undefined, NaN, true==1=="1",false==0=="",null== undefined

    1 1 1 === 全相等(全部相等) ==  值相等(部分相等) demo: var x=0; undefined var y=false; undefined if(x===y){ console ...

  9. GitHub Ribbons : 谈网站的安全性-资源链接如何 预防/实现 爬虫的批量下载!

    GitHub Ribbons : 谈网站的安全性-资源链接如何 预防/实现 爬虫的批量下载! 预防方法: 1. 使用随机数字符串,拼接URL! https://camo.githubuserconte ...

  10. ES2020 All in One

    ES2020 All in One ES2020 new features / ES11 ES2020 中的10个新功能 1. BigInt BigInt是JavaScript中最令人期待的功能之一, ...