一、题目

给定一张 \(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. MySQL数据库系列(三)- MySQL常用引擎MyISAM和InnoDB区别详解

    概述 InnoDB:在MySQL 5.5及之后的版本,InnoDB是MySQL默认的事务型引擎,也是最重要和使用最广泛的存储引擎.它被设计成为大量的短期事务,短期事务大部分情况下是正常提交的,很少被回 ...

  2. Bootstrap 中的 aria-label 和 aria-labelledby

    正常情况下,form表单的input组件都有对应的label.当input组件获取到焦点时,屏幕阅读器会读出相应的label里的文本. <form> <div class=" ...

  3. gradle中的build script详解

    目录 简介 project和task 一个例子 task详细讲解 task脚本 task依赖 动态task 默认task build script的外部依赖 gradle中的build script详 ...

  4. Redis五大类型及底层实现原理

    目录 简单动态字符串链表字典跳跃表整数集合压缩列表对象 对象的类型与编码字符串对象列表对象哈希对象 集合对象有序集合对象类型检查与命令多态内存回收对象共享对象的空转时长 简单动态字符串  导读 Red ...

  5. prototype chain & prototype & __proto__

    prototype chain & prototype & proto prototype chain MDN https://developer.mozilla.org/en-US/ ...

  6. copy-webpack-plugin & ignore folder

    copy-webpack-plugin & ignore folder https://github.com/webpack-contrib/copy-webpack-plugin#ignor ...

  7. PM2 All In One

    PM2 All In One https://pm2.keymetrics.io/ https://pm2.io/ $ yarn global add pm2 # OR $ npm install p ...

  8. website URL & UTM

    website URL & UTM UTM user track message utm_source https://zhuanlan.zhihu.com/p/143473571?utm_s ...

  9. NGK与AOFEX交易所达成战略合作,BGV即将上线A网!

    据NGK官方消息,NGK官方已经与英国伦敦知名交易所AOFEX交易所达成战略合作,将于12月2日全球首发BGV,现已开启充值服务.同时,在12月3日15:00,用户可以参与BGV交易:在12月4日15 ...

  10. javascript初体验(一)

    数据类型 数字 (Number),整数或浮点数,例如42或3.14159 字符串 (String) 布尔值 (Boolean) null (js大小写敏感,因此null和NULL不一样) undefi ...