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】 分配问题的更多相关文章

  1. LG2770/LOJ6122 航空路线问题 费用流 网络流24题

    问题描述 LG2770 LOG6122 题解 教训:关掉流同步之后就不要用其他输入输出方式了. 拆点. 两个拆点之间连\((1,1)\),其他连\((1,0)\) \(\mathrm{Code}\) ...

  2. Libre 6012 「网络流 24 题」分配问题 (网络流,费用流)

    Libre 6012 「网络流 24 题」分配问题 (网络流,费用流) Description 有n件工作要分配给n个人做.第i个人做第j件工作产生的效益为\(c_{ij}\).试设计一个将n件工作分 ...

  3. cogs_14_搭配飞行员_(二分图匹配+最大流,网络流24题#01)

    描述 http://cojs.tk/cogs/problem/problem.php?pid=14 有一些正飞行员和副飞行员,给出每个正飞行员可以和哪些副飞行员一起飞.一架飞机上必须一正一副,求最多多 ...

  4. 2018.10.14 loj#6012. 「网络流 24 题」分配问题(费用流)

    传送门 费用流水题. 依然是照着题意模拟建边就行了. 为了练板子又重新写了一遍费用流. 代码: #include<bits/stdc++.h> #define N 305 #define ...

  5. 【刷题】LOJ 6012 「网络流 24 题」分配问题

    题目描述 有 \(n\) 件工作要分配给 \(n\) 个人做.第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(c_{ij}\) ​​.试设计一个将 \(n\) 件工作分配给 \(n\) ...

  6. 【PowerOJ1753&网络流24题】分配问题(KM)

    题意: 思路:费用流可做 最好的算法是KM板子 #include<bits/stdc++.h> using namespace std; typedef long long ll; typ ...

  7. Luogu P4014 「 网络流 24 题 」分配问题

    解题思路 还是建立超级源点和超级汇点,又因为题目给出规定一个人只能修一个工件,所以建图的时候还要讲容量都设为$1$. 人的编号是$1\rightarrow n$,工件的编号是$n+1\rightarr ...

  8. 【LOJ】 #6012. 「网络流 24 题」分配问题

    题解 又写了一遍KM算法,这题刚好是把最大最小KM拼在一起写的,感觉比较有记录价值 感觉KM始终不熟啊QAQ 算法流程大抵如下,原理就是每次我们通过减少最少的匹配量达成最大匹配,所以获得的一定是最大价 ...

  9. LG2766 最长不下降子序列问题 最大流 网络流24题

    问题描述 LG2766 题解 \(\mathrm{Subtask 1}\) 一个求最长不下降子序列的问题,发现\(n \le 500\),直接\(O(n^2)\)暴力DP即可. \(\mathrm{S ...

  10. 【网络流24题】最长k可重线段集(费用流)

    [网络流24题]最长k可重线段集(费用流) 题面 Cogs的数据有问题 Loj 洛谷 题解 这道题和最长k可重区间集没有区别 只不过费用额外计算一下 但是,还是有一点要注意的地方 这里可以是一条垂直的 ...

随机推荐

  1. 从零开始的Python学习 知识补充sorted

    sorted()方法 sorted()可用于任何一个可迭代对象. 原型为sorted(iterable, cmp=None, key=None, reverse=False) iterable:一个可 ...

  2. 微信小程序开发调试技巧

    1.  查看线上小程序console a.  先打开开发小程序console b.  再打开线上小程序,此时可以查看console

  3. Python之并发编程-多线程

    目录 一.threading模块介绍二.使用说明三.进一步介绍(守护线程,锁(互斥锁.递归锁),信号量,队列,event,condition,定时器) 1.守护线程 2.锁(互斥锁.递归锁) 3.信号 ...

  4. oracle删除死锁进程

    在命令行下运行: select SID,SERIAL# from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID; alte ...

  5. Thirteenth scrum meeting 2015/11/11

    发布bug整理集结: 手机用户体验优化优化: (1)主界面和课程界面的字体规格以及界面结构不同 (2)课程图片的大小格式不统一,造成美观下降 ( 3 )按钮的位置不美观 平板用户体验: (1)Tab键 ...

  6. vim相关命令单独记载

    1. 无敌的可扩展性 1.1 可扩展性给了软件强大的生命 曾几何时,Windows用户对软件的可扩展性没有概念,他们只能对他们使用的软件进行非常有限的定制.扩展软件的权利保留在软件开发者手中.软件的使 ...

  7. Spring笔记①--helloworld

    Spring Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架,它主要是为了解决企业应用开发的复杂性而诞生的: 目的:解决企业应用开发的复杂性 功能:使用基本的Javabean ...

  8. Math 类的使用(一小部分)

    package com.Date.Math; /* Math 数学类, 主要是提供了很多的数学公式. abs(double a) 获取绝对值 ceil(double a) 向上取整 floor(dou ...

  9. 2018软工实践—Beta冲刺(1)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Beta 冲鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调组内工作 调试服务器性能 展示GitHub当日代码/文档签入记录(组内 ...

  10. ResourceBundle类读取properties文件

    1.Properties与ResourceBundle类都可以读取属性文件key/value的键值对 2.ResourceBundle类主要用来解决国际化和本地化问题,国际化时properties文件 ...