链接:

https://www.nowcoder.com/acm/contest/143/E

题意:

给定n个宿舍的新安排, 每个宿舍都有4个人, 问要至少有多少个人换位才能变成新安排。

可以建一个二分图, 左边n个点为原来的安排, 右边n个点为新安排,  每条边花费设为( 4 - 交集), 然后跑费用流。

#include<bits/stdc++.h>
using namespace std;
const int maxN = ;
const int maxM = 1e5 + ;
const int INF = 1e9 + ;
int n, ecnt, S, T;
struct {
int to, w, cost, nxt;
} edge[maxM]; struct Node{
int v, id;
}pre[maxN];
int head[maxN];
int bef[maxN][], after[maxN][]; void init() {
memset(head, -, sizeof(head));
ecnt = ;
}
void addEdge(int u, int v, int w, int c) {
edge[ecnt].to = v;
edge[ecnt].w = w;
edge[ecnt].cost = c;
edge[ecnt].nxt = head[u];
head[u] = ecnt++;
}
inline int dif(int a, int b) {
int res = ;
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
if(bef[a][i] == after[b][j]) {
res--;
break;
}
}
}
return res;
}
void build() {
S = , T = * n + ; for(int i = ; i <= n; i++) {
addEdge(S, i, , );
addEdge(i, S, , );
addEdge(i + n, T, , );
addEdge(T, i + n, , );
} for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
int c = dif(i, j);
addEdge(i, j + n, , c);
addEdge(j + n, i, , -c); //建反向边的时候注意花费要为负
}
}
}
int vis[maxN], cost[maxN];
bool spfa(){
queue<int> q;
memset(vis, , sizeof(vis));
fill(cost ,cost + maxN, INF);
vis[S] = ;
cost[S] = ;
q.push(S);
while(!q.empty()){
int u = q.front();
for(int i = head[u]; i != -; i = edge[i].nxt){
int v = edge[i].to, f = edge[i].w , c = edge[i].cost;
if(f == || cost[v] <= cost[u] + c) continue;
cost[v] = cost[u] + c;
pre[v].v = u;
pre[v].id = i;
if(!vis[v]){
q.push(v);
vis[v] = true;
}
}
vis[u] = ;
q.pop();
}
return cost[T] != INF;
}
int MCMF(){
int flow = ;
int minCost = ;
while(spfa()){
int minFlow = INF;
for(int i = T; i != S; i = pre[i].v){
minFlow = min(minFlow, edge[pre[i].id].w);
}
for(int i = T; i != S; i = pre[i].v){
edge[pre[i].id].w -= minFlow;
edge[pre[i].id ^ ].w += minFlow;
}
minCost += cost[T];
}
return minCost;
}
int main() {
// freopen("1.txt","r", stdin);
ios::sync_with_stdio(false); cin >> n;
init();
for(int i = ; i <= n; i++)
for(int j = ; j < ; j++)
cin >> bef[i][j]; for(int i = ; i <= n; i++)
for(int j = ; j < ; j++)
cin >> after[i][j]; build();
cout << MCMF() << "\n";
}

牛客网暑期ACM多校训练营(第五场) E room(最小费用最大流 , 最小权二分图匹配模板)的更多相关文章

  1. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  2. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  3. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  4. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  5. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

  6. 牛客网暑期ACM多校训练营(第九场)D

    链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...

  7. 牛客网暑期ACM多校训练营(第二场)B discount

    链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...

  8. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  9. 牛客网暑期ACM多校训练营(第二场) I Car 思维

    链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...

  10. 牛客网暑期ACM多校训练营(第二场) D money 思维

    链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...

随机推荐

  1. monxin cms 任意文件删除漏洞

    \program\diypage\receive\edit.php首先看到一个unlink($path);本来应该先看sql语句的,但知道是任意文件删除先跳过删除语句,看看$path怎么传入的倒推上去 ...

  2. 19 标签:xml或者html

    1       标签:xml或者html 1.1  使用XmlSlurper解析xml groovy处理xml非常容易.XmlSlurper 类用来处理xml.在处理xml方面,还有其他的处理方式,但 ...

  3. 一次dbcp和Hikaricp连接池比较联想到的线程池

    最近在测试连接池dbcp和Hikaricp速度时,为了弄清楚Hikaricp速度优势的原因,阅读了二者的源码,源码不是很难,类也没有多少,联想到很多知识,现在来总结一下.

  4. 基于Matlab的标记分水岭分割算法

    转自:http://blog.sina.com.cn/lyqmath 1 综述 Separating touching objects in an image is one of the more d ...

  5. mvc 连接数据库但单复值得问题

    1.  The model backing the ‘MusicStoreDBContext‘ context has changed since the database was created. ...

  6. IDEA JavaSE环境配置

    需指定JDK路径: File -> Project Structure -> Project -> Project SDK -> New -> 选择JDK所在的根目录

  7. Android setVisibility(View.GONE)无效的问题及原因分析

    解决方案:可以在setVisibility()之前调用clearAnimation()方法清除掉动画,或setFillAfter(false)(时间上该函数内部也调用了clearAnimation() ...

  8. iOS Runloop 消息循环

    介绍 Runloop是一种事件监听循环,可以理解成一个while死循环,监听到事件就起来,没有就休息. Runloop可以在不同模式下进行切换,iOS有五种模式,其中UIInitializationR ...

  9. Apache Kafka框架学习

    背景介绍 消息队列的比较 kafka框架介绍 术语解释 文件存储 可靠性保证 高吞吐量实现 负载均衡 应用场景 背景介绍: kafka是由Apache软件基金会维护的一个开源流处理平台,由scala和 ...

  10. SIT&UAT