HUD 1074 doing homework(状压DP)

来源:互联网 发布:linux基础试题选择题 编辑:程序博客网 时间:2024/06/10 18:29

状态压缩的一道题,刚开始我根本想不到是状态压缩,我以为会用什么贪心算法做出来,看了网上大神的解法才知道是要位压缩,感觉不可思议。

状态变量s从0—>(1<<n-1),然后再加上一个循环变量for(i:1->n),二进制的s里面第j位为1表示第j个作业已经做完了,然后对做完第j个作业的之后的状态进行刷新;换一种说法就是刷新s状态是根据能达到这一状态做的最后一种作业来定的;


Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8124    Accepted Submission(s): 3733


Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius
#include<stdio.h>#include<string.h>#include<math.h>#include <iostream>#include <algorithm>using namespace std;const int MAXNUM=1<<15;const int INF = 0x7fffffff;struct{    char name[101];    int deadline;    int workday;}homework[20];struct{    int reduce;    int cost;    int piror;  //该状态之前的最后写的一们作业;}dp[MAXNUM+100];int cases,n;int visited[MAXNUM+100];bool  loop(){    if(!cases)        return false;    cases--;    memset(visited,0,sizeof(visited));    cin>>n;    for(int i=1;i<=n;i++)    {        cin>>homework[i].name>>homework[i].deadline>>homework[i].workday;    }    long long maxstation=(1<<n)-1;    for(long s=0;s<maxstation;s++)    {          for(int i=1;i<=n;i++)          {              if((s&(1<<(i-1)))==0)              {                  long long thistation=s+(1<<(i-1));                  int thiswork=dp[s].cost+homework[i].workday;                  int thisreduce=thiswork-homework[i].deadline;                  if(thisreduce<0)                    thisreduce=0;                  if((visited[thistation]&&dp[s].reduce+thisreduce<dp[thistation].reduce))                  {                      dp[thistation].reduce=dp[s].reduce+thisreduce;                      dp[thistation].piror=i;                      dp[thistation].cost=thiswork;                  }                  if((visited[thistation]==0))                  {                       dp[thistation].reduce=dp[s].reduce+thisreduce;                      dp[thistation].piror=i;                      dp[thistation].cost=thiswork;                      visited[thistation]=1;                  }              }          }    }    long long total=(1<<n)-1;    cout<<dp[total].reduce<<endl;    int j=n;    int outs[n+1];    while(total)    {        int k=dp[total].piror;        outs[j]=k;        j--;        total-=(1<<(k-1));    }    for(int i=1;i<=n;i++)    {        cout<<homework[outs[i]].name<<endl;    }    return true;}int main(){    cin.tie(0);    cin.sync_with_stdio(false);  // freopen("in.txt","r",stdin);    cin>>cases;    while(loop());    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 穿凉鞋脚后跟干裂起硬皮怎么办 夏天穿凉鞋磨脚怎么办 lv皮带黑色掉漆怎么办 黑色衣服穿在身上掉色怎么办 电信卡流量超了怎么办 移动卡流量超了怎么办 狗狗老是挠痒痒怎么办 出差同住的同事睡觉打鼾怎么办 小孩皮肤太黑了怎么办 苹果平板突然黑屏打不开怎么办 孩子认人晚上哭怎么办 主腹动脉有硬块怎么办 糖链抗原125偏高怎么办 狗长了个肿瘤怎么办 腺肌瘤糖类抗原125升高怎么办 糖类抗原724单项升高怎么办? 化疗期间糖类抗原升高怎么办? 门面租客到期不搬怎么办 这几天老想初恋怎么办 结婚了还想初恋怎么办 九年了想初恋了怎么办 吃肥肉恶心想吐怎么办 宝宝吃了母乳不吃奶粉怎么办 母猫的奶少怎么办 鲤鱼打挺起不来怎么办 练不会鲤鱼打挺怎么办 鲤鱼打挺脖子痛怎么办 新买的沙发太高怎么办 额最后离开公司没人关灯怎么办 看到我妹妹就烦怎么办 野塘钓鱼不开口怎么办 团关系找不到接收地怎么办 两岁宝宝就是不肯说话怎么办 两岁宝宝不肯吃药怎么办 并蹄莲叶子大黄怎么办 异地恋见面来大姨妈怎么办 奶水太多宝宝老是呛到怎么办 奶水太多吃奶婴儿呛怎么办 人家不愿意交我为朋友怎么办 面对诋毁我的人怎么办 做事太细致速度太慢怎么办