牛客网暑期ACM多校训练营(第五场) E room(最小费用最大流 , 最小权二分图匹配模板)
链接:
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(最小费用最大流 , 最小权二分图匹配模板)的更多相关文章
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网 暑期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 ,问你 ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- 牛客网暑期ACM多校训练营(第九场) A题 FWT
链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...
- 牛客网暑期ACM多校训练营(第九场)D
链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...
- 牛客网暑期ACM多校训练营(第二场)B discount
链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 牛客网暑期ACM多校训练营(第二场) I Car 思维
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...
- 牛客网暑期ACM多校训练营(第二场) D money 思维
链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...
随机推荐
- shell脚本实现自动化备份
1.备份规则: 在生产环境中有若干服务器需要定时将服务器中应用程序,以及数据库等进行备份.要求在本地服务器中保存近一周的备份,备份服务器中保存最近一月的备份文件. ...
- Unity EditorWindow知识记录
1.创建EditorWindow using UnityEditor; using UnityEngine; public class ZZEditorWindow : EditorWindow { ...
- php 几个比较实用的函数
最近在看代码,发现以下是几个比较实用的函数. 1,取客户端IP 查看复制打印? function getOnlineIp() { $strOnlineIp = ""; if(get ...
- nginx 日志 log_format 及字段说明
1.log_format 普通格式 log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$stat ...
- Java-IDEA环境搭建swagger
1.项目POM导入包(使用Maven管理的代码) 2.POM文件导入包 <dependencyManagement> <dependencies> <dependency ...
- ES6中新增的字符串方法
实例方法:includes(), startsWith(), endsWith() 传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6 又提供 ...
- ACCESS中通过一个字段补齐更新另一个字段
[干货从下面红字开始] 搞了好久的一个问题终于有结果…… 根据学生进出馆的次数和学生报名人数来分配自习间 学生报名是通过工号(学号)来报名的: 而门禁系统统计出来的数据有绝一大部分仅有 卡号没有 工号 ...
- 声明已被否决 VS C++
error C4996声明已被否决,不止一次碰到这个问题,在这里必须mark一下! 尝试这个1.Project Properties > Configuration Properties > ...
- 安卓ListView基础应用
listview简单描述 主页面: package com.example.listview; import com.lidroid.xutils.ViewUtils; import com.lidr ...
- java文件读写链接流向
1)字节流 读写的链接流向源节点->FileInputStream->BufferedInputStream->ObjectInputStream->程序 程序->Obj ...