一开始就想到网络流。。后来一想暴力能不能过。自己写的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 == ? b : gcd(b, a % b);}
int src,tag;
int N,M;
const int INF = 0x3f3f3f3f ;
char size[][] = {"xxa","XXL","XL","L","M","S","XS"};
int d[],cap[][];
bool inq[];
int flow[][];
int p[];
void read()
{
scanf("%d%d",&N,&M);
N /= ;
memset(cap,,sizeof(cap));
for (int i = ; i <= ; i++) { cap[][i] = INF; cap[i][i + ] = N; }
for (int j = ; j <= M; j++)
{
char a[],b[];
scanf("%s%s",a,b);
int s1 = -, s2;
for (int i = ; i <= ; i++)
{
if (strcmp(size[i],a) == )
s1 = i;
if (strcmp(size[i],b) == )
s2 = i;
}
//printf("%d %d\n",s1,s2);
cap[s1 + ][ + j] = ;
cap[s2 + ][ + j] = ;
cap[ + j][ + M + ] = ;
}
src = ;
tag = + M + ;
}
int Edmons_karp()
{
queue<int>q;
int a[];
while (!q.empty()) q.pop();
memset(flow,,sizeof(flow));
int ans = ;
while (true)
{
memset(a,,sizeof(a));
a[src] = INF;
q.push(src);
while(!q.empty())
{
int u = q.front(); q.pop();
for (int v = ; 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] == ) 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 ;
}

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 == ? b : gcd(b, a % b);}
int st[];
int N,M;
bool found;
char size[][] = {"XXL","XL","L","M","S","XS"};
int res;
void read()
{
res = ;
scanf("%d%d",&N,&M);
N /= ;
char tmp[];
for (int i = ; i < ; i++)
tmp[i] = N + '';
tmp[] = '\0';
sscanf(tmp,"%d",&res);
memset(st,,sizeof(st));
for (int i = ; i <= M; i++)
{
char a[],b[];
scanf("%s%s",a,b);
int j;
for (j = ; j < ; j++) if (strcmp(a,size[j]) == ) break;
st[i] |= << j;
for (j = ; j < ; j++) if (strcmp(b,size[j]) == ) break;
st[i] |= << 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[];
for (int i = cur; i <= M; i++)
{
int a = -,b = -;
for (int j = ; j < ; j++)
{
if (st[i] & ( << j))
{
if (a == -)
{
a = j;
}
else
b = j;
}
}
sprintf(str,"%d",temp);
if (str[a] - '' > )
{
change = true;
str[a]--;
sscanf(str,"%d",&temp);
calcu(cur + , temp, cnt + );
str[a]++;
}
temp = sta;
if (str[b] - '' > )
{
change = true;
str[b]--;
sscanf(str,"%d",&temp);
calcu(cur + , temp, cnt + );
str[b]++;
}
if (!change) return ;
}
}
int main()
{
//freopen("sample.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--)
{
found = false;
read();
calcu(,res,);
if (found) printf("YES\n");
else printf("NO\n");
}
return ;
}

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

  1. UVa 11045 My T-shirt suits me / 二分图

    二分图建图 判断是否是完全匹配就行 最大流也行 #include <cstdio> #include <cstring> const int MAX = 300; int a[ ...

  2. Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 7. Graph Algorithms and Implementation Techniques

    uva 10803 计算从任何一个点到图中的另一个点经历的途中必须每隔10千米 都必须有一个点然后就这样 floy 及解决了 ************************************* ...

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

    题意:有N件T恤,N是6的倍数,因为有6种型号,每种件数相同,有M个人,每个人有两种型号的T恤适合他,每个人可以挑其中的一种,问能否所有的人都能分配到T恤. 解析:典型的二分图匹配,每N/6为同种T恤 ...

  4. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  5. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  6. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  7. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  8. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  9. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

随机推荐

  1. Spring的注解@Qualifier

    近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...

  2. Dropping Balls(小球下落)

    紫书P148,例题6-6 Sample Input 4 2 3 4 10 1 2 2 8 128 Sample Output 12 7 512 3 255 这应该不仅仅是一棵完全二叉树,题目中说保证所 ...

  3. Hive 文件格式 & Hive操作(外部表、内部表、区、桶、视图、索引、join用法、内置操作符与函数、复合类型、用户自定义函数UDF、查询优化和权限控制)

    本博文的主要内容如下: Hive文件存储格式 Hive 操作之表操作:创建外.内部表 Hive操作之表操作:表查询 Hive操作之表操作:数据加载 Hive操作之表操作:插入单表.插入多表 Hive语 ...

  4. java线程安全(单例模式)(转载)

    原文链接:http://www.jameswxx.com/java/%E8%AF%B4%E8%AF%B4%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F/ 单例模式?多么简单! ...

  5. 【转帖】置高并发jdbc连接池

    简单的MySQL连接池 <Resource type="javax.sql.DataSource" name="jdbc/TestDB" factory= ...

  6. 绑定host域名 修改手机hosts域名

    windows: C:\Windows\System32\drivers\etc\hosts # 在这儿输入你需要绑定的 hosts 116.31.72.421129 bro-user.flyme.c ...

  7. 使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码 (jvm性能调优)

    技术交流群:233513714 本文是<JVM 性能调优实战之:一次系统性能瓶颈的寻找过程> 的后续篇,该篇介绍了如何使用 JDK 自身提供的工具进行 JVM 调优将 TPS 由 2.5 ...

  8. Spring Boot :Request请求处理流程

    技术交流群:233513714

  9. 《Cracking the Coding Interview》——第3章:栈和队列——题目5

    2014-03-18 05:33 题目:用两个栈来实现一个队列. 解法:栈是反的,队列是正的,反了再反就正过来了.所以,请看代码.操作中时间复杂度有O(1)的,有O(n)的,但均摊下来时间符合O(1) ...

  10. 课时2:用python设计第一个游戏

    目录: 一.第一个小游戏 二.缩进 三.BIF 四.课时02课后习题及答案 ********************* 一.第一个小游戏 ********************* # p2_1.py ...