HDU 1074 Doing Homework,dfs+剪枝(280ms),状态压缩+dp(15ms)

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

Doing Homework

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


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 hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework).

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.
 

Output
For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.
 

Sample Input
  
  
2 3 Computer 3 3 English 20 1 Math 3 2 3 Computer 3 3 English 6 3 Math 6 3
 

Sample Output
  
  
2 Computer Math English 3 Computer English Math
Hint
In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.

题意:有 n 门功课,给出完成每门功课所要的天数和要交作业的日期,超期一天不交扣一分,问怎样安排做作业的顺序可以扣分最少,把最少分输出以及做功课的顺序,如果多个顺序都可以得到最优解,输出字典序最小的那组。

思路:把所有顺序都遍历了,选最优的。dfs剪枝(280ms)(剪枝剪不好就会超时,就比如我写的dfs,0.0),或者是dp(15ms)

大神的dp代码:(我看了好久才明白,我改了代码,输出了中间量,可以看一下,在下面有)

/*
可以用0和1来表示该科目是否完成
那么可以用二进制来表示科目完成的状态
如:假设状态数是5<==>101 那么第一个和第三个科目已经完成
那么就可以遍历所有状态,然后在每个状态下遍历所有任务
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 1<<15
struct node
{
    int pre;//dp[i]前一个状态,从而能在输出的时候通过递归输出
    int cost;//dp[i]到i状态时的所花时间
    int reduce;//dp[i]到i状态时的所减的最短时间
} dp[maxn];
struct node1
{
    char name[110];
    int deadline;
    int day;
} course[maxn];
int vis[maxn];
int N;
void DP()
{
    memset(vis,0,sizeof(vis));
    int i;
    int j;
    dp[0].cost=0;
    dp[0].pre=-1;
    dp[0].reduce=0;
    vis[0]=1;
    int upper=(1<<N)-1;
    for(i=0; i<upper; i++) //遍历所有状态
    {
        for(j=0; j<N; j++)
        {
            int cur=1<<j;
            if((i&cur)==0)//构成能做第j个作业的条件
            {
                int curtemp=cur|i;//当前状态做了第j个作业后的状态
                int day=dp[i].cost+course[j].day;
                dp[curtemp].cost=day;
                int reduce=day-course[j].deadline;
                if(reduce<0)reduce=0;
                reduce+=dp[i].reduce;
                if(vis[curtemp]&&reduce<dp[curtemp].reduce)
                {
                    dp[curtemp].reduce=reduce;
                    dp[curtemp].pre=i;
                }
                if(vis[curtemp]==0)
                {
                    vis[curtemp]=1;
                    dp[curtemp].reduce=reduce;
                    dp[curtemp].pre=i;
                }
            }
        }
    }
}
void output(int cur)
{
    int curtemp=cur^dp[cur].pre;
    int cour=-1;
    while(curtemp)
    {
        cour++;
        curtemp>>=1;
    }
    if(dp[cur].pre!=0)
        output(dp[cur].pre);
    printf("%s\n",course[cour].name);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        int i;
        for(i=0; i<N; i++)
            scanf("%s%d%d",course[i].name,&course[i].deadline,&course[i].day);
        DP();
        int temp=(1<<N)-1;
        printf("%d\n",dp[temp].reduce);
        output((1<<N)-1);
    }
    return 0;
}

我改过的代码输出了中间量(太难懂了,对我这个dp菜鸟)

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 1<<15
struct node
{
    int pre;//dp[i]前一个状态,从而能在输出的时候通过递归输出
    int cost;//dp[i]到i状态时的所花时间
    int reduce;//dp[i]到i状态时的所减的最短时间
}dp[maxn];
struct node1
{
    char name[110];
    int deadline;
    int day;
} course[maxn];
int vis[maxn];
int N;
void DP()
{
    memset(vis,0,sizeof(vis));
    int i;int j;
    dp[0].cost=0;
    dp[0].pre=-1;
    dp[0].reduce=0;
    vis[0]=1;
    int upper=(1<<N)-1;
    for(i=0;i<upper;i++)//遍历所有状态
    {
        printf("*{{{{\ni==%d\n\n",i);
        for(j=0;j<N;j++)
        {
            printf("&{{{{\n j==%d\n",j);
            int cur=1<<j;
            if((i&cur)==0)//构成能做第j个作业的条件
            {
                int curtemp=cur|i;//当前状态做了第j个作业后的状态
                printf("1 curtemp==%d  i==%d  cur==%d\n",curtemp,i,cur);
                int day=dp[i].cost+course[j].day;
                printf("2 day==%d  =  dp[%d].cost==%d  +  course[%d].day==%d\n",day,i,dp[i].cost,j,course[j].day);
                dp[curtemp].cost=day;
                printf("3 dp[%d].cost==%d\n",curtemp,dp[curtemp].cost);
                int reduce=day-course[j].deadline;
                printf("4 reduce==%d  =  day==%d, -  course[%d].deadline==%d\n",reduce,day,j,course[j].deadline);
                if(reduce<0)reduce=0;
                printf("5 reduce==%d + ",reduce);
                reduce+=dp[i].reduce;
                printf("dp[%d].reduce==%d  =  reduce==%d  dp[%d].reduce==%d\n",i,dp[i].reduce,reduce,curtemp,dp[curtemp].reduce);
                if(vis[curtemp]&&reduce<dp[curtemp].reduce)
                {
                   dp[curtemp].reduce=reduce;
                   dp[curtemp].pre=i;
                   printf("6 dp[%d].reduce==%d, dp[%d].pre=%d\n",curtemp,reduce,curtemp,i);
                }
                if(vis[curtemp]==0)
                {
                    vis[curtemp]=1;
                    dp[curtemp].reduce=reduce;
                    dp[curtemp].pre=i;
                     printf("7 dp[%d].reduce==%d, dp[%d].pre=%d\n",curtemp,reduce,curtemp,i);
                }
            }
            printf("&}}}}\n");
        } printf("*}}}}\n");
    }
}
void output(int cur)
{
    int curtemp=cur^dp[cur].pre;
    int cour=-1;
    while(curtemp)
    {
        cour++;
        curtemp>>=1;
    }
    if(dp[cur].pre!=0)
    output(dp[cur].pre);
    printf("%s\n",course[cour].name);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        int i;
        for(i=0;i<N;i++)
            scanf("%s%d%d",course[i].name,&course[i].deadline,&course[i].day);
        DP();
        int temp=(1<<N)-1;
        printf("%d\n",dp[temp].reduce);
        output((1<<N)-1);
    }
    return 0;
}
最后是dfs剪枝代码:(这个剪枝也不好弄)

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node{
    int t,deadline;
    char s[105];
}ar[16];
int n,m,Min,vis[16],path[16],ans[16],dp[70000];
int d[]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
int cmp(node a,node b)
{
    return strcmp(a.s,b.s)<0;
}
void dfs(int i,int day,int fen,int num)
{
    int j,point;
    if (i==n)
    {
        if(dp[m]<Min)
        {
            Min=dp[m];
            memcpy(ans,path,sizeof(ans));
        }
        return;
    }
    for (j=0;j<n;j++)
    {
        if(!vis[j])
        {
            vis[j]=1;
            point=day+ar[j].t-ar[j].deadline;
            if(point<0)point=0;
            num+=d[j];
            fen+=point;
            if(fen<dp[num])
            {
                dp[num]=fen;
                path[i]=j;
                dfs(i+1,day+ar[j].t,fen,num);
            }
            fen-=point;
            num-=d[j];
            vis[j]=0;
        }
    }
}

int main()
{
    int T,i;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        for (i=0;i<n;i++)
            scanf("%s%d%d",ar[i].s,&ar[i].deadline,&ar[i].t);

        for (i=0,m=0;i<n;i++)m+=d[i];
        sort(ar,ar+n,cmp);
        memset(vis,0,sizeof(vis));
        memset(dp,0x3f3f3f3f,sizeof(dp));
        Min=0x3f3f3f3f;
        dfs(0,0,0,0);
        printf("%d\n",Min);
        for (i=0;i<n;i++)
            printf("%s\n",ar[ans[i]].s);
    }
    return 0;
}

我的dfs+剪枝:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int INF = 1e8;
const int inf = 0x3f3f3f3f;
const int maxx = 10100;
const int N = 1000;
int n;
map<int,string>ma;
struct node
{
    int num,limit,day;
} p[20];
int ans[20],anss,um;
int a[20];
int v[20];
int dp[70000];
int d[]= {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
int max(int b,int c)
{
    if(b>c)
        return b;
    return c;
}
void dfs(int l,int k,int sum)
{
    if(sum>=anss)
        return ;
    if(l==n)
    {
        anss=sum;
        memcpy(ans,a,sizeof(ans));
        return ;
    }
    for(int i=0; i<n; i++)
    {
        if(!v[i])
        {
            v[i]=1;
            um+=d[i];
            int m=max(k+p[i].day-p[i].limit,0)+sum;
            if(m<dp[um])
            {
                a[l]=i;
                dp[um]=m;
                dfs(l+1,k+p[i].day,m);
            }
            um-=d[i];
            v[i]=0;
        }
    }
    return ;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,inf,sizeof(dp));
        memset(v,0,sizeof(v));
        memset(a,-1,sizeof(a));
        memset(ans,-1,sizeof(ans));
        anss=inf,um=0;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            cin>>ma[i]>>p[i].limit>>p[i].day;
            p[i].num=i;
        }
        dfs(0,0,0);
        cout<<anss<<endl;
        for(int i=0; i<n; i++)
            cout<<ma[ans[i]]<<endl;
    }
}

没想到这道题的dp(15ms)居然比dfs(280ms)快这么多。dp确实有必要学一下了。













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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值