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三件衣服。这样所有的衣服连向一个超级汇点,容量为一。然后把顾客和相应尺寸的衣服连起来(记得是同尺寸的所有衣服)。最后求最大流。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <map>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 305;
const int INF = 0x3f3f3f3f;
const int OF = 50;
int n, m, L, s, t, rec[N];
map<string, int> mp2;
string size[7] = {"", "XS", "S", "M", "L", "XL", "XXL"};
struct Edge{
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[N];
void init() {
memset(rec, 0, sizeof(rec));
for (int i = 0; i < N; i++) G[i].clear();
edges.clear();
}
void addEdge(int from, int to, int cap, int flow) {
edges.push_back((Edge){from, to, cap, 0});
edges.push_back((Edge){to, from, 0, 0});
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
void input() {
mp2[size[1]] = 1;
mp2[size[2]] = 2;
mp2[size[3]] = 3;
mp2[size[4]] = 4;
mp2[size[5]] = 5;
mp2[size[6]] = 6;
for (int i = 1; i <= m; i++) {
addEdge(0, i, 1, 0);
}
string a, b;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
for (int j = 0; j < L; j++) {
int pa = mp2[a] + OF + j * 6;
addEdge(i, pa, 01, 0);
if (!rec[pa]) {
addEdge(pa, t, 1, 0);
rec[pa] = 1;
}
int pb = mp2[b] + OF + j * 6;
addEdge(i, pb, 1, 0);
if (!rec[pb]) {
addEdge(pb, t, 1, 0);
rec[pb] = 1;
}
}
}
}
int vis[N], d[N];
int BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[u] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int cur[N];
int DFS(int u, int a) {
if (u == t || a == 0) return a;
int flow = 0, f;
for (int &i = cur[u]; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (d[u] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[u][i]^1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int MF() { //dinic算法求最大流
int ans = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
ans += DFS(s, INF);
}
return ans;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &n, &m);
s = 0, t = 200;
L = n / 6;
init();
input();
int ans;
ans = MF();
if (ans == m) printf("YES\n");
else printf("NO\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-07 12:06:00