LouZhang

导航

poj_2584,多重二分匹配

http://poj.org/problem?id=2584

原理和普通二分匹配一样

多重二分匹配也可以像普通二分匹配那样,通过拆点建图用最大流来解

作为自己的模板吧

#include<cstdio>
#include<cstring>

int g[30][30];
int smlxt[30];
char s[15];
int n;
int link[30][30], vlink[30], vis[30];

int f(char c){
    if(c == 'S') return 1;
    if(c == 'M') return 2;
    if(c == 'L') return 3;
    if(c == 'X') return 4;
    if(c == 'T') return 5;
}
int dfs(int u){
    for(int i = 1; i <= 5; i ++){
        if(g[u][i] && !vis[i]){
            vis[i] = 1;
            if(vlink[i] < smlxt[i]){
                link[i][vlink[i]++] = u;
                return 1;
            }
            for(int j = 0; j < smlxt[i]; j ++){
                if(dfs(link[i][j])){
                    link[i][j] = u;
                    return 1;
                }
            }
        }
    }
    return 0;
}
void solve(){
    int ans = 0;
    memset(vlink, 0, sizeof vlink);
    for(int i = 1; i <= n; i ++){
        memset(vis, 0, sizeof vis);
        ans += dfs(i);
    }
    if(ans == n)
      puts("T-shirts rock!");
    else
      puts("I'd rather not wear a shirt anyway...");
}
int main(){
    while(scanf("%s", s)){
        if(strcmp(s, "ENDOFINPUT") == 0) break;
        scanf("%d", &n);
        memset(g, 0, sizeof g);
        for(int i = 1; i <= n; i ++){
            scanf("%s", s);
            int v1 = f(s[0]), v2 = f(s[1]);
            for(int j = v1; j <= v2; j ++)
              g[i][j] = 1;
        }
        for(int i = 1; i <= 5; i ++) scanf("%d", smlxt+i);
        scanf("%s", s);
        solve();
    }
    return 0;
}

posted on 2012-09-02 17:57  louzhang_swk  阅读(211)  评论(1编辑  收藏  举报