2018牛客多校第五场 E.room
题意:
一共有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的更多相关文章
- 2018牛客多校第五场 H.subseq
题意: 给出a数组的排列.求出字典序第k小的b数组的排列,满足1<=bi<=n,bi<bi+1,a[b[i]]<a[b[i+1]],m>0. 题解: 用树状数组倒着求出以 ...
- 牛客多校第五场 F take
链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 题目描述 Kanade has n boxes , the i-th box has p[i] ...
- 牛客多校第五场 J:Plan
链接:https://www.nowcoder.com/acm/contest/143/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524 ...
- 牛客多校第五场-D-inv
链接:https://www.nowcoder.com/acm/contest/143/D来源:牛客网 题目描述 Kanade has an even number n and a permutati ...
- 牛客多校第五场 F take 期望转化成单独事件概率(模板) 树状数组
链接:https://www.nowcoder.com/acm/contest/143/F来源:牛客网 Kanade has n boxes , the i-th box has p[i] proba ...
- 牛客多校第五场 E room 二分图匹配 KM算法模板
链接:https://www.nowcoder.com/acm/contest/143/E来源:牛客网 Nowcoder University has 4n students and n dormit ...
- 字符串dp——牛客多校第五场G
比赛的时候脑瘫了没想出来..打多校以来最自闭的一场 显然从s中选择大于m个数组成的数必然比t大,所以只要dp求出从s中选择m个数大于t的方案数 官方题解是反着往前推,想了下反着推的确简单,因为高位的数 ...
- 【魔改】树状数组 牛客多校第五场I vcd 几何+阅读理解
https://www.nowcoder.com/acm/contest/143/I vc-dimension 题解:分三种情况,组合数学算一下,其中一种要用树状数组维护 技巧(来自UESTC):1. ...
- 2018牛客多校第六场 G.Pikachu
题意: 给出一棵n个点的树,每条边有边权.对这个树加边变成一个完全图.新加的边的权值为边上两点在树上的距离.求完全图上任意两点的最大流之和. 题解: 一共有C(n,2)个点对.假设当前求s到t之间的最 ...
随机推荐
- CLR via c#读书笔记五:方法
注:书本第8章:方法 实例构造器和类(引用类型) 构造器方法在“方法定义元数据表”中始终叫做.ctor(constructor的简称). 构造引用类型的对象,在调用类型的实例构造器之前,为对象分配的内 ...
- Drupal 判断匿名用户必须先登录的解决方法
要实现如果是匿名用户点击checkout链接,要求先登录 方案一.通过添加Rules规则实现 EVENT:After adding a product to the cart Conditions : ...
- MyBatis-MBG(MyBatis Generator)
1.添加jar包 <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>m ...
- Appium(Python)驱动手机淘宝App
请注意操作步骤: 1. 用数据线连接手机, 打开开发者模式, 并赋予相关权限, 并保持不锁屏状态: 2. 启动Appium桌面服务端: 3. 运行程序: 首次运行, Appium会在手机上安装3个Ap ...
- 175. Invert Binary Tree【LintCode by java】
Description Invert a binary tree. Example 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 解题:题目要求讲二叉树的左子树和右子树对调 ...
- python中spilt()函数和os.path.spilt()函数区别
Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...
- 接口_requests_基于python
HTTP request python官方文档:http://cn.python-requests.org/zh_CN/latest/ 1. 环境 基于环境,需要安装requests 模块,安装方法 ...
- 第一、二章——Python简介与Python基础
前言:<Data Wrangling with Python>这本书主要是讲使用Pyhon来处理各种类型保存的数据的. 第一章:Python简介 1.版本选择 本书选择的Python版本是 ...
- iOS音频播放概述
在iOS系统中apple对音频播放需要的操作进行了封装并提供了不同层次的接口 下面对其中的中高层接口进行功能说明: Audio File Services:读写音频数据,可以完成播放流程中的第2步: ...
- asp.net 后台注册(调用)JS
1.使用Page.ClientScript.RegisterClientScriptBlock 使用 Page.ClientScript.RegisterClientScriptBlock可以防止ja ...