UVALIVE 4819 最大流
题意:有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 5555
struct 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 最大流的更多相关文章
- POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...
- uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。
/** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...
- BZOJ 4819 Luogu P3705 [SDOI2017]新生舞会 (最大费用最大流、二分、分数规划)
现在怎么做的题都这么水了.. 题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=4819 (luogu) https://ww ...
- UVALive - 6266 Admiral 费用流
UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...
- UVALive 6887 Book Club 最大流解最大匹配
题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- UVALive - 6571 It Can Be Arranged 最大流
题目链接: http://acm.hust.edu.cn/vjudge/problem/48415 It Can Be Arranged Time Limit: 3000MS 问题描述 Every y ...
- Uvalive 4865 Data Recovery 最大流
题意就是 给一个50 * 50的矩阵 然后给出每行每列元素的和 和一个初始矩阵 矩阵中有些是未知,有些是已知 然后我们求目标矩阵就是把能确定的元素的值求出来,实在不能确定的就置为-1 所有矩阵元素的值 ...
- UVALive 3645 Objective: Berlin(最大流 :时序模型)
题意:已知n(n <= 150)个城市和m(m <= 5000)个航班,每个航班有出发地.到达地.乘坐人数.起飞时间和降落时间(时间用时和分表示),求从一个指定城市出发,去往另一个指定城市 ...
- 【Uvalive 2531】 The K-League (最大流-类似公平分配问题)
[题意] 有n个队伍进行比赛,每场比赛,恰好有一支队伍取胜.一支队伍败.每个队伍需要打的比赛场数相同,给你每个队伍目前已经赢得场数和输得场数,再给你一个矩阵,第 i 行第 j 列 表示队伍 i 和队伍 ...
随机推荐
- 【POJ1733】【带标记并查集】Parity game
Description Now and then you play the following game with your friend. Your friend writes down a seq ...
- 『重构--改善既有代码的设计』读书笔记----Move Field
在类与类之间搬移状态和行为,是重构过程中必不可少的步骤.很有可能在你现在觉得正常的类,等你到了下个礼拜你就会觉得不合适.或者你在下个礼拜创建了一个新的类并且你需要讲现在类的部分字段和行为移动到这个新类 ...
- SQL2012之FileTable与C#的联合应用
关于FileTable是什么,请猛击如下链接:http://technet.microsoft.com/zh-cn/library/ff929144(v=SQL.110).aspx:如您已知道,请跳过 ...
- [翻译]jQuery十周年-John Resig
10th Anniversary of jQuery Today marks the 10th anniversary of the release of jQuery...[原文] 今天是jQuer ...
- dedecms 文章排列方式
orderby='sortrank' 文档排序方式orderby='hot' 或 orderby='click' 表示按点击数排列orderby='sortrank' 或 orderby='pubda ...
- JS设置Cookie,及COOKIE的限制
在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name ...
- jsp注释方式
1,HTML的注释方法 <!--...add your comments here...--> 说明:使用该注释方法,其中的注释内容在客户端浏览中是看不见的.但是查看源代码时,客户是可以看 ...
- FASTMM,FASTCODE,FASTMOVE请移步
http://blog.csdn.net/akof1314/article/details/6524767
- java分页查询
很多数据库自身都提供了分页机制,如SQL Server中提供的top关键字,MySQL数据库中提供的limit关键字,它们都可以设置数据返回的记录数. 通过各种数据库的分页机制实现分页查询,其优点是减 ...
- Solr4.8.0源码分析(21)之SolrCloud的Recovery策略(二)
Solr4.8.0源码分析(21)之SolrCloud的Recovery策略(二) 题记: 前文<Solr4.8.0源码分析(20)之SolrCloud的Recovery策略(一)>中提 ...