题目大意:给定n个盒子,并且有们的长宽,一个盒子可以放到 另一个长宽<=自己长宽的盒子中,求最会的最小占地面积。
思路:法一:可以用二分匹配做,按照面积排序,注意和按照边长排序的区别,然后从大到小进行匹配(因为先要把大的进行匹配之后才匹配小的,否则小的匹配完了之后剩下下对面积大的盒子占地也会多)。
法二:用费用流建图,注意在链接盒子与盒子之间的关系时,费用是负的小盒子的面积这样求出来的是最大的放在大盒子里边的面积。
二分图
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int l,h;
}q[201];
int mp[201][201];
bool op(node a,node b){
return a.l*a.h<b.l*b.h;
}
int cro[201],n;
bool vis[201];
bool so(int x){
for(int i = n;i >=1;-- i){
if(mp[x][i]&&!vis[i]){
vis[i] = true;
if(cro[i] == -1||so(cro[i])){
cro[i] = x;
return true;
}
}
}
return false;
}
int main()
{
int m,i,j,k,ans;
while(~scanf("%d",&n)){
ans=0;
memset(mp,0,sizeof(mp));
for(i = 1;i <= n;++ i){
scanf("%d%d",&q[i].l,&q[i].h);
ans += q[i].l*q[i].h;
}
sort(q+1,q+1+n,op);
for(i = 1;i <= n;++i ){
for(j = i+1 ;j <= n;++ j){
if(q[j].l >= q[i].l&&q[j].h >= q[i].h)
mp[i][j]=1;
}
}
memset(cro,-1,sizeof(cro));
for(i = n;i >=1;-- i){
memset(vis,false,sizeof(vis));
if(so(i))
ans -= q[i].l*q[i].h;
}
printf("%d\n",ans);
}
return 0;
}
费用流:
#include<map>
#include<queue>
#include<cmath>
#include<cstdio>
#include<stack>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f3f
#define eps 1e-8
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
const double PI=acos(-1.0);
using namespace std;
struct node{
int to,w,c,next;
}q[200050*5];
int head[200000*5];
int st,ed,cnt,maxcost;
void add(int a,int b,int w,int c){
q[cnt].to = b;
q[cnt].w = w;
q[cnt].c = c;
q[cnt].next = head[a];
head[a] = cnt++;
q[cnt].to = a;
q[cnt].w = 0;
q[cnt].c = -c;
q[cnt].next = head[b];
head[b] = cnt++;
}
struct no{
int a,b;
}qq[200*4];
int ha[40],dis[2010],flow;
int f[2010],cur[2010];
bool vis[2010];
bool SPFA(){
memset(dis,inf,sizeof(dis));
memset(vis,false,sizeof(vis));
memset(cur,-1,sizeof(cur));
queue<int>Q;
while(!Q.empty())
Q.pop();
Q.push(st);
f[st]=inf;
dis[st]=0;
vis[st] = true;
while(!Q.empty()){
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head[u];~i;i=q[i].next){
int v = q[i].to;
if(dis[v] > dis[u] + q[i].c &&q[i].w > 0){
dis[v] = dis[u] + q[i].c;
cur[v] = i;
f[v] = min(f[u],q[i].w);
if(!vis[v]){
vis[v] = true;
Q.push(v);
}
}
}
}
return dis[ed] != inf;
}
int so(){
int ant=0;
for(int i = ed;i != st;i = q[cur[i]^1 ].to){
q[cur[i] ].w -= 1;
q[cur[i]^1 ].w +=1;
ant += q[cur[i] ].c;
}
return ant;
}
bool op(no x,no y){
if(x.a != y.a)
return x.a<y.a;
return x.b<y.b;
}
int main(){
int n,m,i,j,k,cla;
while(~scanf("%d",&n)){
memset(ha,0,sizeof(ha));
maxcost=flow=cnt=0;st=0;
int ans=0;
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
int s=0;
for(i = 1;i <= n;++ i){
scanf("%d%d",&qq[i].a,&qq[i].b);
s += qq[i].a*qq[i].b;
}
sort(qq+1,1+qq+n,op);
for( i = 1; i <= n; ++ i){
add(st,i,1,0);
}
for(i = 1;i <= n;++i ){
for(j = 1;j < i;++ j){
if(qq[i].a >= qq[j].a && qq[i].b >= qq[j].b){
add(i,j+n,1,-(qq[j].a*qq[j].b));
}
}
}
ed = 2 * n + 1;
for(i = 1;i <= n;++ i){
add( i+n,ed,1,0 );
}
int tmp=0;
while(SPFA()){
tmp += so();
}
printf("%d\n",s+tmp);
}
return 0;
}