链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074
题意:给出每个科目作业完成需要花费的时间,提交作业的最后时间,超过时间要扣分,输出扣分最少的方案。
思路:1到1<<n 二进制代表所有科目完成的状态,遍历每种状态已完成的科目,找到达到这种状态扣分最少的方案并记录路径;(题目要求如果扣分相同的情况下要先完成先出现的科目。)
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
struct node
{
char name[110];
int deadline;
int cost;
}a[20];
const int maxn=1<<15|10;
const int INF=0x3f3f3f3f;
int timen[maxn],dp[maxn],path[maxn];
void out(int x)
{
if(x==0)
return;
out(x-(1<<path[x]));
printf("%s\n",a[path[x]].name);
}
int main()
{
int t,n; scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(timen,0,sizeof(timen));
for(int i=0;i<n;i++)
scanf("%s%d%d",a[i].name,&a[i].deadline,&a[i].cost);
int choice=1<<n;
for(int i=1;i<choice;i++)
{
dp[i]=INF;
for(int j=0;j<n;j++)
{
int num=1<<j;
if(!(i&num)) continue;
int score=timen[i-num]+a[j].cost-a[j].deadline;
if(score<0) score=0;
if(dp[i]>=dp[i-num]+score)
{
dp[i]=dp[i-num]+score;
timen[i]=timen[i-num]+a[j].cost;
path[i]=j;
}
}
}
printf("%d\n",dp[choice-1]);
out(choice-1);
}
return 0;
}