UVA 11045 My T-shirt suits me

一开始就想到网络流。。后来一想暴力能不能过。自己写的T了。看了别人有暴力过的。

暴力的思路就是6进制数字表示给予的衣服的数量。然后每个人的需求表示成01 的6位串然后爆搜。

网络流就建一个源一个汇 然后针对输入 i  - i + 6 边权为N/6; 然后M个人由衣服连M个人边权为1。与源直接相连的点就INF求最大流值判断即可。

别人的建图更简单一些。不需要设置INF;思路一样

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
int src,tag;
int N,M;
const int INF = 0x3f3f3f3f ;
char size[10][20] = {"xxa","XXL","XL","L","M","S","XS"};
int d[100],cap[100][100];
bool inq[100];
int flow[100][100];
int p[100];
void read()
{
    scanf("%d%d",&N,&M);
    N /= 6;
    memset(cap,0,sizeof(cap));
    for (int i = 1; i <= 6; i++)  { cap[0][i] = INF; cap[i][i + 6] = N; }
    for (int j = 1; j <= M; j++)
    {
        char a[8],b[8];
        scanf("%s%s",a,b);
        int s1 = -1, s2;
        for (int i = 1; i <= 6; i++)
        {
            if (strcmp(size[i],a) == 0)
                 s1 = i;
            if (strcmp(size[i],b) == 0)
                 s2 = i;
        }
        //printf("%d %d\n",s1,s2);
        cap[s1 + 6][12 + j] = 1;
        cap[s2 + 6][12 + j] = 1;
        cap[12 + j][12 + M + 1] = 1;
    }
    src = 0;
    tag = 12 + M + 1;
}
int Edmons_karp()
{
    queue<int>q;
    int a[100];
    while (!q.empty()) q.pop();
    memset(flow,0,sizeof(flow));
    int ans = 0;
    while (true)
    {
        memset(a,0,sizeof(a));
        a[src] = INF;
        q.push(src);
        while(!q.empty())
        {
            int u = q.front(); q.pop();
            for (int v = 1; v <= tag; v++)
                if (!a[v] && cap[u][v] > flow[u][v])
            {
                p[v] = u;
                q.push(v);
                a[v] = min(a[u],cap[u][v] - flow[u][v]);
            }
        }
        if (a[tag] == 0) break;
        for (int u = tag; u != src; u = p[u])
        {
            flow[p[u]][u] += a[tag];
            flow[u][p[u]] -= a[tag];
        }
        ans += a[tag];
    }
    return ans;
}
int main()
{
    //freopen("sample.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        read();
        int ans = Edmons_karp();
        //printf("%d\n",ans);
        if (ans >= M) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

TLE 的代码也贴一下吧

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
int st[32];
int N,M;
bool found;
char size[10][20] = {"XXL","XL","L","M","S","XS"};
int res;
void read()
{
    res = 0;
    scanf("%d%d",&N,&M);
    N /= 6;
    char tmp[10];
    for (int i = 0; i < 6 ; i++)
        tmp[i] = N + ‘0‘;
    tmp[6] = ‘\0‘;
    sscanf(tmp,"%d",&res);
    memset(st,0,sizeof(st));
    for (int i = 1; i <= M; i++)
    {
        char a[6],b[6];
        scanf("%s%s",a,b);
        int j;
        for (j = 0; j < 6; j++) if (strcmp(a,size[j]) == 0) break;
        st[i] |= 1 << j;
        for (j = 0; j < 6; j++) if (strcmp(b,size[j]) == 0) break;
        st[i] |= 1 << j;
    }
}
void calcu(int cur, int sta, int cnt)
{
    if (found) return ;
    if (cnt >= M) { found = true; return ; }
    int temp = sta;
    bool change = false;
    char str[20];
    for (int i = cur; i <= M; i++)
    {
        int a = -1,b = -1;
        for (int j = 0; j < 6; j++)
        {
            if (st[i] & (1 << j))
            {
                if (a == -1)
                {
                    a = j;
                }
                else
                    b = j;
            }
        }
        sprintf(str,"%d",temp);
        if (str[a] - ‘0‘ > 0)
        {
            change = true;
            str[a]--;
            sscanf(str,"%d",&temp);
            calcu(cur + 1, temp, cnt + 1);
            str[a]++;
        }
        temp = sta;
        if (str[b] - ‘0‘ > 0)
        {
            change = true;
            str[b]--;
            sscanf(str,"%d",&temp);
            calcu(cur + 1, temp, cnt + 1);
            str[b]++;
        }
        if (!change) return ;
    }
}
int main()
{
    //freopen("sample.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while (T--)
    {
        found = false;
        read();
        calcu(1,res,0);
        if (found) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

时间: 2024-08-10 02:05:44

UVA 11045 My T-shirt suits me的相关文章

uva 11045 My T-shirt suits me (二分图匹配 最大流)

uva 11045 My T-shirt suits me 题目大意:有n件衣服(一定是6的倍数,六种尺码n / 6套),m个试穿者,每个试穿者都有两种合适的尺码(尺码一共有六种:XS, S, M, L, XL, XXL).问是否所有试穿者都能找到合适的衣服. 解题思路:设置一个超级源点,连向所有的试穿者,容量为1.把相同的衣服,当成不同的,比如XS型号的衣服有三件,我们则把它分为编号为1, 1 + 6, 1 + 12三件衣服.这样所有的衣服连向一个超级汇点,容量为一.然后把顾客和相应尺寸的衣服

UVA 11045 My T-shirt suits me (二分图)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1986  My T-shirt suits me  Our friend Victor participates as an instructor in an environmental volunteer program. His boss asked Victor to distribut

UVA 11045-My T-shirt suits me(二分图匹配)

题意:有N件T恤,N是6的倍数,因为有6种型号,每种件数相同,有M个人,每个人有两种型号的T恤适合他,每个人可以挑其中的一种,问能否所有的人都能分配到T恤. 解析:典型的二分图匹配,每N/6为同种T恤,对于单个人,将他与它适合的两种T恤的所有标号连边,最后计算最大匹配,如果小于M,则不可行,否则可行. 代码如下: #include<cstdio> #include<cstring> #include<string> #include<algorithm> #

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

UVA - 12316 Sewing Buttons with Grandma (有重复元素的组合)

Description  Sewing Buttons with Grandma  After so many years of studying math in the Academy of Colombian Mathematics (ACM) in the tropic, Eloi has finally decided to visit his grandmother for the winter in the north hemisphere. ``Buttons and patche

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED