51nod 1392 装盒子
第一行一个数N表示盒子的个数。
接下来N行,每行两个正整数,表示每个盒子的长度和宽度。
所有整数都是正的(N,以及盒子的长宽),且不超过200。
一行一个整数表示最终最小的占地面积。
对于相同大小的盒子可以相互嵌套而只保留一个,考虑最大费用最大流,每个盒子i拆成a[i],b[i]两点,若x可放入y,则连边a[x]->b[y],流量1,费用为x的面积,代表x放入y对答案的影响,源点到a[i]连边,流量1,费用0,使每个盒子最多只能放进一个盒子,a[i],b[i]到汇点连边,流量1,费用0,使每个盒子最多只能被放入一个盒子
#include<cstdio>
#include<queue>
#include<algorithm>
int n;
struct pos{
int a,b;
}ps[];
bool operator<(pos a,pos b){
return a.a!=b.a?a.a<b.a:a.b<b.b;
}
bool operator==(pos a,pos b){
return a.a==b.a&&a.b==b.b;
}
const int N=,inf=0x3f3f3f3f;
int es[N],enx[N],ev[N],ec[N],e0[N],ep=,S,T,ans=,l[N],in[N],pv[N],pe[N];
std::queue<int>q;
void adde(int a,int b,int f,int c){
es[ep]=b;enx[ep]=e0[a];ev[ep]=f;ec[ep]=c;e0[a]=ep++;
es[ep]=a;enx[ep]=e0[b];ev[ep]=;ec[ep]=-c;e0[b]=ep++;
}
bool sp(){
for(int i=;i<=T;++i)l[i]=-inf;
l[S]=;
q.push(S);
while(!q.empty()){
int w=q.front();q.pop();
in[w]=;
for(int i=e0[w];i;i=enx[i])if(ev[i]){
int u=es[i];
if(l[w]+ec[i]>l[u]){
l[u]=l[w]+ec[i];
pv[u]=w;
pe[u]=i;
if(!in[u])in[u]=,q.push(u);
}
}
}
return l[T]>;
}
void mcs(){
for(int w=T;w!=S;w=pv[w]){
int e=pe[w];
--ev[e];
++ev[e^];
}
ans-=l[T];
}
int cs[];
int main(){
scanf("%d",&n);
for(int i=;i<n;++i)scanf("%d%d",&ps[i].a,&ps[i].b);
std::sort(ps,ps+n);
n=std::unique(ps,ps+n)-ps;
S=n*+;T=S+;
for(int i=;i<n;++i){
ans+=ps[i].a*ps[i].b;
for(int j=;j<n;j++)if(i!=j&&ps[i].a<=ps[j].a&&ps[i].b<=ps[j].b){
adde(i+,n+j+,,ps[i].a*ps[i].b);
}
adde(S,i+,,);
adde(n+i+,T,,);
adde(i+,T,,);
}
while(sp())mcs();
printf("%d",ans);
return ;
}
51nod 1392 装盒子的更多相关文章
- 51nod 1392 装盒子(费用流)
如果权值为\(1\)就是最长反链. 然而并不是.考虑用费用流. 把每一个盒子\(i\)拆成i和\(i+n\). 设源点为\(S\),汇点为\(T\). \(S\)向每一个i连容量为\(1\),费用为\ ...
- 51nod 1392:装盒子 匈牙利+贪心
1392 装盒子 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 关注 有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子 ...
- 51nod 算法马拉松4 D装盒子(网络流 / 二分图最优匹配)
装盒子 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子只可以套入长和宽分别不小于它的盒子,并且 ...
- 视频学习_css基础学习
块状元素 block element 容器元素 设置高宽 width height 可以容纳 文本 内脸 和其他块状 霸道 独占一行 特例:form 只容纳 块状元素 常见元素 http:// ...
- 递归转手工栈处理的一般式[C语言]
是任意形式的递归,是化解的一般式. 主题所谓的“递归调用化解为栈处理”,意思是,将递归函数调用化解为“一个由stack_push stack_pop stack_top等函数调用组成的循环式子”.这里 ...
- 抽象工厂模式详解 —— head first 设计模式
项目实例 假设你有一家 pizza 店,你有很多种 pizza,要在系统中显示你所有 pizza 种类.实现这个功能并不难,使用普通方式实现: public class PizzaStore { Pi ...
- 工厂模式之简单工厂模式,head first设计模式
简单设计模式比较简单,15分钟的时间,跟着我做,十几分钟学会简单设计模式,开始吧: 1.使用new实例化一个对象时,你肯定知道这种代码缺少弹性,绑定着具体的类会导致代码更加脆弱,简单工厂模式就是针对接 ...
- Codeforces Round #464 (Div. 2) B. Hamster Farm[盒子装仓鼠/余数]
B. Hamster Farm time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- css 大话盒子模型
什么是盒子模型? CSS中, Box Model叫盒子模型(或框模型),Box Model规定了元素框处理元素内容(element content).内边距(padding).边框(border) 和 ...
随机推荐
- #include #import @class 的一些用法区别
从网上查了一些资料,整理了一下,发现很多都说的比较详尽,下面摘录自网络 说一下#import同class之间的区别 在ios中我们经常会在.h和.m中引入一些类啊等等一般用的是#import来进行声明 ...
- Codeforces Round #365 (Div. 2) A 水
A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- com.google.guava 包解析 ——Google Guava官方教程(中文版)
全网址 http://ifeve.com/google-guava/ 竹子博客: http://www.cnblogs.com/peida/archive/2013/06/08/ ...
- Codeforces Flipping game 动态规划基础
题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...
- html部分---表单、iframe、frameset及其他字符的用法(以及name、id、value的作用与区别);
<form action="aa.html" method="post/get"> /action的作用是提交到..,methed是提交方法,用po ...
- c#部分---结构体再利用;
//定义一个结构体,存放关于车辆的几个信息 //将所有车的信息都放入集合中 //车型号 价格(W) 轴距 (mm) 油耗(L/100km) //宝马320Li 38 2920 6.9 //宝马520L ...
- caffe: test code 执行出问题: Check failed: FLAGS_weights.size() > 0 (0 vs. 0) Need model weights to score.
Check failed: FLAGS_weights.size() > 0 (0 vs. 0) Need model weights to score. 出现这个错误,但是我记得昨天还好好的, ...
- powershell samples
1,导出至EXCEL $arr =New-Object System.Collections.ArrayList $i = 1 $pstablelist = @(); $array =get-user ...
- 非对称SVD电影推荐系统
采用1M MovieLensz数据(80%train, 20%test, UserIDs range between 1 and 6040 ,MovieIDs range between 1 and ...
- 动态加载dll,并创建类对象放入到list中。
private List<IVisualControlsPlug> visualPlugs = new List<IVisualControlsPlug>(); public ...