[NOIP模拟] 方盒子 费用流

Description

    给你一些盒子,当且仅当一个盒子的长宽都小于等于另一个盒子,那么这个盒子可以被放进另一个盒子。求最外层的盒子的最小面积。

Input

    N, N 个盒子的形状。

Output

    最优解。

Sample input

3
1 1
1 2
2 1

Sample output

4

Hint

1 N, L, W 200


Solution

    这估计是我有史以来炸的最惨的一天了,我还是太弱了啊,找规律的题型明显不行啊,而且我的暴力也很垃圾啊。
    这道题很明显得是个费用流,我在考场上也想到是个费用流,但是完全想不出来如何构图,明显是自己太弱了。其实我们只需要费用流转换一下,我们不求最大流最小费用,而是最大费用,我们这样建图, 建两排点,点代表盒子,一个盒子能个包含另一个各自就连一条上节为 1, 费用为小的盒子的面积,原点向一排点全部连边,另一排点连向汇点,然后跑一边最大费用, 总的盒子的面积减去跑出来的费用就是答案,其实这个最大费用,可以将边权改成负的,然后跑最小费用。唉,我太弱了。




Code

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <ctime>
#include <map>
#include <vector>
#include <queue>
using namespace std;

inline int read() {
    int i = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1; ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        i = (i << 3) + (i << 1) + ch - '0'; ch = getchar();
    }
    return i * f;
}

const int MAXN = 5010;
const int MAXM = 5e4 + 10;
const int INF = 0x3f3f3f3f;
int first[MAXN], Enum, dist[MAXN], pre[MAXN], N, M, source, sink, tail;

struct node {
    int x, y;
    bool operator < (const node & a) const {
        if(x == a.x) return y < a.y;
        return x < a.x;
    }
    bool operator == (const node & a) const {
        return x == a.x && y == a.y;
    }
};
node a[MAXN], b[MAXN];

struct point {
    int from, to, cap, flow, cost, next;
};
point edge[MAXM * 2];
bool vis[MAXN];

inline void init() {
    Enum = 0;
    memset(first, -1, sizeof(first));   
}

inline void addedge(int u, int v, int w, int c) {
    point E1 = {u, v, w, 0, c, first[u]};
    edge[Enum] = E1;
    first[u] = Enum++;
    point E2 = {v, u, 0, 0, -c, first[v]};
    edge[Enum] = E2;
    first[v] = Enum++;
}

inline bool SPFA(int s, int t) {
    queue<int> que;
    memset(dist, INF, sizeof(dist));
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    dist[s] = 0;
    vis[s] = true;
    que.push(s);
    while(!que.empty()) {
        int u = que.front();
        que.pop();
        vis[u] = false;
        for(int i = first[u]; i != -1; i = edge[i].next) {
            point E = edge[i];
            if(dist[E.to] > dist[u] + E.cost && E.cap > E.flow) {
                dist[E.to] = dist[u] + E.cost;
                pre[E.to] = i;
                if(!vis[E.to]) {
                    vis[E.to] = true;
                    que.push(E.to);
                }
            }
        }
    }
    return pre[t] != -1;
}

int MCMF(int s, int t) {
    int cost = 0, f = 0;
    while(SPFA(s, t)) {
        int ans = INF;
        for(int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
            point E = edge[i];
            ans = min(ans, E.cap - E.flow);
        }
        for(int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) {
            edge[i].flow += ans;
            edge[i ^ 1].flow -= ans;
            cost += edge[i].cost * ans;
        }
    }
    return cost;
}

int main() {
    int n = read();
    memset(first, 255, sizeof(first));
    for(int i = 1; i <= n; ++i) a[i].x = read(), a[i].y = read();
    sort(a + 1, a + n + 1);
    n = unique(a + 1, a + n + 1) - a - 1;
    int S = 0, T = n * 2 + 1;
    for(int i = n; i >= 1; --i) {
        addedge(S, i, 1, 0);
        addedge(i + n, T, 1, 0);
        for(int j = 1; j <= i; ++j) 
            if(i != j) {
                if(a[i].y >= a[j].y)
                    addedge(i, j + n, 1, -a[j].x * a[j].y);
            }
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i)
        ans += a[i].x * a[i].y;
    printf("%d\n", ans + MCMF(S, T));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值