My T-shirt suits me - UVa 11045 最大流

76 篇文章 0 订阅

My T-shirt suits me

Our friend Victor participates as an instructor in an environmental volunteer program. His boss asked Victor to distribute N T-shirts to M volunteers, one T-shirt each volunteer, where N is multiple of six, and N$ \ge$M. There are the same number of T-shirts of each one of the six available sizes: XXL, XL, L, M , S, and XS. Victor has a little problem because only two sizes of the T-shirts suit each volunteer.


You must write a program to decide if Victor can distribute T-shirts in such a way that all volunteers get a T-shirt that suit them. If N $ \neq$ M, there can be some remaining T-shirts.

Input 

The first line of the input contains the number of test cases. For each test case, there is a line with two numbers N and MN is multiple of 6, 1$ \le$N$ \le$36, and indicates the number of T-shirts. Number M1$ \le$M$ \le$30, indicates the number of volunteers, with N$ \ge$M. Subsequently, M lines are listed where each line contains, separated by one space, the two sizes that suit each volunteer (XXL, XL, L, M , S, or XS).

Output 

For each test case you are to print a line containing YES if there is, at least, one distribution where T-shirts suit all volunteers, or NO, in other case.

Sample Input 

 
3
18 6
L XL
XL L
XXL XL
S XS
M S
M L
6 4
S XL
L S
L XL
L XL
6 1
L M

Sample Output 

 
YES
NO
YES

题意:给你n/6套衣服,每个人只能穿给定的两种大小的衣服,问是否能够匹配。

思路:用网络流的话就是最大流,其实也可以用二分图匹配。吐槽:只能穿L和S是什么情况,能穿大号和小号,穿中号就不行么。。。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
int T,t,n,m,N;
char b[][5]={"\0","XXL","XL","L","M","S","XS"};
char s[10];
map<string,int> match;
int cap[55][55],flow[55][55],F,p[55],mi[55],INF=1e9;
bool vis[55];
queue<int> qu;
void EK()
{
    int u,v,i,j;
    memset(p,0,sizeof(p));
    F=0;
    while(true)
    {
        memset(mi,0,sizeof(mi));
        mi[0]=INF;
        qu.push(0);
        while(!qu.empty())
        {
            u=qu.front();
            qu.pop();
            for(v=0;v<=N;v++)
               if(!mi[v] && cap[u][v]>flow[u][v])
               {
                   p[v]=u;
                   qu.push(v);
                   mi[v]=min(mi[u],cap[u][v]-flow[u][v]);
               }
        }
        if(mi[N]==0)
          break;
        for(u=N;u!=0;u=p[u])
        {
            flow[p[u]][u]+=mi[N];
            flow[u][p[u]]-=mi[N];
        }
        F+=mi[N];
    }
}
int main()
{
    int i,j,k;
    for(i=1;i<=6;i++)
       match[b[i]]=i;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d%d",&n,&m);
        N=6+m+1;
        memset(cap,0,sizeof(cap));
        memset(flow,0,sizeof(flow));
        for(i=1;i<=m;i++)
        {
            for(k=1;k<=2;k++)
            {
                scanf("%s",s);
                cap[match[s]][6+i]=1;
            }
            cap[6+i][N]=1;
        }
        for(i=1;i<=6;i++)
           cap[0][i]=n/6;
        EK();
        if(F==m)
          printf("YES\n");
        else
          printf("NO\n");
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值