题意:
衣服共有六种型号,现给出套数,志愿者人数,以及每个志愿者合适的型号,问能否让所有志愿者都有衣服
思路:
由于衣服共六种型号,又有中数量,可以知道每种型号几件,这是建一个起点0指向每个型号,cap为n/6,然后志愿者可穿型号流向志愿者,流量为1,然后每个志愿者流向终点,流量也为1,因为每个人最多穿一件
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
const int maxn = 40;
const int INF = 0x3c3c3c3c;
int m, n;
char ch[6][4] = {"XXL", "XL", "L", "M", "S", "XS"};
int change(char* s) {
for(int i=0; i<=5; i++)
if(strcmp(s, ch[i]) == 0) return i+1;
}
int p[maxn];
int a[maxn];
int flow[maxn][maxn];
int map[maxn][maxn];
void ek2() {
memset(flow, 0, sizeof(flow));
int ans = 0;
queue<int> q;
while(1) {
q.push(0);
memset(a, 0, sizeof(a));
a[0] = INF;
memset(p, 0, sizeof(p));
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i=0; i<=m+7; i++) {
if(!a[i] && map[u][i]>flow[u][i]) {
a[i] = min(a[u], map[u][i]-flow[u][i]);
p[i] = u;
q.push(i);
}
}
}
if(a[m+7]==0) break;
for(int i=m+7; i; i=p[i]) {
flow[p[i]][i] += a[m+7];
flow[i][p[i]] -= a[m+7];
}
ans += a[m+7];
}
if(ans == m) printf("YES\n");
else printf("NO\n");
}
int main() {
int kase;
scanf("%d", &kase);
char str[4];
while(kase--) {
memset(map, 0, sizeof(map));
int num = 0;
scanf("%d%d", &n, &m);
for(int i=1; i<=6; i++) {
map[0][i] = n/6;
}
for(int i=1; i<=m; i++){
scanf("%s", str);
int k = change(str);
map[k][6+i] = 1;
scanf("%s", str);
k = change(str);
map[k][6+i] = 1;
}
for(int i=7; i<=m+6; i++) {
map[i][m+7] = 1;
}
ek2();
}
return 0;
}