HDU 1074 doing homework (pressure DP)

Source: Internet
Author: User

Question:

Deadline that provides the time required to complete n homework and the time required to hand in N homework.

If you pay a day later than deadline, you will be deducted a point.

The order of completion is arranged to minimize the number of points deducted.


Train of Thought Analysis:

DP [s] indicates that the optimal score deducted from homework in S state is completed.


For each status, we add each unfinished course to the course during the transfer, which ensures that the course is completed one by one.


Note that the smallest Lexicographic Order is the problem. In the beginning, the input strings are sorted from small to large, and each time they are added to the State from small to large.


In this way, the minimum Lexicographic Order of the added status can be ensured.


But also the output scheme, so the ANS array saves the lessons added to the previous state and current state.


#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <set>#include <vector>#include <map>#include <string>#define maxn 1005using namespace std;int dp[1<<15];int ans[1<<15][2];int time[1<<15];struct node{    string str;    int dead,cost;    bool operator < (const node &cmp)const    {        return str<cmp.str;    }}a[20];void print(int s){    if(s==0)return;    print(ans[s][0]);    cout<<a[ans[s][1]].str<<endl;}int main(){    int T;    cin>>T;    while(T--)    {        int n;        cin>>n;        for(int i=0;i<n;i++)            cin>>a[i].str>>a[i].dead>>a[i].cost;        sort(a,a+n);        memset(dp,0x3f,sizeof dp);        memset(time,0,sizeof time);        memset(ans,0,sizeof ans);        dp[0]=0;        for(int s=0;s<(1<<n);s++)        {            for(int i=0;i<n;i++)            {                if((1<<i)&s)continue;                int c=max(0,time[s]+a[i].cost-a[i].dead);                if(dp[s]+c < dp[s|(1<<i)])                {                    dp[s|(1<<i)]=dp[s]+c;                    time[s|(1<<i)]=time[s]+a[i].cost;                    ans[s|(1<<i)][0]=s;                    ans[s|(1<<i)][1]=i;                }            }        }        cout<<dp[(1<<n)-1]<<endl;        print(ans[(1<<n)-1][0]);        cout<<a[ans[(1<<n)-1][1]].str<<endl;    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.