2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]
题目链接:https://www.nowcoder.com/acm/contest/143/E
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Nowcoder University has 4n students and n dormitories ( Four students per dormitory). Students numbered from 1 to 4n.
And in the first year, the i-th dormitory 's students are (x1[i],x2[i],x3[i],x4[i]), now in the second year, Students need to decide who to live with.
In the second year, you get n tables such as (y1,y2,y3,y4) denote these four students want to live together.
Now you need to decide which dormitory everyone lives in to minimize the number of students who change dormitory.
输入描述:
The first line has one integer n. Then there are n lines, each line has four integers (x1,x2,x3,x4) denote these four students live together in the first year Then there are n lines, each line has four integers (y1,y2,y3,y4) denote these four students want to live together in the second year
输出描述:
Output the least number of students need to change dormitory.
输入
2
1 2 3 4
5 6 7 8
4 6 7 8
1 2 3 5
输出
2
说明
Just swap 4 and 5
备注
1<=n<=100 1<=x1,x2,x3,x4,y1,y2,y3,y4<=4n It's guaranteed that no student will live in more than one dormitories.
题意:
现有n个宿舍,4n个学生,即一个宿舍四个学生,学生从1~4n编号,
第一年,每个宿舍x1[i],x2[i],x3[i],x4[i]代表四个学生,
现在第二年,有n张表,每张表上y1,y2,y3,y4代表该宿舍四个学生分别想和谁一起住,
现在需要决策每个学生住哪个宿舍,使得换宿舍的学生数最少,输出这个数。
题解:
转化为最小费用最大流解决的二分图问题,对每个去年的宿舍,向每个今年的组合连一条边,权值为1,费用为需要搬的人数(4-相同的人数),源点到去年各点,今年各点到汇点,都连一条权值为1费用为0的最大流,跑一次费用流即可。(参考https://www.nowcoder.com/discuss/90015?type=101&order=0&pos=1&page=0)
说实话,第一次看到这题比较懵逼的,因为以为宿舍必须住四个人,但是看了题解之后,发现好像没有规定宿舍住的人数。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int INF=0X3f3f3f3f; struct Edge{
int u,v,c,f,a;
};
struct MCMF
{
int s,t;
vector<Edge> E;
vector<int> G[*maxn];
int vis[*maxn];
int d[*maxn];
int pre[*maxn];
int aug[*maxn];
void init(int l,int r)
{
for(int i=l;i<=r;i++) G[i].clear();
E.clear();
}
void addedge(int from,int to,int cap,int cost)
{
E.push_back((Edge){from,to,cap,,cost});
E.push_back((Edge){to,from,,,-cost});
int m=E.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool SPFA(int s,int t,int &flow,int &cost)
{
memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));
d[s]=, vis[s]=, pre[s]=, aug[s]=INF;
queue<int> q;
q.push(s);
while(!q.empty())
{
int now=q.front(); q.pop();
vis[now]=;
for(int i=;i<G[now].size();i++)
{
Edge& e=E[G[now][i]];
int nex=e.v;
if(e.c>e.f && d[nex]>d[now]+e.a)
{
d[nex]=d[now]+e.a;
pre[nex]=G[now][i];
aug[nex]=min(aug[now],e.c-e.f);
if(!vis[nex])
{
q.push(nex);
vis[nex]=;
}
}
}
}
if(d[t]==INF) return ;
flow+=aug[t];
cost+=d[t]*aug[t];
for(int i=t;i!=s;i=E[pre[i]].u)
{
E[pre[i]].f+=aug[t];
E[pre[i]^].f-=aug[t];
}
return ;
} int mincost()
{
int flow=,cost=;
while(SPFA(s,t,flow,cost));
return cost;
}
}mcmf; int n;
struct Dorm{
int x[];
}fst[maxn],snd[maxn];
int cost(Dorm fst,Dorm snd)
{
int res=;
for(int i=;i<;i++)
{
bool stay=;
for(int j=;j<;j++) stay|=(fst.x[i]==snd.x[j]);
res-=stay;
}
return res;
} int main()
{
scanf("%d",&n);
mcmf.init(,*n+);
mcmf.s=, mcmf.t=*n+; for(int i=;i<=n;i++) for(int k=;k<;k++) scanf("%d",&fst[i].x[k]);
for(int i=;i<=n;i++) for(int k=;k<;k++) scanf("%d",&snd[i].x[k]); for(int i=;i<=n;i++)
{
mcmf.addedge(mcmf.s,i,,);
mcmf.addedge(n+i,mcmf.t,,);
for(int j=;j<=n;j++) mcmf.addedge(i,n+j,,cost(fst[i],snd[j]));
} printf("%d\n",mcmf.mincost());
}
2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)
Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)
题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...
- 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]
题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 2018牛客网暑期ACM多校训练营(第二场) A - run - [DP]
题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...
- 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]
题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs and where are i ...
- 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]
题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述 Given a sequence of integers a1, a2, ..., an a ...
- 2018牛客网暑期ACM多校训练营(第九场)A -Circulant Matrix(FWT)
分析 大佬说看样例就像和卷积有关. 把题目化简成a*x=b,这是个xor的FWT. FWT的讲解请看:https://www.cnblogs.com/cjyyb/p/9065615.html 那么要求 ...
随机推荐
- iOS分辨率的那些事儿(转)
1 iOS设备的分辨率 iOS设备,目前最主要的有3种(Apple TV等不在此讨论),按分辨率分为两类 iPhone/iPod Touch 普屏分辨率 320像素 x 480像素 Retina ...
- C++实现按1的个数排序
题目内容:有一些0.1字符串,将其按1的个数的多少的顺序进行输出. 输入描述:本题只有一组测试数据.输入数据由若干数字组成,它是由若干个0和1组成的数字. 输出描述:对所有输入的数据,按1的个数进行生 ...
- VS中自定义代码段
如果数据属性的数量比较多,那么输入总是要花费较多的时间,这里有个小技巧,就是使用快捷的输入方法,但是VS自身提供的代码段是有限的,幸运的是我们可以通过:工具> 代码段管理器>添加来添加自定 ...
- linux-满足多字符条件统计行数
测试数据: 2017-10-24 14:14:11:1123 [ INFO] order_type=add,order_id=9150882564978710367790292017-10-24 14 ...
- java List分批处理
java List分批处理,例如对List中的数据进行批量插入. 方法一: /** * ClassName:Test List分批处理 * @author Joe * @version * @sinc ...
- XPath的初步认识
嘿嘿,最近开始上班,不是过于太忙,而是自己一直在学习一些项目中用到的而我暂时还没接触的知识,WCF,log4等,感觉还没有总结的需要吧,虽然都了解啦,但是暂时还是初步的学习,基础的暂时是知道啦,还没有 ...
- (原)android修改文件所属的用户组
首先得安装了busybox: 命令如下: busybox fileName 其中的0表示root,改成1000则表示system,改成2000则表示shell.
- Android学习之蓝牙操作
BluetoothAdapter 用法 蓝牙运行原理:通过BluetoothAdapter 蓝牙适配器处理任务,如果蓝牙被启动之后,系统会自动去搜索其它设备,如果匹配到附近的设备就发送一个广播,Bro ...
- shell 获取脚本的绝对路径
basepath=$(cd `dirname $0`; pwd) 在此解释下basepath : dirname $0,取得当前执行的脚本文件的父目录 cd `dirname $0`,进入这个目录(切 ...
- 二分法求平方根(Python实现)
使用二分法(Bisection Method)求平方根. def sqrtBI(x, epsilon): assert x>0, 'X must be non-nagtive, not ' + ...