hdu 1074 Doing Homework(상 압+기록 경로)
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7316 Accepted Submission(s): 3240
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
题目大意:
有n门课程,每门课程有个作业。这个作业有个最晚上交时间和完成这门课需要的时间。假如没有按照规定时间完成,每晚一天减少一分。
思路:枚举(1<<n)-1种状态,一共n门课程,对每种课程的状态,判断该课程(1<<i)能否右(1--(1<<n))种状态中的一个状态得到,然后dp记录学习了当前课程所花费的时间,和上一门课程的是哪门
有n门课程,每门课程有个作业。这个作业有个最晚上交时间和完成这门课需要的时间。假如没有按照规定时间完成,每晚一天减少一分。
最后DFS回溯输出路径
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define N 20
#define INF 0x3f3f3f3f
#define MOD 2009
#define met(a, b) memset (a, b, sizeof(a))
typedef long long LL;
struct node
{
char str[N];
int deadline;///
int cost;///
}stu[N];
struct node1
{
int time;///
int score;///
int now;///
int pre;///
}dp[1<<16];
void Search_path (int n)
{
if (!n) return;
Search_path (dp[n].pre);
printf ("%s
", stu[dp[n].now].str);
return;
}
int main ()
{
int t, n;
scanf ("%d", &t);
while (t--)
{
met (dp, 0);
met (stu, 0);
scanf ("%d", &n);
for (int i=0; i<n; i++)
scanf ("%s%d%d", stu[i].str, &stu[i].deadline, &stu[i].cost);
int Lim = (1<<n)-1;
for (int i=1; i<=Lim; i++)
{
dp[i].score =INF;
for (int j=n-1; j>=0; j--)
{
if (i & (1<<j))
{
int x = i - (1<<j);
int Score = dp[x].time + stu[j].cost - stu[j].deadline;
if (Score < 0) Score = 0;
if (Score + dp[x].score < dp[i].score)
{
dp[i].score = Score + dp[x].score;
dp[i].time = dp[x].time + stu[j].cost;
dp[i].pre = x;
dp[i].now = j;
}
}
}
}
printf ("%d
", dp[Lim].score);
Search_path (Lim);
}
return 0;
}
기억 화 검색
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define N 20
#define INF 0x3f3f3f3f
#define MOD 2009
#define met(a, b) memset (a, b, sizeof(a))
typedef long long LL;
struct node
{
char str[N];
int deadline;///
int cost;///
}stu[N];
struct node1
{
int time;///
int score;///
}dp[1<<16];
int n;
void Solve (node1 &A, node1 B, node C)
{
int Time = B.time + C.cost;
int Score = B.score + abs(min(C.deadline-Time, 0));
if (A.score > Score|| (A.score==Score && A.time<Time))
{
A.score = Score;
A.time = Time;
}
}
node1 DFS (int Sta)
{
if (dp[Sta].score != -1) return dp[Sta];
dp[Sta].score = INF;
for (int i=0; i<n; i++)
{
if (Sta & (1<<i))
Solve (dp[Sta], DFS (Sta-(1<<i)), stu[i]);
}
return dp[Sta];
}
bool OK (node1 A, node1 B, node C)
{
int Time = B.time + C.cost;
int Score = B.score + abs (min (C.deadline-Time, 0));
if (A.score == Score && A.time == Time)
return true;
return false;
}
void Search_path (int Sta)
{
if (!Sta) return;
for (int i=n-1; i>=0; i--)
{
if (Sta & (1<<i) && OK (dp[Sta], dp[Sta-(1<<i)], stu[i]))
{
Search_path(Sta-(1<<i));
puts (stu[i].str);
break;
}
}
}
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
for (int i=0; i<n; i++)
scanf ("%s%d%d", stu[i].str, &stu[i].deadline, &stu[i].cost);
met (dp, -1);
dp[0].score = dp[0].time = 0;
node1 A = DFS ((1<<n)-1);
printf ("%d
", A.score);
Search_path((1<<n)-1);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.