Marathon - ZOJ 2683 dp

来源:互联网 发布:网络综合布线设计方案 编辑:程序博客网 时间:2024/06/07 16:55

Marathon

Time Limit: 2 Seconds      Memory Limit: 65536 KB

A team marathon (relay race) is held in Seoul. The team marathon rules are as follows:

  • Each team consists of exactly N runners.
  • The winner is the team that runs the longest distance for D days.
  • For each day, exactly one runner from each team should run.
  • If a team member runs on a particular day, that team member should run for the entire day. That is, runner exchanges are only possible at the beginning of each day.
  • Each of the N runners on a team should run for at least one day, but at most three days.
  • If a runner participates in running for a day or for a sequence of days, and then, is switched with another runner, he/she cannot participate in the marathon again.

A team, called chic-chic-pok-pok, joins the team marathon. The coach of the team has already determined the order in which his N runners will run in D days, but has not yet decided the exact number of days each runner should run. Now, he will make that determination by examining each runner's prior running record. The running record of each runner contains a list of three numbers. The i-th (i = 1, 2, 3) number indicates the distance (in kilometers) he/she can run for a period of i consecutive day(s). For example, if the running record of a runner is (4, 7, 9), the coach assumes that the runner can run distances of 4 kilometers in one day, 7 kilometers in two days, and 9 kilometers in three days. Note that if (a, b, c) is the running record of a runner, it should satisfy that a <= b <= c.

Now, the coach wants to know the longest distance his team can run for D days. For example, suppose N = 3, D = 4, and the running records of the first, second, and third runners on team chic-chic-pok-pok are (4, 7, 8), (2, 4, 6), and (4, 5, 6), respectively. Then, the longest distance the team can run in 4 days is 13 kilometers; The first runner runs 7 km in two days (i.e., the first and the second days), the second runs 2 km in a single day (i.e., the third day), and the third runs 4 km in a single day (i.e., the fourth day).

Write a program that returns the longest distance the team can run in D days.

Input

The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case starts with a line containing an integer N, the number of runners on team-chic-chic-pok-pok, 1 <= N <= 50. The next line contains an integer D, the number of days in the marathon, 1 <= D <= 150. The next N lines contain the runners�� running records, one for each runner, from the first runner to the last runner.

Output

Print exactly one line for each test case. The line is to contain the integer that is the longest distance the team can run in D days. If it is impossible to calculate the distance due to a lack of or an inconsistency in the running records, or if there is a violation of the marathon rules, the line is to contain ?1. The following shows sample input and output for two test cases.

Sample Input

2344 7 82 4 64 5 6272 3 53 6 8

Sample Output

13-1

题意:有n个运动员,要跑m天,每个人最少跑1天,最多跑3天,告诉你他们在这3天各能跑多远,问你这个团队在比赛中最多跑多远。

思路:dp[i][j]表示第i个运动员在第j天跑完后最多能跑多远,有点类似于背包吧。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[60][160];int main(){    int t,n,m,i,j,k,a,b,c;    scanf("%d",&t);    while(t--)    {        memset(dp,-1,sizeof(dp));        dp[0][0]=0;        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++)        {            scanf("%d%d%d",&a,&b,&c);            for(j=m;j>=1;j--)            {                if(j>=1 && dp[i-1][j-1]>=0) dp[i][j]=max(dp[i][j],dp[i-1][j-1]+a);                if(j>=2 && dp[i-1][j-2]>=0) dp[i][j]=max(dp[i][j],dp[i-1][j-2]+b);                if(j>=3 && dp[i-1][j-3]>=0) dp[i][j]=max(dp[i][j],dp[i-1][j-3]+c);            }        }        printf("%d\n",dp[n][m]);    }}



0 0
原创粉丝点击