解题思路

还是建立超级源点和超级汇点,又因为题目给出规定一个人只能修一个工件,所以建图的时候还要讲容量都设为$1$。

人的编号是$1\rightarrow n$,工件的编号是$n+1\rightarrow 2\times n$。人和超级源点连边,工件和超级汇点连边,跑一个最小费用最大流和最大费用最大流。

附上代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxnode = , maxedge = , INF = ;
inline int read() {
int x = , f = ; char c = getchar();
while (c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while (c >= '' && c <= '') {x = x* + c-''; c = getchar();}
return x * f;
}
int n, m, dis[maxnode], Depth[maxnode], head[maxnode], cnt = , s, t, Ans;
struct edge {
int nxt, u, v, w;
}ed[maxedge];
inline void addedge(int x, int y, int cap) {
ed[++cnt].nxt = head[x];
ed[cnt].v = y, ed[cnt].w = cap, ed[cnt].u = x;
head[x] = cnt;
}
inline bool BFS() {
queue<int> Q;
memset(Depth, , sizeof(Depth));
Depth[s] = , Q.push(s);
int u;
while(!Q.empty()) {
u = Q.front();
Q.pop();
for(int i=head[u]; i; i=ed[i].nxt) {
if(ed[i].w > && Depth[ed[i].v] == ) {
Depth[ed[i].v] = Depth[u] + ;
Q.push(ed[i].v);
if(ed[i].v == t) return true;
}
}
}
return false;
}
inline int Dinic(int u, int cap) {
if(u == t) return cap;
int delta;
for(int i=head[u]; i; i=ed[i].nxt) {
if(Depth[ed[i].v] == Depth[u] + && ed[i].w > ) {
delta = Dinic(ed[i].v, min(cap, ed[i].w));
if(delta > ) {
ed[i].w -= delta;
ed[i^].w += delta;
return delta;
}
}
}
return ;
}
int main() {
n = read(), m = read();
s = , t = n+m+;
for(int i=; i<=n; i++) addedge(s, i, ), addedge(i, s, );
for(int i=n+; i<=m; i++) addedge(i, t, ), addedge(t, i, );
static int x, y;
while () {
x = read(), y = read();
addedge(x, y, ), addedge(y, x, );
if(x == - && y == -) break;
}
while (BFS()) Ans += Dinic(s, INF);
if(Ans) printf("%d\n", Ans);
else printf("No Solution!\n");
if(Ans) {
for(int i=; i<=cnt; i+=) {
if(ed[i].v != s && ed[i].v != t && ed[i^].v != s && ed[i^].v != t)
if(ed[i^].w != )printf("%d %d\n", ed[i].u, ed[i].v);
}
}
return ;
}

Luogu P4014 「 网络流 24 题 」分配问题的更多相关文章

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

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

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

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

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

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

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

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

  5. Luogu P4016 「 网络流 24 题 」负载平衡问题

    吐槽题目难度,这个题建模好像比前两个都要难,但是难度评级却比第二个要低. 解题思路 依旧是考虑如何建模和建立源点汇点.每个点的货物数量到最后都一样的话肯定是等于他们的平均值.用 $num$ 数组存储原 ...

  6. Libre 6009 「网络流 24 题」软件补丁 / Luogu 2761 软件安装问题 (最短路径,位运算)

    Libre 6009 「网络流 24 题」软件补丁 / Luogu 2761 软件安装问题 (最短路径,位运算) Description T 公司发现其研制的一个软件中有 n 个错误,随即为该软件发放 ...

  7. Libre 6007 「网络流 24 题」方格取数 / Luogu 2774 方格取数问题 (网络流,最大流)

    Libre 6007 「网络流 24 题」方格取数 / Luogu 2774 方格取数问题 (网络流,最大流) Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从 ...

  8. Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流)

    Libre 6006 「网络流 24 题」试题库 / Luogu 2763 试题库问题 (网络流,最大流) Description 问题描述: 假设一个试题库中有n道试题.每道试题都标明了所属类别.同 ...

  9. Libre 6005 「网络流 24 题」最长递增子序列 / Luogu 2766 最长递增子序列问题(网络流,最大流)

    Libre 6005 「网络流 24 题」最长递增子序列 / Luogu 2766 最长递增子序列问题(网络流,最大流) Description 问题描述: 给定正整数序列x1,...,xn . (1 ...

随机推荐

  1. exception log

    except Exception as e: l = [str(i) for i in [dbid, f_mp3, e]] log_s = '||'.join(l) logging.exception ...

  2. 下载的php_yal.dll文件添加到php的ext

    下载的php_yal.dll文件添加到php的ext http://pecl.php.net/package/yaf/3.0.6/windows yaf. use_namespace=1 ;;exte ...

  3. JLabel作为展现元素时需要注意的事项

    如果没有内容,JLabel默认透明就无法作为点击区域了,所以为了让其可以响应鼠标事件需要设置 setOpaque(true) 这样就可以响应鼠标事件了 (吐槽一下,多年以前在大学做个web地图导航的网 ...

  4. android developer官网不能打开怎么办

    映射网站: http://wear.techbrood.com

  5. jsp制作登录正在加载页面.....

    1. <body style="margin:0px;"> <div id="loading"> <div class=" ...

  6. 【168】ENVI入门系列

    参考:ENVI-IDL中国的博客 [ENVI入门系列]01.ENVI产品简介与入门 [ENVI入门系列]02.自定义坐标系(北京54.西安80.2000坐标系) [ENVI入门系列]03.基于自带定位 ...

  7. codevs1183 泥泞的道路(01分数规划)

    1183 泥泞的道路  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description CS有n个小区,并且任意小区之间都有两 ...

  8. GG_Model 类库与数据库表对应建立实体类

    3.4.GG_Model 类库与数据库表对应建立实体类 我这里不教大家写代码,直接用TT模板自动生成,省去写代码的麻烦. A. 三个文件MysqlDbhelper.ttinclude .mysqlMa ...

  9. 数据结构之单链表(C实现)

    list.h #ifndef LIST_H #define LIST_H #include <iostream> #include <stdio.h> #include < ...

  10. Linux文件详解

    一.Linux文件类型分:普通文件.目录文件.链接文件.设备文件.管道文件. 1.普通文件:由ls -al显示属性时,第一个属性为 [-],例如 [-rwxrwxrwx].包括: 纯文本文件(ASCI ...