zoukankan      html  css  js  c++  java
  • 状压dp

    hdoj-1074:

    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

    题意就是:有n个作业每个作业有一个最后完成时间和一个需要的时间,如果超过最后完成的时间,超过一天扣一分,求最少要扣多少分

    思路:根据题意 n最多有15,所以把状态压缩成2进制,每一位0表示还没有选 1 表示选了,状态转移就是 前一个转态到后一个 根据二进制的性质

    ,最后还要输出路径,所以要 保留他,用一个结构体保留他的前驱和他由前驱到现在 需要完成那个作业;

    代码;

     1 #include <stdlib.h>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<bits/stdc++.h>
     5 #include<cstdio>
     6 #include<cstring>
     7 #include<cmath>
     8 #define ll long long
     9 const ll INF=0x3f3f3f3f;
    10 #define mod 1000000007
    11 #define mem(a,b) memset(a,b,sizeof(a))
    12 #define  INF 0x3f3f3f3f
    13 //__builtin_popcount
    14 using namespace std;
    15 //priority_queue
    16 const ll MAX=100000;
    17 struct A
    18 {
    19     string name;
    20     int a,b;
    21 } s[30];
    22 struct B
    23 {
    24     int time,pre,cot,id;//
    25 } dp[1<<15+3];
    26 void prin(int z)
    27 {
    28     if(z)
    29     {
    30         prin(dp[z].pre);
    31         cout<<s[dp[z].id].name<<endl;
    32     }
    33 }
    34 int main()
    35 {
    36     int t;
    37     cin>>t;
    38     while(t--)
    39     {
    40         mem(dp,0);
    41         int n;
    42         int m;
    43         cin>>n;
    44         m=1<<n;
    45         for(int i=1; i<=n; i++)
    46         {
    47             cin>>s[i].name>>s[i].a>>s[i].b;
    48         }
    49         for(int i=1; i<m; i++)
    50         {
    51             dp[i].cot=INF;
    52             for(int j=0; j<n; j++)
    53             {
    54                 if(((1<<j)&i)==0||(1<<j)>i)continue;
    55                 int pre=i-(1<<j);
    56                 int tt=dp[pre].time+s[j+1].b-s[j+1].a;
    57                 tt=max(tt,0);
    58                 if(dp[pre].cot+tt<=dp[i].cot)
    59                 {
    60                     dp[i].cot=dp[pre].cot+tt;
    61                     dp[i].id=j+1;
    62                     dp[i].time=dp[pre].time+s[j+1].b;
    63                     dp[i].pre=pre;
    64                 }
    65             }
    66         }
    67         cout<<dp[m-1].cot<<endl;
    68         prin(m-1);
    69     }
    70 }
    View Code
  • 相关阅读:
    MyBatis 配置文件 用户密码加密存储
    MyBatis 实例
    mybatis 入门搭建
    文件上传
    struts 结果类型
    Struts 拦截器
    log4j
    Struts Action 控制器
    Struts
    四、常用的Maven命令
  • 原文地址:https://www.cnblogs.com/zxz666/p/11195723.html
Copyright © 2011-2022 走看看