链接:

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. 《统计学习方法》笔记九 EM算法及其推广

    本系列笔记内容参考来源为李航<统计学习方法> EM算法是一种迭代算法,用于含有隐变量的概率模型参数的极大似然估计或极大后验概率估计.迭代由 (1)E步:求期望 (2)M步:求极大 组成,称 ...

  2. 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历

    给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7],    3   ...

  3. STM32的低功耗模式

    一 待机模式standby和STOP模式的区别: 进入低功耗模式:都一样,都是先关闭相应时钟,关闭相应外设,配置相应所有IO口(浮动输入),然后配置相应的唤醒中断源,中断影响的O口,然后调用相应函数进 ...

  4. axios delete 请求

    axios delete 请求 在传递一个参数的时候,直接把参数放在请求连接后面,用'/' 连接就可以了 this.axios.post(this.APIURL+'/'+ID) //http://ww ...

  5. Ceizenpok’s formula Gym - 100633J 扩展Lucas定理 + 中国剩余定理

    http://codeforces.com/gym/100633/problem/J 其实这个解法不难学的,不需要太多的数学.但是证明的话,我可能给不了严格的证明.可以看看这篇文章 http://ww ...

  6. watir-webdriver使用过程中异常

    1.在jruby版本1.6.7中,报异常:not such file to load --watir-webdriver 解决方法 :在文件的首行添加:require 'rubygems'       ...

  7. Spring框架学习——AOP的开发

    一.AOP开发中的相关术语. ——JoinPoint(连接点):指那些可以被拦截到的点.比如增删改查方法都可以增强,这些方法就可以被称为是连接点. ——PointCut:切入点,真正被拦截的点,指对哪 ...

  8. bootstrap输入框组、导航和导航条

    输入框组(input groups) 避免使用select  支持不好,使用输入框组 尺寸根据  input-group-lg    input-group-sm来选择   <div class ...

  9. top 进程管理

    top 动态查看进程 前五行解释: 第一行参数说明: top - 07:06:19    当前时间 up 10 min,  系统运行时间,格式为时:分 1 user,  当前登录用户数 load av ...

  10. OAuth2.0基本原理及应用

    OAuth2.0基本原理及应用 一.OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 在详细讲解OAuth 2.0之前,需要了解几个专 ...