动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
2016-04-14 14:12
405 查看
2621: [Usaco2012 Mar]Cows in a Skyscraper
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 303 Solved: 150
[Submit][Status][Discuss]
Description
[Mark Gordon, Neal Wu, Fatih Gelgi, 2012] A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor. The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.Input
Line 1: N and W separated by a space. * Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.Output
* Line 1: A single integer, R, indicating the minimum number of elevator rides needed. * Lines 2..1+R: Each line describes the set of cows taking one of the R trips down the elevator. Each line starts with an integer giving the number of cows in the set, followed by the indices of the individual cows in the set.Sample Input
4 105
6
3
7
Sample Output
3#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int INF=1000000000; const int maxn=1<<20; int n,W,v[20]; struct Node{ int x,y; Node(int x_=INF,int y_=INF){ x=x_;y=y_; } Node operator +(Node b){ if(y+b.y>W) return Node(x+b.x+1,b.y); return Node(x+b.x,y+b.y); } bool operator <(const Node &b)const{ return x!=b.x?x<b.x:y<b.y; } }; Node f[maxn]; int cnt,st[maxn]; int st2[maxn],tmp; int vis[maxn],tim; int main(){ scanf("%d%d",&n,&W); for(int i=1;i<=n;i++) scanf("%d",&v[i]); st[++cnt]=0;f[0].x=f[0].y=0; for(int Ti=1;Ti<=n;Ti++){ tim++; for(int j=1;j<=cnt;j++){ for(int i=1;i<=n;i++){ if(st[j]&(1<<(i-1))) continue; f[st[j]|(1<<(i-1))]=min(f[st[j]|(1<<(i-1))],f[st[j]]+Node(0,v[i])); if(vis[st[j]|(1<<(i-1))]!=tim){ st2[++tmp]=st[j]^(1<<(i-1)); vis[st[j]|(1<<(i-1))]=tim; } } } memcpy(st,st2,sizeof(st2)); cnt=tmp;tmp=0; } printf("%d\n",f[(1<<n)-1].y>0?1+f[(1<<n)-1].x:f[(1<<n)-1].x); return 0; }
相关文章推荐
- iOS代码获取图片主色调
- Node.js实现数据推送
- random 模块
- VC屏幕截图并保存为bmp、jpg、png等格式文件
- IOS-UI学习笔记(超级猜图)
- Android合并两个APP的具体做法(掌握)
- bug管理工具为开发者工作带来哪些改变?
- 160413、生成随机校验码
- $0返回值的含义
- FPGA低级建模---按键去抖动
- android:configChanges
- Linux监控子进程实现
- Hive外部分区表加载flume打到hdfs上文件,读不到.tmp文件
- SQL的更新语句update(更新表中数据)
- MySQL数据库时区配置
- JVM1
- Java编译后生成带$符号的class
- 如何确定函数依赖和其作用
- PIL使用示例
- 在 eclipse 上安装 genymotion 插件不成功的解决办法