UVALIVE 4819 最大流
来源:互联网 发布:淘宝怎么进去卖家中心 编辑:程序博客网 时间:2024/03/29 02:11
题意:有N场比赛,每场比赛需要一定数量的题目数,现在有M个题目,每个题目只能提供给特定的几场比赛,并且一次只能在一场比赛中出现。
问最多可以举办多少场比赛。
思路:因为N = 15 , 所以直接二进制枚举举办的比赛的情况,然后对于每种情况建图,
S - >题目,流量1
题目 ->比赛,流量1
比赛->T,流量为该场比赛需要的题目数。
每次都跑最大流,看是否等于所需的题目数,然后更新答案即可。
#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <iostream>#include <algorithm>#define Max 2505#define FI first#define SE second#define ll long long#define PI acos(-1.0)#define inf 0x3fffffff#define LL(x) ( x << 1 )#define bug puts("here")#define PII pair<int,int>#define RR(x) ( x << 1 | 1 )#define mp(a,b) make_pair(a,b)#define mem(a,b) memset(a,b,sizeof(a))#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )using namespace std;#define N 222#define M 5555struct kdq { int e , next , l ;} ed[M] ;int head[N] , num ;void init() { mem(head ,-1) ; num = 0 ;}void add(int s ,int e ,int l) { ed[num].e = e ; ed[num].next = head[s] ; ed[num].l = l ; head[s] = num ++ ; ed[num].e = s ; ed[num].next = head[e] ; ed[num].l = 0 ; head[e] = num ++ ;}map<string ,int>MM;int n , m ;int a[N] ;string x ;int fk[111][111] ;char now[M] ;int S , T ;int deep[111] ;int qe[111111] ;int dinic_bfs() { mem(deep , -1) ; deep[S] = 0 ; int h = 0 , t = 0 ; qe[h ++ ] = S ; while(h > t) { int tp = qe[t ++ ] ; for (int i = head[tp] ; ~i ; i = ed[i].next ) { int e = ed[i].e ; int l = ed[i].l ; if(l > 0 && deep[e] == -1) { deep[e] = deep[tp] + 1 ; qe[h ++ ] = e ; } } } return deep[T] != -1 ;}int dinic_dfs(int now , int f) { if(now == T)return f ; int flow = 0 ; for (int i = head[now] ; ~i ; i = ed[i].next ) { int e = ed[i].e ; int l = ed[i].l ; if(deep[e] == deep[now] + 1 && l > 0 && (f - flow) > 0) { int mm = min(l , f - flow) ; int nn = dinic_dfs(e , mm) ; flow += nn ; ed[i].l -= nn ; ed[i ^ 1].l += nn ; } } if(!flow)deep[now] = -2 ; return flow ;}int dinic() { int flow = 0 ; while(dinic_bfs()) { flow += dinic_dfs(S , inf) ; } return flow ;}int main() { int ca = 0 ; while(scanf("%d%d",&n,&m) , (n + m)) { S = 0 , T = n + m + 1 ; init() ; MM.clear() ; mem(fk ,0) ; for (int i = 1 ; i <= n ; i ++ ) { cin >> x ; MM[x] = i ; scanf("%d",&a[i]) ; } string st ; st.clear() ; gets(now) ; for (int i = 1 ; i <= m ; i ++ ) { gets(now) ; st.clear() ; int l = strlen(now) ; if(l == 0)continue ; for (int j = 0 ; j < l ; j ++ ) { if(now[j] == ' ') { if(st.size() == 0)continue ; fk[MM[st]][i] = 1 ; st.clear() ; continue ; } st += now[j] ; } if(st.size()) { fk[MM[st]][i] = 1 ; } } int ans = 0 ; for (int i = 0 ; i < (1 << n) ; i ++ ) { init() ; int nk = 0 ; int sum = 0 ; for (int j = 1 ; j <= m ; j ++ )add(S , j , 1) ; for (int j = 0 ; j < n ; j ++ ) { if(i & (1 << j)) { for (int k = 1 ; k <= m ; k ++ ) { if(fk[j + 1][k]) { add(k , j + 1 + m , 1) ; } } nk ++ ; sum += a[j + 1] ; add(j + 1 + m , T , a[j + 1]) ; } } int fff = dinic() ; if(fff == sum)ans = max(ans , nk) ; } printf("Case #%d: %d\n",++ ca ,ans) ; } return 0 ;}
- UVALIVE 4819 最大流
- UVALive 6557 Stampede!(最大流)
- UVALive 4957 Fake scoreboard 最大流
- Uvalive 4865 Data Recovery 最大流
- uvalive 2957 Bring Them There(最大流)
- UVALive 6851The Programmers(最大流)
- UVALive 3645 Objective: Berlin(最大流)
- 【UVALive 3231】Fair Share 最大流
- UVALive 2531 The K-League 最大流
- uvalive 最大子矩阵
- UVALive 2957 Bring Them There(拆点+最大流)
- UVALive 5220 Internet Bandwidth(最大流,模板题)
- UVALive 2957Bring Them There (最大流+拆点)
- UVALive - 3268 Jamie's Contact Groups(二分+最大流)
- UVALive - 2957 Bring Them There(最大流 图论建模)
- UVALive - 2531 The K-League(最大流+枚举)
- UVALive - 3972 March of the Penguins(最大流+枚举)
- UVALive 7037 (最大密度子图 网络流)
- 数字彩色电视摄像机结构
- Struts 2 Tutorial: Create Struts 2 Application in Eclipse
- HDU 1115 求多边形重心
- 《30天自制操作系统》读书笔记Day17
- Directx 3D编程实例:随机绘制的立体图案旋转
- UVALIVE 4819 最大流
- 源程序就是设计 ---By Jack Reeves
- python练习--天气信息抓取(1)
- 链接分析算法之:HITS算法
- Android 漫游之路------ListView入门、Layout的Inflater创建View对象、常用数据适配器
- Flash Builder 入门
- 利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
- Computing Tangent Space Basis Vectors for an Arbitrary Mesh
- OpenCV学习--saturate_cast防止数据溢出