POJ 1042 Gone Fishing

Gone Fishing

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) :    Accepted Submission(s) : 
Problem 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
PKU
 黑书1.2.2 例1

枚举在每个湖结束钓鱼的情况,去掉移动的时间。这样在各个湖之间的移动就可以看成瞬移的了,贪心可以很容易的求出最大解。

  1 #include <iostream>
  2 #include <cstring>
  3 
  4 #define ninf -99999999
  5 
  6 using namespace std;
  7 
  8 int n,h;
  9 int f[28][200];
 10 int nf[200];
 11 int tf[200];
 12 int d[28];
 13 int t[28];
 14 
 15 int main()
 16 {
 17     while(cin>>n&&n)
 18     {
 19         cin>>h;
 20         for(int i=1;i<=n;i++)
 21             cin>>f[i][1];
 22         for(int i=1;i<=n;i++)
 23             cin>>d[i];
 24         for(int i=1;i<=n-1;i++)
 25             cin>>t[i];
 26 
 27         for(int i=0;i<200;i++)
 28             nf[i]=1;
 29         memset(tf,0,sizeof(tf));
 30 
 31         for(int i=1;i<=n;i++)
 32         {
 33             for(int j=2;j<=h*12;j++)
 34                {
 35                    f[i][j]=f[i][j-1]-d[i];
 36                    if(f[i][j]<0)
 37                     f[i][j]=0;
 38                }
 39         }
 48         int nums;
 49         int times=h*12;
 50         for(nums=1;nums<=n-1&&times>0;nums++)
 51         {
 52             times-=t[nums];
 53         }
 54         if(times<=0)///有两种方式跳出,注意判断
 55              nums--;///最多可以停在NUMS上
 57 
 58         int fish=0;
 59         int Maxing=ninf;
 60         int times2=-1;
 61         for(int i=1;i<=nums;i++)
 62         {
 63             int waste=0;
 64             for(int j=1;j<=i-1;j++)
 65                 waste+=t[j];
 66             times=h*12-waste;
 67             fish=0;
 68             for(int j=0;j<200;j++)
 69                 nf[j]=1;
 70 
 71             ///greed
 72 
 73             int sum;
 74             for(int j=1;j<=times;j++)
 75             {
 76                 sum=ninf;
 77                 int nth=-1;
 78                 for(int k=1;k<=i;k++)
 79                 {
 81                     if(f[k][nf[k]]>sum&&f[k][nf[k]]>=0)
 82                     {
 83                         sum=f[k][nf[k]];
 84                         nth=k;
 85                     }
 86                 }
 87                 if(sum>0)
 88                 {
 89                     nf[nth]++;
 90                     fish+=sum;
 91                 }
 92             }
 93             if(Maxing<fish)
 94             {
 95                 Maxing=fish;///最多可以钓的鱼
 96                 times2=i;///应该在TIMES2号湖上结束钓鱼
 97             }
100         }
101 
105         memset(tf,0,sizeof(tf));
106 
107         for(int i=0;i<200;i++)
108             nf[i]=1;
109 
110         fish=0;
111         int waste=0;
112         for(int o=1;o<=times2-1;o++)
113             waste+=t[o];
114         times=h*12-waste;
117         int sum=ninf;
118         memset(tf,0,sizeof(tf));
119         for(int j=1;j<=times;j++)///TIMES次钓鱼
120         {
121             sum=ninf;
122             int nth=-1;
123             for(int k=1;k<=times2;k++)
124             {
127                 if(f[k][nf[k]]>sum&&f[k][nf[k]]>=0)
128                 {
129                     sum=f[k][nf[k]];
130                     nth=k;
131                 }
132             }
133             if(sum>0)
134             {
135                 nf[nth]++;
136                 tf[nth]++;
137                 fish+=sum;
138             }
139             else if(sum==0)
140             {
141                 tf[1]++;
142             }
143         }
144 
151         for(int i=1;i<=n;i++)
152           {
153               cout<<tf[i]*5;
154               if(i!=n)
155                 cout<<", ";
156           }
157         cout<<endl;
158         cout<<"Number of fish expected: "<<Maxing<<endl<<endl;
159 
160 
161     }
162 
163     return 0;
164 }
原文地址:https://www.cnblogs.com/CKboss/p/3047408.html