链接:

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. CF #541div2 E

    题目本质:忽略串的变化,只记载26个字母的相关变化. 解决方法: 在上一次与本次的转移过程中,情况并不多,主要取决于本次串的首尾字母,若不是本次的首尾字母,会被置1:如果是的话,分情况接一下并更新.另 ...

  2. Codeforces 997D(STL+排序)

    D. Divide by three, multiply by two time limit per test 1 second memory limit per test 256 megabytes ...

  3. hdu2586 LCA带边权的Targan算法

    bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=2586 #include<bits/stdc++.h> using names ...

  4. shell脚本实现自动化备份

    1.备份规则: 在生产环境中有若干服务器需要定时将服务器中应用程序,以及数据库等进行备份.要求在本地服务器中保存近一周的备份,备份服务器中保存最近一月的备份文件.                    ...

  5. Y2165终极分班考试题。

    第一题答案:D 2.下面关于SQLServer中视图的说法错误的是:C 答案:视图是数据中存储的数据值得集合. 3.在JAVA中,关于日志记录工具log4j的描述错误的是:D 答案:log4j个输出级 ...

  6. MVC系列学习(十六)-区域的学习

    1.查找控制器的过程 1.1调用其他项目中的控制器 a.先到网站根目录下的bin文件夹下,遍历所有的程序集 b.找到以Controller结尾的类 c.再找出其中继承了Controller的类 d.接 ...

  7. .Net 遍历目录下第一层的子文件夹和子文件夹里的文件

    今天再完成一道任务的时候需要遍历得到所有txt文件,搜索很久终于得到了一个很方便的方法. foreach (string o in Directory.GetDirectories(@"D: ...

  8. mybatis实现使用原生的sql

    1.相应的xml文件中 <select id="selectByCategories" resultType="map" parameterType=&q ...

  9. arcgis jsapi接口入门系列(2):图层基础操作

    //图层相关demo layerFun: function () { //获取地图的所有图层(不包括的图层类型:底图图层(basemaps)) let layers = this.map.layers ...

  10. windows的cmd和git bash的常用命令

    windows下使用git bash,使用的事linux下的命令,整理常用命令如下: windows下的命令 linux下的命令 命令的含义 cd e:\xx cd /e/xx 切换到xx目录 cd ...