题意:有n个寝室,每个寝室4个人,现在在搞搬寝室的活动,告诉你每个寝室之前的人员名单,和之后的人员名单,问最少需要几个人要搬寝室。

思路:

转化为最小费用最大流解决的二分图问题,对每个去年的宿舍,向每个今年的组合连一条边,权值为1,费用为需要搬的人数(4-相同的人数),源点到去年各点,今年各点到汇点,都连一条权值为1费用为0的最大流,跑一次费用流即可。

 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; // template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------show time----------------*/
int n,m;
const int maxn =;
const int max_edge = ;
struct Edge{
int to,nxt = -;
ll val,cost;
};
Edge gEdges[max_edge*];
ll gHead[maxn],gPre[maxn],gDist[maxn],gPath[maxn];
bool in[maxn];
int gCount = ;
void addEdge(int u,int v,ll c,ll w){
gEdges[gCount].to = v;
gEdges[gCount].val = c;
gEdges[gCount].cost = w;
gEdges[gCount].nxt = gHead[u];
gHead[u] = gCount++; gEdges[gCount].to = u;
gEdges[gCount].val = ;
gEdges[gCount].cost = -*w;
gEdges[gCount].nxt = gHead[v];
gHead[v] = gCount++;
} bool spfa(int s,int t){
// memset(gDist, inff, sizeof(gDist)); for(int i=; i<=*n+; i++){
gDist[i] = inff;
}
memset(gPre , - , sizeof(gPre));
memset(in,false,sizeof(in));
queue<int>Q;
Q.push(s); in[s] = true;
gDist[s] = ;
while(!Q.empty()){
int u = Q.front(); Q.pop();
in[u] = false;
for(int e = gHead[u]; e!=-; e = gEdges[e].nxt){
int v = gEdges[e].to;
if(gEdges[e].val> && gDist[v] > gDist[u] + gEdges[e].cost){
gDist[v] = gDist[u] + gEdges[e].cost;
gPre[v] = u;
gPath[v] = e;
if(in[v]==false){
Q.push(v);
in[v] = true;
}
}
}
}
if(gPre[t]==-)return false;
else return true;
}
ll flow = ;
ll MinCostFlow(int s,int t){
ll cost = ;
while(spfa(s,t)){
ll f = inff; for(int u = t; u!=s; u = gPre[u]){
if(f > gEdges[gPath[u]].val)
f = gEdges[gPath[u]].val;
}
flow += f;
cost += 1ll*gDist[t] * f;
for(int u=t; u!=s; u=gPre[u]){
gEdges[gPath[u]].val -= f;
gEdges[gPath[u]^].val += f;
}
}
return cost;
}
struct room{
int a,b,c,d;
}rm[maxn];
int getw(int x,int y){
int res = ;
res -= (rm[x].a == rm[y].a ||rm[x].a == rm[y].b||rm[x].a == rm[y].c||rm[x].a == rm[y].d);
res -= (rm[x].b == rm[y].a ||rm[x].b == rm[y].b||rm[x].b == rm[y].c||rm[x].b == rm[y].d);
res -= (rm[x].c == rm[y].a ||rm[x].c == rm[y].b||rm[x].c == rm[y].c||rm[x].c == rm[y].d);
res -= (rm[x].d == rm[y].a ||rm[x].d == rm[y].b||rm[x].d == rm[y].c||rm[x].d == rm[y].d);
return res;
}
int main(){
memset(gHead,-,sizeof(gHead));
int s,t;
scanf("%d", &n);
for(int i=; i<=n*; i++){
scanf("%d%d%d%d", &rm[i].a, &rm[i].b, &rm[i].c, &rm[i].d);
} for(int i=; i<=n; i++){
addEdge(,i,,);
addEdge(n+i,*n+,,);
for(int j=+n; j<=*n; j++){
addEdge(i,j,,getw(i,j));
}
}
ll cost = MinCostFlow(,*n+);
printf("%lld\n" ,cost);
return ;
}

牛客

参考:

作者:东林AotoriChiaki
链接:https://www.nowcoder.com/discuss/90015?type=101&order=0&pos=1&page=0
来源:牛客网

牛客2018多校第五场E-room 最小费用最大流的更多相关文章

  1. 牛客暑假多校第五场A.gpa

    一.题意 给出你的N门课程的考试成绩和所占的机电数目.允许你放弃K门课的成绩,要求你的平均学分绩最高能达到多少. Kanade selected n courses in the university ...

  2. 牛客2018多校第六场 J Heritage of skywalkert - nth_element

    传送门 题意:提供一个随机生成函数,让你生成n个数,然后问你其中能找到的两个数的最小公倍数 最大 是多少. 思路:可以用nth_element()函数在O(n)下求出前 15 个大的数(当然,100个 ...

  3. 牛客暑假多校第五场 D inv

    题意:给你一个n, 接来下给你一个 [1,n] 中偶数的排列, 还有一个 [1, n] 中 奇数 按照递增的顺序排列, 现在求一个原数列, 使得偶数列排序 和 奇数列 都是原数列的一个子序列, 现在求 ...

  4. 牛客暑假多校第五场 I vcd

    这个题目一个队友没读懂, 然后我读错了题目, 还让他堆了半天的公式写了半天的代码, 交上去一直0.0, 另一队友问题目有没有读错, 我坚持没有读错, 然后坑了2个小时的时间,不然应该会早一点做出来. ...

  5. 2019牛客暑期多校第五场题解ABGH

    A.digits 2 传送门 题意:给你一个n,要求输出一个每一位数字之和能整除n且其本身也能整除n的数.n不超过100,要求的数不超过10000位数. 题解:直接将n输出n次. 代码: #inclu ...

  6. 2019HDU多校第三场 K subsequence——最小费用最大流

    题意 给定一个 $n$ 个整数的数列,从中至多选取 $k$ 个上升子序列(一个元素最多被选一次),使得选取的元素和最大. 分析 考虑这个问题和经典网络流问题“最长不下降子序列”相似,我们考虑对这个建图 ...

  7. 牛客网多校第3场C-shuffle card 平衡树或stl(rope)

    链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...

  8. 牛客网多校第3场Esort string (kmp)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

  9. 牛客网多校赛第九场A-circulant matrix【数论】

    链接:https://www.nowcoder.com/acm/contest/147/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...

随机推荐

  1. PHP xdebug API接口优化揪出了getimagesize这个鬼

    在API优化list中,公司客户系统的服务号客服有个获取聊天消息的接口getHistory请求时间很长,就去优化了下,记下过程. 一,配置环境,追踪使用Xdebug: 1.在https://xdebu ...

  2. 四、Python基础(1)

    目录 四.Python基础(1) 四.Python基础(1) 1.什么是变量? 一种变化的量,量是记录世界上的状态,变指得是这些状态是会变化的. 2.为什么有变量? 因为计算机程序的运行就是一系列状态 ...

  3. Lasso估计学习笔记(二)

    先看Lasso估计学习笔记(一),这篇是续的上一篇

  4. 分布式存储——ceph 的 python 基础接口

    python 使用 boto 库完成分布式存储读.写.判断接口 import boto import boto.s3.connection from boto.s3.key import Key im ...

  5. 使用 php 内部web服务器

    使用 php 内部web服务器如网站目录 d:\web\index.php1.打开命令窗口,输入下列3条命令cd d:cd d:\web\index.phpphp -S localhost:80802 ...

  6. 移动开发-UI设计

        UI:手机的用户界面 UI物理版:手机实际的屏幕像素 UI设计版:我们截屏的手机界面在ps中去量,发现的尺寸 UI放大版:手机的尺寸等比放大1.5倍得出的分辨率   响应式布局 原由:窗体缩小 ...

  7. TinyMCE使用

    1.文本&文本字体颜色 与word类似不赘述 2.字体加粗&划线 与word类似不赘述 选中后  ctrl + B  加粗快捷键 选中后  ctrl + I  斜体快捷键 选中后  c ...

  8. Django上线部署之IIS

    环境: 1.Windows Server 2016 Datacenter 64位 2.SQL Server 2016 Enterprise 64位 3.Python 3.6.0 64位 4.admin ...

  9. maven学习(2)仓库和配置

    1:本地资源库.中央存储库.远程存储库 1.1   本地资源库 当你建立一个 Maven 的项目,Maven 会检查你的 pom.xml 文件,以确定哪些依赖需要下载.首先,Maven 将从本地资源库 ...

  10. Spring-Boot:Profile简单示例

    //Resources目录下创建 application.properties spring.profiles.active=prod //Resources目录下创建 application-pro ...