hdu1074(集合上的动态规划 状态压缩)
来源:互联网 发布:pubmed数据库是什么 编辑:程序博客网 时间:2024/06/14 18:59
这是一道好题,是一道状态压缩DP,下面是参照大神写的代码。
有n门功课 ,共有1<<n个状态,每个状态对应一个集合,状态1001表示课程1和4在集合里面;
DP思想:假设某个状态A,j属于A,通过{ A-j } 来得到状态A,时间复杂度是n*(2^n)是一个NP问题,在n比较小的情况下可以用这种方法
代码如下:
#include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<time.h>#include<math.h>#define eps 1e-9#define inf 10000000#define P system("pause")using namespace std;struct node{ int cost,end; char name[100]; }a[20];struct node1{ int cost,reduce;//cost当前花的时间,reduce扣分 int per; //前一个状态}dp[1<<15];int n;void output(int k){ if(k<=0) return ; output(dp[k].per); int temp=dp[k].per^k; int id=0; //temp>>=1; temp=temp>>1; while(temp) { id++; temp>>=1; } printf("%s\n",a[id].name); }int main(){//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout); int t,i,j; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0;i<n;i++) scanf("%s%d%d",a[i].name,&a[i].end , &a[i].cost); dp[0].cost=dp[0].reduce=0; dp[0].per=-1; int k = (1<<n); for(i=1;i<k;i++){ dp[i].cost=0;dp[i].reduce=inf;dp[i].per=-1; for(j=0;j<n;j++) { int cur = 1<<j ; if(i&cur) { dp[i].cost=dp[i^cur].cost+a[j].cost; //i^cur表示从i集合中减去j元素 int reduc=dp[i].cost-a[j].end; if(reduc<0) reduc=0; if(dp[i].reduce >= dp[i^cur].reduce+reduc){ dp[i].reduce = dp[i^cur].reduce+reduc; dp[i].per=i^cur; } } } } printf("%d\n",dp[k-1].reduce); output(k-1); // printf("%d\n",dp[k-1].per); } // P; return 0; }
0 0
- hdu1074(集合上的动态规划 状态压缩)
- hdu1074动态规划状态压缩
- D - Doing Homework HDU1074 ( 动态规划 + 状态压缩 )
- 动态规划---状态压缩(即集合上的动态规划)
- 【BZOJ2734】【HNOI2012】集合选数(状态压缩,动态规划)
- 状态压缩的动态规划
- hdu1074 Doing Homework(状态压缩dp)
- hdu1074 Doing Homework (状态压缩dp)
- HDU1074 状态压缩DP
- HDU1074 状态压缩DP
- hdu1074状态压缩
- hdu1074(dp状态压缩)
- hdu1074 状态压缩dp
- hdu1074 状态压缩dp
- 状态压缩动态规划
- 状态压缩动态规划
- 状态压缩动态规划
- 状态压缩动态规划
- 构建FTP服务器——server-u
- 成为明星程序员的10个提示
- 解析xx.properties文件信息
- 【编译问题/JAVA】Class path contains multiple SLF4J bindings
- C++ STL 查找多个结果
- hdu1074(集合上的动态规划 状态压缩)
- oracle客户端连接oracle数据库有关配置
- 使用CAShapeLayer与UIBezierPath画出想要的图形
- 最值得珍惜的东西
- 跟我学做c#皮肤美化
- C++ 程序员必经之路 —— 构造、析构、虚析构
- bootstrap翻译文档
- 有关hadoop执行包含外在jar包的jar包程序
- 车牌识别