题意:
有一些箱子,已知长宽高(不能旋转),一个较大的箱子中最多可以放入一个较小的箱子(小箱子里有可能也有箱子),问最外层最少有多少箱子。
相当于有一些点(x, y, z),若两个点i和j满足xi < xj, yi < yj, zi < zj,那么i到j连边,求最小路径覆盖。
由最小路径覆盖 = n - 二分图最大匹配,写个hungary即可。
#include <cstdio>
const int maxn = 505, maxm = maxn * maxn;
int n, a[maxn], b[maxn], c[maxn], head[maxn], cnt, vis[maxn], from[maxn], clo;
struct _edge {
int v, next;
} g[maxm];
inline int iread() {
int f = 1, x = 0; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f * x;
}
inline void add(int u, int v) {
g[cnt] = (_edge) {v, head[u]};
head[u] = cnt++;
}
bool hungary(int x) {
for(int i = head[x]; ~i; i = g[i].next) if(vis[g[i].v] != clo) {
vis[g[i].v] = clo;
if(!from[g[i].v] || hungary(from[g[i].v])) {
from[g[i].v] = x;
return 1;
}
}
return 0;
}
int main() {
while(1) {
n = iread();
if(n == 0) break;
cnt = 0; clo = 0;
for(int i = 1; i <= n; i++) vis[i] = from[i] = 0, head[i] = -1, a[i] = iread(), b[i] = iread(), c[i] = iread();
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(i != j && a[i] < a[j] && b[i] < b[j] && c[i] < c[j])
add(i, j);
int ans = 0;
for(int i = 1; i <= n; i++) {
clo++;
ans += hungary(i);
}
printf("%d\n", n - ans);
}
return 0;
}