hdu 1074 Doing Homework

http://acm.hdu.edu.cn/showproblem.php?pid=1074

状态压缩DP 的模板题

学习了一下状态压缩DP

这里处理字典序的方法 有排序,貌似排序的还简单一些

我用的指针

signp指向上一个状态,p指向状态对应取的字符串

代码:


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
using namespace std;
const int inf=1000000000;
struct node
{
    char word[105];
    int deadline;
    int cost;
}num[20];
struct knode
{
    int score;
    int signp;
    int p;
    int cost;
}dp[1<<16];
void dfs(int n)
{
    if(n==0)
    return;
    dfs(dp[n].signp);
    puts(num[dp[n].p].word);
}
int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        scanf("%s%d%d",num[i].word,&num[i].deadline,&num[i].cost);
        dp[0].cost=0;
        dp[0].score=0;
        for(int i=1;i<(1<<n);i++)
        {
            dp[i].score=inf;
            for(int j=1;j<=n;j++)
            {
                int m=1<<(j-1);
                if(m&i)
                {
                    int k=i-m;
                    int score=num[j].cost-(num[j].deadline-dp[k].cost);
                    if(score<0)
                    score=0;
                    if(score+dp[k].score<dp[i].score)
                    {
                        dp[i].cost=dp[k].cost+num[j].cost;
                        dp[i].score=score+dp[k].score;
                        dp[i].signp=k;
                        dp[i].p=j;
                    }
                    else if(score+dp[k].score==dp[i].score&&strcmp(num[dp[i].p].word,num[j].word)<0)
                    {
                            dp[i].cost=dp[k].cost+num[j].cost;
                            dp[i].score=score+dp[k].score;
                            dp[i].signp=k;
                            dp[i].p=j;
                    }
                }
            }
        }
        printf("%d\n",dp[(1<<n)-1].score);
        dfs((1<<n)-1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值