题目链接:https://www.nowcoder.com/acm/contest/143/E

时间限制:C/C++ 1秒,其他语言2秒
空间限制: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 - [最小费用最大流模板题]的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  3. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  4. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  5. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  6. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

  7. 2018牛客网暑期ACM多校训练营(第二场) A - run - [DP]

    题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...

  8. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

  9. 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述  Given a sequence of integers a1, a2, ..., an a ...

  10. 2018牛客网暑期ACM多校训练营(第九场)A -Circulant Matrix(FWT)

    分析 大佬说看样例就像和卷积有关. 把题目化简成a*x=b,这是个xor的FWT. FWT的讲解请看:https://www.cnblogs.com/cjyyb/p/9065615.html 那么要求 ...

随机推荐

  1. Git------如何使用Git Bash Here提交代码

    转载:码云帮助文档地址 http://git.mydoc.io/?t=154712 1.打开“Git Bash Here” 2.输入: ssh-keygen -t rsa -C "xxxxx ...

  2. JAVA内存泄露分析及解决

    一,问题产生     项目采用Tomcat6.0为服务器,数据库为mysql5.1,数据库持久层为hibernate3.0,以springMVC3.0为框架,项目开发完成后,上线前夕进行稳定性拷机,测 ...

  3. ubuntu 13.04 nginx.conf 配置详解

    1.nginx.conf 文件,路径为:/etc/nginx/agin.conf #使用的用户和组 user www-data; #指定工作衍生进程数(一般等于CPU总核数或总核数的两倍) worke ...

  4. 关于openssl的编译与使用

    关于openssl的编译与使用,可以参考这两往篇文章 http://blog.csdn.net/lazyclough/article/details/7456131 http://www.leaves ...

  5. JavaScript之 for...in

    for-in 可以用来枚举对象的属性,还有数组的索引,用法: 枚举对象属性 var o={name:'a',age:25,sex:'male'} for(var each in o){ console ...

  6. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  7. 【数据分析】Superset 之一 准备

    https://segmentfault.com/a/1190000005083953 http://blog.csdn.net/bingoxubin/article/details/78583165 ...

  8. 【Java知识点专项练习】之 接口和抽象类的区别

    接口和抽象类的区别 接口(interface)可以说成是抽象类的一种特例,接口中的所有方法都必须是抽象的.接口中的方法定义默认为public abstract类型,接口中的成员变量类型默认为publi ...

  9. FileReader类和FileWriter类的基本用法示例

    package com.example.io; import java.io.File; import java.io.FileReader; import java.io.FileWriter; i ...

  10. Qt——布局管理器

    教程地址 运行截图: 代码: #include "mainwindow.h" #include <QApplication> #include <QHBoxLay ...