zoukankan      html  css  js  c++  java
  • BNU 33693——Problemsetting——————【枚举+最大流】

    Problemsetting

    Time Limit: 5000ms
    Memory Limit: 131072KB
    64-bit integer IO format: %lld      Java class name: Main
    Type: 
    None
     
     

    [PDF Link]

    It's well-known that different programming contests require different kind of problems. For example, maximal array size for TopCoder problem is only 50, and you definitely can not give a Suffix Tree problem to IOI because children will not have a chance to solve it (except of some touristic-inclined ones). Thus not every problem is acceptable for every contest.

    You are preparing problemsets for N different contests. These contests require different number of problems, depending of type. For example, ACM ICPC style problemset usually has 10 problems, TopCoder SRM - 5 and so on.

    Luckily you have already prepared M different problems. For each problem you have determined a set of contests you can give that problem to. Also you know the required number of problems for each contest.

    Find out the maximal number of different contests for which you can simultaneously compose complete problemsets from the given set of problems. All problems in the problemsets must be unique, i.e. no problem can be used twice in different problemsets.

     

    Input

    The input file contains several test cases.

    The first line of each test case contains 2 integers N and M (1 < N < 15, 0 < M < 50) - the number of different contests and the number of prepared problems. Each of the followingN lines contains the name of contest, followed by the required number of problems for that contest. The name of a contest consists of lower - and uppercase Latin letters and/or digits, is not empty and does not exceed 100 characters. Contest names are case-sensitive. It's guaranteed that all contest names will be pairwise different. The required number of problems does not exceed 100.

    Each of the following M lines contains a (possibly empty) list of acceptable contest names for each problem, separated by a single space. It's guaranteed that all contest names will be correct (i.e., noted in the previous section of the current test case) and unique.

    The line containing two zeroes indicates the end of the input file.

    For each test case print an answer for that case on a new line, as shown in the sample output.

     

    Sample Input

    4 5 
    IOI 3 
    IPSC 2 
    TopCoder 2 
    SEERC 10 
    IOI 
    IPSC TopCoder 
    IOI IPSC 
    IOI IPSC 
    TopCoder SEERC 
    1 1 
    SampleContest 1 
    SampleContest 
    0 0
    

    Sample Output

    Case #1: 2 
    Case #2: 1
    

    Source

     
     
    题目大意:给你n个比赛,m个题目。每个题目可以给某几个比赛用,但是只能用一次。问你在满足每个比赛需要的题目数量的条件下,最多可以办多少场比赛。
     
    解题思路:用网络流可以做。我们枚举可以举办的比赛,然后从源点向所枚举的比赛加边,容量为该比赛可以选多少个题目。如果最后的最大流大于等于所枚举比赛所需题目总数,说明所枚举的比赛可以举办。更新出来最大的比赛数目即可。 构图:除了从源点向比赛加边,还需从比赛向所能使用的题目加边,容量为1,还要从题目向汇点加边,容量为1.
     
    吐槽一下,这个题目的输入好像空格不均匀。
     
     
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<math.h>
    #include<string>
    #include<iostream>
    #include<queue>
    #include<stack>
    #include<map>
    #include<vector>
    #include<set>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define mid (L+R)/2
    #define lson rt*2,L,mid
    #define rson rt*2+1,mid+1,R
    const int mod = 1e9+7;
    const int maxn = 100;
    const int INF = 0x3f3f3f3f;
    
    int Map[55][55];
    int need[55];
    struct Edge{
        int from, to, next, cap, flow;
        Edge(){}
        Edge(int _from, int _to, int _cap, int _flow):from(_from), to(_to), cap(_cap),flow(_flow){}
    };
    int contest_table[20];
    vector<Edge>edges;
    vector<int>G[maxn];
    void AddEdge(int from, int to, int cap){
        edges.push_back(Edge(from, to, cap, 0));
        edges.push_back(Edge(to, from, 0, 0));
        int m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
    }
    int capacity[maxn];
    struct Dinic{
        int s, t; //
    
        bool vis[maxn];
        int d[maxn];
        int cur[maxn];
        bool BFS(){
            memset(vis,0,sizeof(vis));
            queue<int>Q;
            Q.push(s);
            d[s] = 0;
            vis[s] = 1;
            while(!Q.empty()){
                int x = Q.front(); Q.pop();
                for(int i = 0; i < G[x].size(); ++i){
                    Edge &e = edges[G[x][i]];
                    if(!vis[e.to] && e.cap > e.flow){
                        vis[e.to] = 1;
                        d[e.to] = d[x] + 1;
                        Q.push(e.to);
                    }
                }
            }
            return vis[t];
        }
        int DFS(int x,int a){
            if(x == t || a == 0) return a;
            int flow = 0, f;
            for(int &i = cur[x]; i < G[x].size(); i++){
                Edge &e = edges[G[x][i]];
                if(d[x] + 1 == d[e.to]&&(f = DFS(e.to, min(a,e.cap - e.flow))) > 0){
                    e.flow += f;
                    edges[G[x][i]^1].flow -= f;
                    flow += f;
                    a -= f;
                    if(a == 0) break;
                }
            }
            return flow;
        }
        int Maxflow(int s,int t){
            this->s = s; this->t = t;
            int flow = 0;
            while(BFS()){
                memset(cur,0,sizeof(cur));
                flow += DFS(s, INF);
            }
            return flow;
        }
    };
    
    int n, m;
    int constructG(int enumc){
        for(int i = 0; i <= n+m+10; i++){
            G[i].clear();
        }
        edges.clear();
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(Map[i][j]){
                    AddEdge(i, n+j, 1);
                }
            }
        }
        for(int  i = 1; i <= m; i++){
            AddEdge(n+i,n+m+1,1);
        }
        int ret = 0;
        for(int i = 1; i <= n; i++){
            if(enumc&1){
                AddEdge(0, i, capacity[i]);
                ret += need[i];
            }
            enumc /= 2;
        }
        return ret;
    }
    int main(){
    	string str;
    	int cas = 0;
    	char s[100000];
    	while(scanf("%d%d",&n,&m)!=EOF&&(n+m) != 0){
            map<string,int>mp;
    		int c;
    		for(int i = 1; i <= n; i++){
    			scanf("%s",s);
                str = s;
    			mp[str] = i;
    			scanf("%d",&need[i]);
    		}
    		memset(Map,0,sizeof(Map));
    		memset(capacity,0,sizeof(capacity));
    		getchar(); getchar();
    		for(int i = 1; i <= m; i++){
                str = "";
                gets(s);
                int len = strlen(s);
                for(int j = 0; j < len-1; j++)  {
                    if(s[j]==' '&&j!=0){
                        if(s[j-1]!=' '){
                            Map[mp[str]][i]=1;
                            capacity[mp[str]]++;
                        }
                        str="";
                    }else if(s[j]!=' ') str+=s[j];
                }
                if(str!="") {
                    Map[mp[str]][i]=1;
                    capacity[mp[str]]++;
                }
    		}
    		int enumc = (int)pow((double)2,(double)n);
    		int ans = 0;
            Dinic ansf;
    		for(int i = 0; i < enumc; i++){
                int j = i, cnum = 0, c = 0;
                while(j){
                    if(j&1)
                        cnum++;
                    j = j >> 1;
                }
                if(ans >= cnum) continue;
                int needMaxf = constructG(i);
                int Maxf = ansf.Maxflow(0,n+m+1);
                if(Maxf >= needMaxf){
                    ans = cnum;
                }
    		}
    		printf("Case #%d: ",++cas);
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    自定义标签
    ssm学习的第一个demo---crm(1)
    xml文件中的${}
    Mybatis的回顾学习
    Mapper的.xml文件的delete的参数问题
    mybatis中xml文件的${}和#{}区别
    Mybatis:通过MapperScannerConfigurer进行mapper扫描
    Spring的applicationContext.xml的疑问解析
    Spring MVC 的springMVC.xml疑问解析
    【剑指 Offer】04.二维数组中的查找
  • 原文地址:https://www.cnblogs.com/chengsheng/p/5510214.html
Copyright © 2011-2022 走看看