LOJ6004 「网络流 24 题
来源:互联网 发布:号码数据库 编辑:程序博客网 时间:2024/06/05 16:45
大家都很强,可与之共勉 。
题意:
假设有来自
问是否存在方案,若存在输出一种方案。
题解:
最大流满流判是否有解。然后输出方案判哪些边被流满流。
建边方式:
我们虚拟一个原点
S 和汇点T 。
然后把单位认为是X 集合,餐桌认为是Y 集合。
S 向X 集合里面的所有元素连一条容量为其代表人数的弧,Y 集合的每一个元素向T 连一条容量为其容纳人数的弧。
X 集合中的每一个元素向Y 集合中的每一个元素连一条容量为1 的弧(我认为不要求输出方案的话是可以虚拟节点的连|X|+|Y| 条边,输出方案也可以,只是麻烦一点)。
然后最大流,如果满流就存在解
# include <bits/stdc++.h># define N 5010class Network {private : struct edge { int to, w, nxt ; edge ( ) { } edge ( int to, int w, int nxt ) : to ( to ), w ( w ), nxt ( nxt ) { } } g [60010 << 1] ; int head [N], cur [N], ecnt ; int S, T , dep [N] ; inline int dfs ( int u, int a ) { if ( u == T || ! a ) return a ; int flow = 0, v, f ; for ( int& i = cur [u] ; i ; i = g [i].nxt ) { v = g [i].to ; if ( dep [v] == dep [u] + 1 ) { f = dfs ( v, std :: min ( g [i].w, a - flow ) ) ; g [i].w -= f, g [i ^ 1].w += f ; flow += f ; if ( a == flow ) return a ; } } if ( ! flow ) dep [u] = -1 ; return flow ; } inline bool bfs ( int S, int T ) { static std :: queue < int > q ; memset ( dep, 0, sizeof ( int ) * ( T + 1 ) ) ; dep [S] = 1 ; q.push ( S ) ; while ( ! q.empty ( ) ) { int u = q.front ( ) ; q.pop ( ) ; for ( int i = head [u] ; i ; i = g [i].nxt ) { int& v = g [i].to ; if ( g [i].w && ! dep [v] ) { dep [v] = dep [u] + 1 ; q.push ( v ) ; } } } return dep [T] ; }public : Network ( ) { ecnt = 1 ; } inline void add_edge ( int u, int v, int w ) { g [++ ecnt] = edge ( v, w, head [u] ) ; head [u] = ecnt ; g [++ ecnt] = edge ( u, 0, head [v] ) ; head [v] = ecnt ; } inline void clear ( ) { ecnt = 1 ; memset ( head, 0, sizeof head ) ; } inline int dinic ( int S, int T ) { this -> S = S, this -> T = T ; static int rt = 0 ; // static ; while ( bfs ( S, T ) ) { memcpy ( cur, head, sizeof ( int ) * ( T + 1 ) ) ; rt += dfs ( S, 0x3f3f3f3f ) ; } return rt ; } void display ( int n, int m, int sum, int S, int T ) { if ( dinic ( S, T ) == sum ) { puts ( "1" ) ; } else { puts ( "0" ) ; return ; } for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = head [i] ; j ; j = g [j].nxt ) if ( g [j].w == 0 && g [j].to <= m + n ) { printf ( "%d ", g [j].to - n ) ; } puts ( "" ) ; } }} Lazer ;int main ( ) { int n, m ; int sum ( 0 ) ; scanf ( "%d%d", & n, & m ) ; const int S = n + m + 1, T = n + m + 2 ; for ( int i = 1 ; i <= n ; ++ i ) { static int x ; scanf ( "%d", & x ) ; sum += x ; Lazer.add_edge ( S, i, x ) ; } for ( int i = 1 ; i <= m ; ++ i ) { static int x ; scanf ( "%d", & x ) ; Lazer.add_edge ( i + n, T, x ) ; } for ( int i = 1 ; i <= n ; ++ i ) //Lazer.add_edge ( i, X, 0x3f3f3f3f ) ; for ( int j = 1 ; j <= m ; ++ j ) {//Lazer.add_edge ( X, i + n, 0x3f3f3f3f ) ; Lazer.add_edge ( i, j + n, 1 ) ; } Lazer.display ( n, m, sum, S, T ) ; return 0 ;}
阅读全文
0 0
- LOJ6004 「网络流 24 题
- LOJ6004 网络流24题
- loj6004「网络流 24 题」圆桌聚餐(最大流)
- 【网络流】网络流24题
- [网络流]: 网络流24题
- 网络流24题
- 网络流24题
- 网络流24题
- 网络流24题
- 「网络流 24 题」试题库
- 「网络流 24 题」试题库
- LOJ6000 「网络流 24 题
- LOJ6001 「网络流 24 题
- LOJ6002 「网络流 24 题
- LOJ6003 「网络流 24 题
- LOJ6005 「网络流 24 题
- LOJ6006「网络流 24 题
- LOJ6007 「网络流 24 题
- 人脸关键点:TCNN-Tweaked Convolutional Neural Networks
- 成为 Android 大牛的10大独门秘籍
- springboot(五):spring data jpa的使用
- AndroidStudio3.0最新 Android Profiler分析器(cpu memory network 分析器)
- 函数的调用过程,栈帧的创建与销毁
- LOJ6004 「网络流 24 题
- Spring Cloud Commons 普通抽象
- Angular练习之animations动画
- android圆形进度条
- 父类引用调用子类对象
- springboot(六):如何优雅的使用mybatis
- postgresql 不阻塞创建索引
- C++基于对象--Class without pointer
- USB设备请求