P4014 分配问题
\(\color{#0066ff}{题目描述}\)
有 \(n\) 件工作要分配给 \(n\) 个人做。第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(c_{ij}\) 。试设计一个将 \(n\) 件工作分配给 \(n\) 个人做的分配方案,使产生的总效益最大。
\(\color{#0066ff}{输入格式}\)
文件的第 \(1\) 行有 \(1\) 个正整数 \(n\),表示有 \(n\) 件工作要分配给 \(n\) 个人做。
接下来的 \(n\) 行中,每行有 \(n\) 个整数 \(c_{ij}\) ,表示第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(c_{ij}\) 。
\(\color{#0066ff}{输出格式}\)
两行分别输出最小总效益和最大总效益。
\(\color{#0066ff}{输入样例}\)
5
2 2 2 1 2
2 3 1 2 4
2 0 1 1 1
2 3 4 3 3
3 2 1 2 1
\(\color{#0066ff}{输出样例}\)
5
14
\(\color{#0066ff}{数据范围与提示}\)
none
\(\color{#0066ff}{题解}\)
裸的费用流。。。
好像可以二分图最大带权匹配。。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#define _ 0
#define LL long long
inline LL in() {
LL x = 0, f = 1; char ch;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
return x * f;
}
int n, m, s, t;
const int maxn = 105050;
const int inf = 0x7fffffff;
struct node {
int to, dis, can;
node *nxt, *rev;
node(int to = 0, int dis = 0, int can = 0, node *nxt = NULL):to(to), dis(dis), can(can), nxt(nxt) {}
void *operator new (size_t) {
static node *S = NULL, *T = NULL;
return (S == T)&&(T = (S = new node[1024]) + 1024) , S++;
}
};
typedef node* nod;
nod head[maxn], road[maxn];
int dis[maxn], change[maxn];
bool vis[maxn];
int c[120][120];
std::queue<int> q;
inline void add(int from, int to, int dis, int can) {
nod o = new node(to, dis, can, head[from]);
head[from] = o;
}
inline void link(int from, int to, int dis, int can) {
add(from, to, dis, can);
add(to, from, -dis, 0);
head[from]->rev = head[to];
head[to]->rev = head[from];
}
inline bool spfa1() {
for(int i = s; i <= t; i++) dis[i] = inf, change[i] = inf;
dis[s] = 0;
q.push(s);
while(!q.empty()) {
int tp = q.front(); q.pop();
vis[tp] = false;
for(nod i = head[tp]; i; i = i->nxt) {
if(dis[i->to] > dis[tp] + i->dis && i->can > 0) {
dis[i->to] = dis[tp] + i->dis;
road[i->to] = i;
change[i->to] = std::min(change[tp], i->can);
if(!vis[i->to]) vis[i->to] = true, q.push(i->to);
}
}
}
return change[t] != inf;
}
inline bool spfa2() {
for(int i = s; i <= t; i++) dis[i] = -inf, change[i] = inf;
dis[s] = 0;
q.push(s);
while(!q.empty()) {
int tp = q.front(); q.pop();
vis[tp] = false;
for(nod i = head[tp]; i; i = i->nxt) {
if(dis[i->to] < dis[tp] + i->dis && i->can > 0) {
dis[i->to] = dis[tp] + i->dis;
road[i->to] = i;
change[i->to] = std::min(change[tp], i->can);
if(!vis[i->to]) vis[i->to] = true, q.push(i->to);
}
}
}
return change[t] != inf;
}
inline void mcmf1()
{
int cost = 0;
while(spfa1()) {
cost += change[t] * dis[t];
for(int i = t; i != s; i = road[i]->rev->to) {
road[i]->can -= change[t];
road[i]->rev->can += change[t];
}
}
printf("%d\n", cost);
}
inline void mcmf2()
{
int cost = 0;
while(spfa2()) {
cost += change[t] * dis[t];
for(int i = t; i != s; i = road[i]->rev->to) {
road[i]->can -= change[t];
road[i]->rev->can += change[t];
}
}
printf("%d", cost);
}
int main() {
n = in();
s = 0, t = (n << 1) + 1;
for(int i = 1; i <= n; i++) {
link(s, i, 0 ,1);
link(i + n, t, 0, 1);
for(int j = 1; j <= n; j++)
link(i, j + n, c[i][j] = in(), 1);
}
mcmf1();
for(int i = s; i <= t; i++) head[i] = NULL;
for(int i = 1; i <= n; i++) {
link(s, i, 0 ,1);
link(i + n, t, 0, 1);
for(int j = 1; j <= n; j++)
link(i, j + n, c[i][j], 1);
}
mcmf2();
return 0;
}
P4014 分配问题的更多相关文章
- 洛谷P4014 分配问题【最小/大费用流】题解+AC代码
洛谷P4014 分配问题[最小/大费用流]题解+AC代码 题目描述 有 n 件工作要分配给 n 个人做.第 i 个人做第 j 件工作产生的效益为c ij. 试设计一个将 n 件工作分配给 n 个人做的 ...
- 洛谷——P4014 分配问题
P4014 分配问题 题目描述 有 nn 件工作要分配给 nn 个人做.第 ii 个人做第 jj 件工作产生的效益为 c_{ij}cij .试设计一个将 nn 件工作分配给 nn 个人做的分配方案, ...
- 洛谷P4014分配问题——网络流24题
题目:https://www.luogu.org/problemnew/show/P4014 最大/小费用最大流裸题. 代码如下: #include<iostream> #include& ...
- P4014 分配问题 网络流
题目描述 有 nn 件工作要分配给 nn 个人做.第 ii 个人做第 jj 件工作产生的效益为 c_{ij}cij .试设计一个将 nn 件工作分配给 nn个人做的分配方案,使产生的总效益最大. 输 ...
- 洛谷P4014 分配问题(费用流)
传送门 可以把原图看做一个二分图,人在左边,任务在右边,求一个带权的最大和最小完美匹配 然而我并不会二分图做法,所以只好直接用费用流套进去,求一个最小费用最大流和最大费用最大流即可 //minamot ...
- 洛谷 P4014 分配问题 【最小费用最大流+最大费用最大流】
其实KM更快--但是这道题不卡,所以用了简单粗暴的费用流,建图非常简单,s向所有人连流量为1费用为0的边来限制流量,所有工作向t连流量为1费用为0的边,然后对应的人和工作连(i,j,1,cij),跑一 ...
- 洛谷P4014 分配问题(费用流)
题目描述 有 nn 件工作要分配给 nn 个人做.第 ii 个人做第 jj 件工作产生的效益为 c_{ij}cij .试设计一个将 nn 件工作分配给 nn 个人做的分配方案,使产生的总效益最大. ...
- luogu P4014 分配问题
简单的费用流问题,每个人对每个任务连边,每个任务对汇点连,源点对每个人连,最大费用取反即可 #include<bits/stdc++.h> using namespace std; #de ...
- 洛谷 P4016负载平衡问题【费用流】题解+AC代码
洛谷 P4016负载平衡问题 P4014 分配问题[费用流]题解+AC代码 负载平衡问题 题目描述 GG 公司有n个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n ...
随机推荐
- Oracle 11g R2(11.2.0.4) RAC 数据文件路径错误解决--ORA-01157 ORA-01110: 数据文件
Oracle 11g R2(11.2.0.1) RAC 数据文件路径错误解决--ORA-01157 ORA-01110: 数据文件 oracle 11g R2(11.2.0.4) rac--scan ...
- ghld data format
%CTF: 1.00%FileType: PROF strp "VelocityProfile"%PROFSpec: 1.00 2006 00 00%Manufacturer: C ...
- Vue指令学习
# new Vue({ vue所有的数据都是放到data里面的 # data:{ vue对象的数据 # a:1,对象 # b:[] , # } # methods:{vue对象的方法 # dosomt ...
- 如何修改AWR的retention,interval
检查AWR当前设置: SQL> select * from dba_hist_wr_control; DBID SNAP_INTERVAL RETENTION TOPNSQL --------- ...
- spring配置c3p0连接池
- Docker的Gitlab镜像的使用
Gitlab是一款非常强大的开源源码管理系统.它支持基于Git的源码管理.代码评审.issue跟踪.活动管理.wiki页面,持续集成和测试等功能.基于Gitlab,用户可以自己搭建一套类似Github ...
- How to watch property in attrs of directive
Q: I have a controller that has a counter that changes from time to time. That counter is tied to an ...
- Android禁止程序自动旋转的配置
在想要禁止的Activity中加入 android:screenOrientation="portrait" 属性,其中,portrait是竖屏,landscape是横屏
- HDU 4899 Hero meet devil (状压DP, DP预处理)
题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...
- uniqid() 函数 和 microtime()函数
uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.语法 uniqid(prefix,more_entropy) 参数 描述prefix 可选.为 ID 规定前缀.如果 ...