poj1042

本文探讨了一种算法,帮助钓鱼爱好者John在有限时间内选择最优的钓鱼路线,以最大化捕获鱼的数量。通过考虑每条路线的时间成本、初始鱼类数量以及鱼类随时间减少的速率,该算法提供了一个详细的步骤来规划钓鱼行程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gone Fishing
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 24255 Accepted: 7082

Description

John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,...,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=192). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >= 0 ), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di (di >= 0). If the number of fish expected to be caught in an interval is less than or equal to di , there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch. 
Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.

Input

You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.

Output

For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected. 
If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.

Sample Input

2 
1 
10 1 
2 5 
2 
4 
4 
10 15 20 17 
0 3 4 3 
1 2 3 
4 
4 
10 15 50 30 
0 3 4 3 
1 2 3 
0 

Sample Output

45, 5 
Number of fish expected: 31 

240, 0, 0, 0 
Number of fish expected: 480 

115, 10, 50, 35 
Number of fish expected: 724 

Source

 
 1 //greedy + enum
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int n,h;
11     int originF[26],f[26],t[26],d[26];
12     int maxSumFish,timeLake[26];
13 
14     while(scanf("%d",&n)!=EOF && n)
15     {
16         //init input
17         scanf("%d",&h);
18         h*=60;
19         memset(originF,0,sizeof(originF));
20         for(int i=1; i<=n; i++)
21         {
22             scanf("%d",&originF[i]);
23         }
24         for(int i=1; i<=n; i++)
25         {
26             scanf("%d",&d[i]);
27         }
28         int tmpt;
29         t[1]=0;
30         for(int i=2; i<=n; i++)
31         {
32             scanf("%d",&tmpt);
33             t[i]=t[i-1]+tmpt;
34         }
35         //分别计算在1...i湖钓鱼的最大值
36         maxSumFish=-1;
37         for(int i=1; i<=n; i++)
38         {
39             memcpy(f,originF,(i+1)*sizeof(originF[0]));
40             int timeLeft=h-t[i]*5;
41             int sumFish=0;
42             int tmpTimeLake[26];
43 
44             memset(tmpTimeLake,0,sizeof(tmpTimeLake));
45             //直到把时间取完
46             while(timeLeft>0)
47             {
48                 int chosenLake=1, getFish=0;
49                 for(int j=1; j<=i; j++)
50                 {
51                     if(f[j]>getFish)
52                     {
53                         getFish=f[j];
54                         chosenLake=j;
55                     }
56                 }
57                 tmpTimeLake[chosenLake]+=5;
58                 sumFish+=getFish;
59                 f[chosenLake]-=d[chosenLake];
60                 timeLeft-=5;
61             }
62             //比较是否为最好情况
63             if(sumFish>maxSumFish)
64             {
65                 maxSumFish=sumFish;
66                 memcpy(timeLake,tmpTimeLake,sizeof(tmpTimeLake));
67             }
68         }
69         //输出
70         for(int i=1;i<n;i++)
71             printf("%d, ",timeLake[i]);
72         printf("%d\n",timeLake[n]);
73         printf("Number of fish expected: %d\n\n",maxSumFish);
74 
75 
76 
77     }
78     return 0;
79 }

 

 

#include<iostream>
#include<string>
#include<cstring>
#include <cmath>
#include<cstdio>
#include<algorithm>
#define Max(a,b) a>b?a:b;
using namespace std;
int ti[30],fi[30][400],di[30],dp[30][400];
int h ,n,i,j,k;

int main()
{

    while(~scanf("%d",&n)&&n)
    {
        memset(fi,0,sizeof(fi));
        scanf("%d",&h);
        h=h*12;//共h个五分钟
        for(i=1;i<=n;i++)//i=1
        scanf("%d",&fi[i][1]);

        for(i=1;i<=n;i++)
        scanf("%d",&di[i]);

        for(i=2;i<=n;i++)//i=2
        scanf("%d",&ti[i]);
        ti[1]=0;

        for(i=1;i<=n;i++)
        {
           for(k=2;k<=h;k++)
           {
               if(fi[i][k-1]<=di[i])break;
               fi[i][k]=fi[i][k-1]-di[i];
           }
        }

       memset(dp,-1,sizeof(dp));
       dp[0][0]=0;
       int sum,tt=0;
       for(i=0;i<n;i++)
       {
           for(j=0;j<=h;j++)
           {   sum=0;
               for(k=0;k<=h&&dp[i][j]!=-1;k++)//!!!**没想到这小细节还是关键来
               {
                  if(j+ti[i+1]+k>h)break;
                  dp[i+1][j+ti[i+1]+k]=Max(dp[i][j]+sum,
                         dp[i+1][j+ti[i+1]+k]);
                 if(fi[i+1][k+1]>0)
                  sum+=fi[i+1][k+1];
               }
           }
       }
        int mark=1,MAX=0;
        for(i=1;i<=n;i++)
        {
            if(MAX<dp[i][h])
            {
                MAX=dp[i][h];
                mark=i;
            }
        }
//cout<<"MAX="<<MAX<<endl;
       int MMAX=MAX;
        for(i=mark;i>=2;i--)
        {
           int sum=0;
           for(k=0;k<=h;k++)
           {
               if(MAX==sum+dp[i-1][h-k-ti[i]])
               {
                   fi[i][0]=k;
                   break;
               }
               sum=sum+fi[i][k+1];
           }
           h=h-ti[i]-k;
           MAX-=sum;//开始漏了
        }

        fi[1][0]=h;
        for(i=1;i<n;i++)
        printf("%d, ",fi[i][0]*5);
        printf("%d\n",fi[i][0]*5);
        printf("Number of fish expected: %d\n\n",MMAX);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/eric-blog/archive/2012/08/19/2646851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值