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 ...
随机推荐
- 淘宝的前端类库-KISSY
KISSY(淘宝) KISSY是淘宝的前端类库,几乎在淘宝的每个页面上都能看到它的身影. KISSY提供稳定的核心,包括 oo.dom.Event.Anim.Ajax 等:强大且易用的脚本加载器,特有 ...
- python中引号中有双引号
#/usr/bin/python import os name = "ABC" #ABC是具体的模块名,产品经理每一次给的模块名字都不一样 os.environ['name'] = ...
- IDEA创建MAVEN 无骨架WEB 项目
Idea创建maven带有骨架的web项目的时候,会缺少必要文件夹,而且会多出来一些我们不需要的东西 详见:IDEA创建Maven Web项目 所以我们也可以创建无骨架项目: 创建maven项目 不选 ...
- RxAndroid基本使用1
1,基本使用 public class MainActivity extends ActionBarActivity implements View.OnClickListener, View.OnT ...
- dubbo-admin打包和zookper安装
1 首选安装Zookper,下载zookeeper-3.5.3-beta版本,在这里我主要演示这个:下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/ ...
- Codeforces #528 Div2 F (1087F) Rock-Paper-Scissors Champion 树状数组+set
题意:n个人站成一排,初始时刻每个人手中都有一个图案,可能是石头,剪刀,布3个中的1种,之后会随机选取相邻的两个人玩石头剪刀布的游戏,输的人会离开(如果两个人图案相同,则随机选择一个人离开).执行(n ...
- Angular03 将数据添加到组件中
准备:已经搭建好angular-cli环境.知道如何创建组件 一.将一个数据添加到组件中 1 创建一个新的组件 user-item 2 将组件添加到静态模板中 3 为组件添加属性,并利用构造器赋值 4 ...
- 项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据
1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据 2 菜单数据添加 2.1 使用c ...
- ZROI2018提高day5t1
传送门 分析 我们不难将条件转换为前缀和的形式,即 pre[i]>=pre[i-1]*2,pre[i]>0,pre[k]=n. 所以我们用dp[i][j]表示考虑到第i个数且pre[i]= ...
- Google androd性能优化经典
2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...