【费用流】【网络流24题】【P4014】 分配问题
Description
有 \(n\) 件工作要分配给 \(n\) 个人做。第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(C_{i,j}\) 。试设计一个将 \(n\) 件工作分配给 \(n\) 个人做的分配方案,使产生的总效益最大。
Input
文件的第 \(1\) 行有 \(1\) 个正整数 \(n\),表示有 \(n\) 件工作要分配给 \(n\) 个人做。
接下来的 \(n\) 行中,每行有 \(n\) 个整数 \(C_{i,j}\),表示第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(C_{ij}\)。
Output
两行分别输出最小总效益和最大总效益。
Hint
\(1~\leq~n~\leq~100\)
Solution
先考虑最小收益,由于必须所有的工作都被分配,所以这个限制可以转化为最大流,由于是最小费用,所以可以转化成最小费用最大流。
将人和工作之间连边,容量为 \(1\),费用为效益。建立超级源点超级汇点,源点连向人,容量为 \(1\),费用为 \(0\)。工作连向汇点,容量为 \(1\),费用为 \(0\)。这样保证了一个任务选且被选一次,同时费用即为收益。
考虑最大收益:将所有费用取相反数,求出答案再取相反即可。
Code
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define ci const int
#define cl const long long
typedef long long int ll;
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
int top=0;
do {OPT::buf[++top] = static_cast<char>(x % 10 + '0');} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 210;
const int INF = 0x3f3f3f3f;
struct Edge {
int from, to, flow, fee;
Edge *nxt, *bk;
};
Edge *hd[maxn], *pre[maxn];
inline void cont(Edge *u, Edge *v, int from, int to, int fl, int fe) {
u->from = from; u->to = to; u->flow = fl; u->fee = fe; u->bk = v;
u->nxt = hd[from]; hd[from] = u;
}
inline void conet(int from, int to, int fl, int fe) {
Edge *u = new Edge, *v = new Edge;
cont(u, v, from, to, fl, fe); cont(v, u, to, from, 0, -fe);
}
int n, s, t, ans;
int cost[maxn], maxflw[maxn], MU[maxn][maxn];
bool inq[maxn];
std::queue<int>Q;
bool SPFA();
void argu();
int main() {
freopen("1.in", "r", stdin);
qr(n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
qr(MU[i][j]); conet(i, j + n, 1, MU[i][j]);
}
}
s = (n << 1) | 1; t = (n << 1) + 2;
for (int i = 1; i <= n; ++i) conet(s, i, 1, 0);
for (int i = n + 1; i < s; ++i) conet(i, t, 1, 0);
ans = 0;
while (SPFA()) argu();
qw(ans, '\n', true);
memset(hd, 0, sizeof hd);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) conet(i, j + n, 1, -MU[i][j]);
}
for (int i = 1; i <= n; ++i) conet(s, i, 1, 0);
for (int i = n + 1; i < s; ++i) conet(i, t, 1, 0);
ans = 0;
while (SPFA()) argu();
qw(-ans, '\n', true);
return 0;
}
bool SPFA() {
memset(cost, 0x3f, sizeof cost);
memset(inq, 0, sizeof inq);
memset(pre, 0, sizeof pre);
memset(maxflw, 0, sizeof maxflw);
cost[s] = 0; Q.push(s); maxflw[s] = INF;
while (!Q.empty()) {
int h = Q.front(); Q.pop(); inq[h] = false;
if (!maxflw[h]) continue;
for (Edge *e = hd[h]; e; e = e->nxt) if (e->flow > 0) {
int to = e->to;
if (cost[to] > (cost[h] + e->fee)) {
cost[to] = cost[h] + e->fee;
maxflw[to] = std::min(maxflw[h], e->flow);
if (!inq[to]) Q.push(to);
inq[to] = true; pre[to] = e;
}
}
}
return cost[t] != INF;
}
void argu() {
for (Edge *e = pre[t]; e; e = pre[e->from]) {
e->flow -= maxflw[t];
e->bk->flow += maxflw[t];
}
ans += maxflw[t] * cost[t];
}
【费用流】【网络流24题】【P4014】 分配问题的更多相关文章
- LG2770/LOJ6122 航空路线问题 费用流 网络流24题
问题描述 LG2770 LOG6122 题解 教训:关掉流同步之后就不要用其他输入输出方式了. 拆点. 两个拆点之间连\((1,1)\),其他连\((1,0)\) \(\mathrm{Code}\) ...
- Libre 6012 「网络流 24 题」分配问题 (网络流,费用流)
Libre 6012 「网络流 24 题」分配问题 (网络流,费用流) Description 有n件工作要分配给n个人做.第i个人做第j件工作产生的效益为\(c_{ij}\).试设计一个将n件工作分 ...
- cogs_14_搭配飞行员_(二分图匹配+最大流,网络流24题#01)
描述 http://cojs.tk/cogs/problem/problem.php?pid=14 有一些正飞行员和副飞行员,给出每个正飞行员可以和哪些副飞行员一起飞.一架飞机上必须一正一副,求最多多 ...
- 2018.10.14 loj#6012. 「网络流 24 题」分配问题(费用流)
传送门 费用流水题. 依然是照着题意模拟建边就行了. 为了练板子又重新写了一遍费用流. 代码: #include<bits/stdc++.h> #define N 305 #define ...
- 【刷题】LOJ 6012 「网络流 24 题」分配问题
题目描述 有 \(n\) 件工作要分配给 \(n\) 个人做.第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(c_{ij}\) .试设计一个将 \(n\) 件工作分配给 \(n\) ...
- 【PowerOJ1753&网络流24题】分配问题(KM)
题意: 思路:费用流可做 最好的算法是KM板子 #include<bits/stdc++.h> using namespace std; typedef long long ll; typ ...
- Luogu P4014 「 网络流 24 题 」分配问题
解题思路 还是建立超级源点和超级汇点,又因为题目给出规定一个人只能修一个工件,所以建图的时候还要讲容量都设为$1$. 人的编号是$1\rightarrow n$,工件的编号是$n+1\rightarr ...
- 【LOJ】 #6012. 「网络流 24 题」分配问题
题解 又写了一遍KM算法,这题刚好是把最大最小KM拼在一起写的,感觉比较有记录价值 感觉KM始终不熟啊QAQ 算法流程大抵如下,原理就是每次我们通过减少最少的匹配量达成最大匹配,所以获得的一定是最大价 ...
- LG2766 最长不下降子序列问题 最大流 网络流24题
问题描述 LG2766 题解 \(\mathrm{Subtask 1}\) 一个求最长不下降子序列的问题,发现\(n \le 500\),直接\(O(n^2)\)暴力DP即可. \(\mathrm{S ...
- 【网络流24题】最长k可重线段集(费用流)
[网络流24题]最长k可重线段集(费用流) 题面 Cogs的数据有问题 Loj 洛谷 题解 这道题和最长k可重区间集没有区别 只不过费用额外计算一下 但是,还是有一点要注意的地方 这里可以是一条垂直的 ...
随机推荐
- mv命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/piaozhe116/p/6084214.html mv命令是move的缩写,可以用来移动文件或者将文件改名(move ...
- “秒杀”问题的数据库和SQL设计【转载】
“秒杀”问题的数据库和SQL设计 APRIL 21ST, 2015 问题的来源 完全不考虑一致性的方案 表结构 方案 存在的问题 保证单用户不会重复购买 解决超卖问题 方案 优化 提高性能了 鱼与熊掌 ...
- Scrum Meeting 11.06
成员 今日任务 明日计划 用时 徐越 学习ListView+simpleAdapter,actionBar.阅读并修改前端代码 继续修改前端代码.完善数据库 4h 赵庶宏 构建后端数据库,进行完善 ...
- Alpha阶段产品功能说明
先展示一下我们的功能流程图吧~ 一.学生用户 1. 学生登陆注册 BuaaClubs是北航所有在校生都可以注册登录的网站. 登陆界面是这样哒~ 2. 浏览报名活动 同学们可以在这个网站上查看所有社团发 ...
- Daily Scrumming 2015.10.23(Day 4)
今明两天任务表 Member Today’s Task Tomorrow’s Task 江昊 继续学习rails ActiveRecord 数据库迁移 域名备案申请 学习rails router配置与 ...
- 在Windows下制作静态库和动态库
一:静态库的创建 VC++6.0中new一个的为win32 static library工程,之后有二个选项.根据需求选吧. 具体的类或者函数的添加过程和标准的工程一样,直接创建新的类或者添加新 的. ...
- 《Spring2之站立会议1》
<Spring2之站立会议1> 昨天,查找了关于建立起服务器和客户端两端的连接的资料,并学习了如何写把两者建立起来的代码: 今天,模仿着相关资料,分别写了客户端和服务器两端的代码: 遇到的 ...
- 【CSAPP笔记】13. 链接
下面就要进入本书的第二部分--在系统上运行程序.书的第一部分,主要是研究单个应用程序,关注的是数据类型.机器指令.程序性能.存储器系统等话题.在书的第二部分,我们继续对计算机系统的探索.现代操作系统与 ...
- 微服务注册与发现 —— eureka
基础概念 在微服务系统中,服务的注册和发现是第一步,常用的有: Eureka:https://github.com/Netflix/eureka Zookeeper:https://zookeeper ...
- PROFIBUS-DP现场总线的结构及应用
PROFIBUS的最大优点在于具有稳定的国际标准EN50170作保证,并经实际应用验证具有普遍性.目前已广泛应用于制造业自动化.流程工业自动化和楼宇.交通电力等领域. PROFIBUS由3个兼容部分组 ...