poj1042 Gone Fishing

http://poj.org/problem?id=1042

 

Topic: John is going to go fishing. He has h hours available (1 <= h <= 16), the area has n lakes (2 <= n <= 25), and can be reached along a one-way street. John starts from Lake 1, but he can end at any lake he wants. He can only go from lake to lake, but he doesn't need to stop at any of them unless he wants to. For each i = 1,...n - 1, the 5-minute interval from lake i to lake i + 1 is ti (0 < ti <= 192). For example, t3 = 4 means it takes 20 minutes to go from lake 3 to lake 4. To help plan his fishing trip, John gathered some information about the lake. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi (fi >= 0), is known. Fishing every 5 minutes reduces the number of fish expected to be caught in the next 5 minutes (di >= 0). If the number of fish expected to be caught during the interval is less than or equal to di, then no more fish will remain in the lake during the next period. To simplify the plan, John assumes that no one will be fishing at the lake to influence the number of fish he wants to catch.

Write a program that helps John plan his fishing trip to maximize the number of fish caught. The number of minutes spent on each lake must be a multiple of 5.

 

That is to say, John wants to catch the most fish, but there are n lakes, and it will take time to move between lakes, so John may have n kinds of final stop positions, we have to compare the total number of fishing in these n cases , select the largest, and then output the result. For the convenience of expression, we can take fragmented fishing. First, subtract the time required to reach the pth (p<=n) from the total time, then the remaining time is the time used for fishing. Assuming that John can move instantaneously, John Select the lake with the most fish remaining to fish each time, so that the number of fish will be maximized. Of course, John definitely won't be displaced instantly, but it doesn't matter, it's enough to change from fragmented fishing to integrated fishing.

 

Algorithm idea: Greedy algorithm, since John may stop at any position of n lakes, we have to consider n situations and choose the one with the most total fishing. In each case, greedily go to the lake that can catch the most fish, and if all the fish are caught, stay in the first lake (this is the case). Obviously, every time we are greedy, we have to compare and select the lake with the most remaining fish for fishing. After the fishing is finished, the corresponding expected number of fishing is reduced.

 

#include <iostream>
#include <xmemory>
#include <climits>
#define N 25
using namespace std;
int main(void) 
{
    int n, h;
     int fi[N], di[N], ti[N]; // fi[] expected fish count, di[] decrease fish count every five minutes, ti[] displacement time 
    int time[N] ; // The time to stay in each lake when the current lake stops 
    int best[N]; // The time to stay in each lake corresponding to the optimal solution 
    int bestFishNum; // The total number of fishes for the optimal solution 
    int fishNum; / / current total fishing 
    while (cin >> n && n)
    {
        cin >> h;
        h *= 12 ; // There are h 5 minutes in total 
        for ( int i = 0 ; i < n; i++) cin >> fi[i]; // Expected number of fishing 
        for ( int i = 0 ; i < n; i++ ) cin >> di[i]; // decrease the number of fish every five minutes 
        for ( int i = 0 ; i < n - 1 ; i++) cin >> ti[i]; // shift time 
        bestFishNum = INT_MIN;
        memset(best , 0 , sizeof best);
         for ( int p = 0 ; p < n; p++) { // Exhaust the position of the lake that finally stopped 
            memset(time , 0 , sizeof (time));
            fishNum = 0 ;
             int remainingTime = h; // current remaining time 
            int remainingFish[N]; // current number of fish remaining in each lake 
            for ( int i = 0 ; i < n; i++ )
                remainingFish[i] = fi[i];//初始化
            for (int j = 0; j < p; j++)
                remainingTime -= ti[j]; // subtract the distance first, then assume that John can move freely between lake 0 and lake i 
            while (remainingTime > 0 ) { // until time runs out 
                int max = INT_MIN;
                 int pos = 0 ; // The current position that can fish the most 
                for ( int j = 0 ; j <= p; j++) // The position that can fish the most every time 
                    if (remainingFish[j] > max)
                    {
                        max = remainingFish[j];
                        pos = j; // find position 
                    }
                time[pos] ++; // corresponding time ++ 
                fishNum += remainingFish[pos]; // accumulated fish count 
                remainingFish[pos] =remainingFish[pos]- di[pos]> 0 ?remainingFish[pos]-di[ pos]: 0 ;
                remainingTime--;
            }
            if (fishNum > bestFishNum)
            {
                bestFishNum = fishNum;
                for (int i = 0; i < n; i++)
                {
                    best[i] = time[i];
                 }
            }
        }
        for (int i = 0; i < n - 1; i++)
            cout << best[i] * 5 << ", ";
        cout << best[n - 1] * 5 << endl;//注意输出格式
        cout << "Number of fish expected: " << bestFishNum << endl << endl;
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324973272&siteId=291194637