51 nod 1392 装盒子(费用流||二分图 骚)

基准时间限制:1 秒 空间限制:131072 KB 分值: 160  难度:6级算法题
 收藏
 关注
有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放。注意一个盒子只可以套入长和宽分别不小于它的盒子,并且一个盒子里最多只能直接装入另外一个盒子 (但是可以不断嵌套),例如1 * 1 可以套入2 * 1,而2 * 1再套入2 * 2。套入之后盒子占地面积是最外面盒子的占地面积。给定N个盒子大小,求最终最小的总占地面积。
Input
第一行一个数N表示盒子的个数。
接下来N行,每行两个正整数,表示每个盒子的长度和宽度。
所有整数都是正的(N,以及盒子的长宽),且不超过200。
Output
一行一个整数表示最终最小的占地面积。
Input示例
3
1 1
1 2
2 1
Output示例
4


二分图 +贪心(面积大的先匹配)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
const int inf = 1000;
const int N = 1000;
const double eps = 1e-7;
int a[N], b[N];
struct node
{
    int to, next, cap;
    int d;
}p[1000000];
int head[N], cnt, n;
int match[N], vis[N], w[N][N];
void init()
{
    memset(head,-1,sizeof(head));
    memset(match,-1,sizeof(match));
    memset(w,0,sizeof(w));
    cnt=0;
    return ;
}
void add(int u,int v)
{
    p[cnt].to=v,p[cnt].next=head[u];head[u]=cnt++;
    return ;
}
struct node1
{
    int l, r;
    bool operator <(const node1 &A)const
    {
        return l*r<A.l*A.r;
    }
}g[N];
int cmp1(node1 A,node1 B){return (A.l==B.l&&A.r==B.r);}
int dfs(int u)
{
    for(int i=n;i>=1;i--)
    {
        int v=i;
        if(vis[v]||w[u][v]==0) continue;
        vis[v]=1;
        if(match[v]==-1||dfs(match[v]))
        {
            match[v]=u;
            return 1;
        }
    }
    return 0;
}

int main()
{
    init();
    scanf("%d", &n);
    int sum=0;
    for(int i=1;i<=n;i++) scanf("%d %d", &g[i].l,&g[i].r);
    sort(g+1,g+n+1);
    for(int i=1;i<=n;i++)
    {
        sum+=g[i].l*g[i].r;
        for(int j=i+1;j<=n;j++)
            if(g[i].l<=g[j].l&&g[i].r<=g[j].r)  w[i][j]=1;
    }
    for(int i=n;i>=1;i--)
    {
        if(dfs(i))
        {
            memset(vis,0,sizeof(vis));
            sum-=g[i].l*g[i].r;
        }
    }
    printf("%d\n",sum);
    return 0;
}



费用流:每次计算出 这条路径中的 最小边 的费用 即为这次花费 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
const int inf = 0x3f3f3f3f;
const int N = 1e5;
const double eps = 1e-7;
int a[N], b[N];
struct node
{
    int to, next, cap;
    int d;
}p[1000000];
int head[N], cnt;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
    return ;
}
void add(int u,int v,int cap,int d)
{
    p[cnt].to=v,p[cnt].cap=cap,p[cnt].next=head[u],p[cnt].d=d;head[u]=cnt++;
    p[cnt].to=u,p[cnt].cap=0,p[cnt].next=head[v],p[cnt].d=-d;head[v]=cnt++;
    return ;
}
queue<int>q;
int vis[N], pre[N];
int dist[N],dx[N];
int spfa(int s,int t)
{
    for(int i=s;i<=t;i++) vis[i]=0,dist[i]=inf,dx[i]=inf, pre[i]=-1;
    while(!q.empty()) q.pop();
    dx[s]=0,dist[s]=0,vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=p[i].next)
        {
            int v=p[i].to,mint=min(dx[u],p[i].d);
            if(p[i].cap>0&&dist[v]>dist[u]+p[i].d&&mint<dx[v])
            {
                pre[v]=i;
                dx[v]=mint,dist[v]=dist[u]+p[i].d;
                if(!vis[v]) vis[v]=1,q.push(v);
            }
        }
    }
    return dx[t]!=inf;
}
double min_cost(int s,int t)
{
    int cost=0;
    while(spfa(s,t))
    {
        int flow=inf;
        for(int i=pre[t];i!=-1;i=pre[p[i^1].to]) flow=min(flow,p[i].cap);
        for(int i=pre[t];i!=-1;i=pre[p[i^1].to]) p[i].cap-=flow,p[i^1].cap+=flow;
        cost+=flow*dx[t];
    }
    return cost;
}
struct node1
{
    int l, r;
    bool operator <(const node1 &A)const
    {
        if(l!=A.l) return l<A.l;
        return r<A.r;
    }
}g[N];
int cmp1(node1 A,node1 B){return (A.l==B.l&&A.r==B.r);}
int main()
{
    int n;
    init();
    scanf("%d", &n);
    for(int i=1;i<=n;i++) scanf("%d %d", &g[i].l,&g[i].r);
    sort(g+1,g+n+1);
    n=unique(g+1,g+n+1,cmp1)-(g+1);
    int s=0, t=2*n+1;
    for(int i=1;i<=n;i++)
    {
        add(s,i,1,0);
        add(i,n+i,1,-g[i].l*g[i].r);
        add(n+i,t,1,0);
        for(int j=i+1;j<=n;j++)
            if(g[i].l<=g[j].l&&g[i].r<=g[j].r)   add(n+i,j,1,0);
    }
    int ans=-min_cost(s,t);
    printf("%d\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值