您的位置:首页 > 产品设计 > UI/UE

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

2015-01-29 00:27 453 查看


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

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

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 M. N is
multiple of 6, 1

N

36,
and indicates the number of T-shirts. Number M, 1

M

30,
indicates the number of volunteers, with N

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
==0)
          break;
        for(u=N;u!=0;u=p[u])
        {
            flow[p[u]][u]+=mi
;
            flow[u][p[u]]-=mi
;
        }
        F+=mi
;
    }
}
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]
=1;
        }
        for(i=1;i<=6;i++)
           cap[0][i]=n/6;
        EK();
        if(F==m)
          printf("YES\n");
        else
          printf("NO\n");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: