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之间的最 ...
随机推荐
- python之saltstack二次开发
一.salt的概念 salt是一个配置管理系统,能够维护预定义状态的远程节点(比如,确保指定的报被安装,指定的服务在运行).一个分布式远程执行系统,用来在远程节点(可以是单个节点,也可以是任意规则挑选 ...
- 问题集 - console.log在IE下不可用
js中添加如下一段代码即可. if(!window.console){ window.console = {}; } if(!window.console.log){ window.console.l ...
- ajax跨域请求php
在众多站群中,不同功能的系统使用独立的一个域名,各系统之间存在相互调用的关系.使用js的XMLHttpRequest调用其他域名提示跨域权限不足.有些可能认为都同属于同一个顶级域名或者说域名一模一样怎 ...
- 【JUC源码解析】AQS
简介 AQS,也即AbstractQueuedSynchronizer,抽象队列同步器,提供了一个框架,可以依赖它实现阻塞锁和相关同步器.有两种类型,独占式(Exclusive)和共享式(Share) ...
- 使用ListView+ObjectDataSource+DataPager实现增删改查加分页
一.配置objectDataSource 选择业务逻辑层的类 二.配置Select对应的方法,必须是一个带两个整型参数的方法,第一个参数表示要查看的第一条记录的前一条30,第二个参数每页最多能显示的记 ...
- 「日常训练」 Longest Run on a Snowboard (UVA-10285)
题意 其实就是一条二维的LIS,但是还是做的一愣一愣的,多努力. 考虑$dp[i][j]$为从(i,j)出发的二维LIS的最大值,那么$dp[i][j]=max\{dp[i−di[k]][j−dj[k ...
- Oracle作业练习题
第一问 //登陆scott用户 //解锁 alter user scott account unlock; //给用户申请密码 alter user scott identified by tiger ...
- lintcode735. Replace With Greatest From Right
Given an array of integers, replace every element with the next greatest element (greatest element o ...
- 周期串 (Periodic Strings,UVa455)
#include<stdio.h> #include<string.h> int main(void) { int n,stlen,i,j; ]; while(scanf(&q ...
- 【Linux 运维】Centos7初始化网络配置
设置网络 (1)动态获取一个IP地址 #dhclient 系统自动自动获取一个IP地址#ip addr 查看获取的ip地址(2)查看网关,子网掩码 虚拟机编辑>虚拟 ...