题意:

  一共有n个宿舍,每个宿舍有4个人。给出第一年的人员分布和第二年的人员分布,问至少有多少人需要移动。

题解: 

  对于第一年的每个宿舍,向今年的每种组合连边。流量为1,费用为(4 - 组合中已在该宿舍的人数)。

  最后跑一边费用流。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
const int INF = 0x3f3f3f3f;
int n, k;
int x[], y[];
int g[][];
typedef pair<int, int> P;
struct edge { int to, cap, cost, rev; };
int V;
vector<edge> G[N];
int h[N];
int dist[N];
int prevv[N], preve[N];
void add_edge(int from, int to, int cap, int cost) {
G[from].push_back((edge){to, cap, cost, (int)G[to].size()});
G[to].push_back((edge){from, , -cost, (int)G[from].size()-});
}
int min_cost_flow(int s, int t, int f) {
int res = ;
fill(h, h + V, );
while(f > ) {
priority_queue<P, vector<P>, greater<P> > que;
fill(dist, dist + V, INF);
dist[s] = ;
que.push(P(, s));
while(!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
int len = G[v].size();
for(int i = ; i < len; i++) {
edge &e = G[v][i];
if(e.cap > && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push(P(dist[e.to], e.to));
}
}
}
if(dist[t] == INF) return -;
for(int v = ; v < V; v++) h[v] += dist[v];
int d = f;
for(int v = t; v != s; v = prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);
f -= d;
res += d*h[t];
for(int v = t; v != s; v = prevv[v]) {
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++)
for(int j = ; j <= ; j++) {
scanf("%d", &k);
x[k] = i;
y[k] = j;
}
for(int i = ; i <= n; i++)
for(int j = ; j <= ; j++) {
scanf("%d", &k);
g[x[k]][y[k]] = i;
}
for(int i = ; i <= n; i++) add_edge(, i, , );
for(int i = n+; i <= *n; i++) add_edge(i, *n+, , );
for(int i = ; i <= n; i++) {
for(int j = n+; j <= *n; j++) {
int cnt = ;
for(int k = ; k <= ; k++) if(g[i][k] == j-n) cnt--;
add_edge(i, j, , cnt);
}
}
V = *n+;
printf("%d\n", min_cost_flow(, *n+, n));
}

2018牛客多校第五场 E.room的更多相关文章

  1. 2018牛客多校第五场 H.subseq

    题意: 给出a数组的排列.求出字典序第k小的b数组的排列,满足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0. 题解: 用树状数组倒着求出以 ...

  2. 牛客多校第五场 F take

    链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 题目描述 Kanade has n boxes , the i-th box has p[i] ...

  3. 牛客多校第五场 J:Plan

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

  4. 牛客多校第五场-D-inv

    链接:https://www.nowcoder.com/acm/contest/143/D来源:牛客网 题目描述 Kanade has an even number n and a permutati ...

  5. 牛客多校第五场 F take 期望转化成单独事件概率(模板) 树状数组

    链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 Kanade has n boxes , the i-th box has p[i] proba ...

  6. 牛客多校第五场 E room 二分图匹配 KM算法模板

    链接:https://www.nowcoder.com/acm/contest/143/E来源:牛客网 Nowcoder University has 4n students and n dormit ...

  7. 字符串dp——牛客多校第五场G

    比赛的时候脑瘫了没想出来..打多校以来最自闭的一场 显然从s中选择大于m个数组成的数必然比t大,所以只要dp求出从s中选择m个数大于t的方案数 官方题解是反着往前推,想了下反着推的确简单,因为高位的数 ...

  8. 【魔改】树状数组 牛客多校第五场I vcd 几何+阅读理解

    https://www.nowcoder.com/acm/contest/143/I vc-dimension 题解:分三种情况,组合数学算一下,其中一种要用树状数组维护 技巧(来自UESTC):1. ...

  9. 2018牛客多校第六场 G.Pikachu

    题意: 给出一棵n个点的树,每条边有边权.对这个树加边变成一个完全图.新加的边的权值为边上两点在树上的距离.求完全图上任意两点的最大流之和. 题解: 一共有C(n,2)个点对.假设当前求s到t之间的最 ...

随机推荐

  1. SpringBoot学习:添加JSP支持

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)pom中添加依赖: <!-- https://mvnrepository.c ...

  2. leetcode笔记--7 Find the Difference

    question: Given two strings s and t which consist of only lowercase letters. String t is generated b ...

  3. 使用nmon监控得出网络实时速度以及最大、最小、平均网络传送速度

    首先我们得搞清楚几个概念,即什么是网速?什么是带宽? 举两个个例子: 1.家里装网线,宽带提供商说我们的带宽是100兆. 2.用迅雷下载电影,迅雷显示实时的下载速度是每秒3兆,或者说是3MB/s. 这 ...

  4. jdbc 连接各种数据库

    package com.fh.controller.ruitai.util; import java.sql.Connection; import java.sql.DriverManager; im ...

  5. 即刻开始使用Kotlin开发Android的12个原因(KAD 30)

    作者:Antonio Leiva 时间:Jul, 11, 2017 原文链接:https://antonioleiva.com/reasons-kotlin-android/ 这组文章已到最后了,它们 ...

  6. POSTMan 快速上手(一图带你玩 Postman )

    POSTMan 快速上手(一图带你玩 Postman ):

  7. ortp打印日志

    //向字符串中打印数据 static char* ms_strdup_vprintf(const char *fmt, va_list ap) { ; char *p,*np; #ifndef WIN ...

  8. Unity自带标准资源包中的特效

  9. 前端开发工程师 - 01.页面制作 - 第1章.Photoshop切图

    第1章--Photoshop切图 工具.面板.视图 什么是切图? 1. 从设计稿(.psd)中切出网络素材,如按钮.图标.logo.背景图等 2. 编写代码,在代码中使用图片,生成静态页面 --给网页 ...

  10. Map Reduce Application(Top 10 IDs base on their value)

    Top 10 IDs base on their value First , we need to set the reduce to 1. For each map task, it is not ...