Poj1042_ Greedy + Enumeration

Source: Internet
Author: User

Reference: http://file:///home/foogle/%E4%B8%8B%E8%BD%BD/firefox/blog_63509b890100ndjr.html

title : poj1042

Algorithm : enumeration + Greedy

Test Instructions :

John has an H-hour free time and he is going fishing. There are a total of N lakes in the area of fishing, and all the lakes are arranged in a one-way way (John has to go to the next lake to catch fish after he has fished at a lake), and John has to start fishing from Lake 1th, but he can end his fishing trip at any lake.

This problem takes 5 minutes as the unit time, and John decreases linearly as the number of fish fished every 5 minutes in each lake increases over time. The number of fish that can be caught in the first 5 minutes of each lake is indicated by the decrease in the number of fishing in each lake adjacent to 5 minutes, expressed by Di, when John walks from any lake to its next lake with TI.

A scheme that allows John to catch as many fish as possible in a limited h-hour.

Ideas:

The first thing to note is that John can only walk forward, and the words returned will only increase John's time on the road, thus reducing the time he is fishing. So this problem is as follows:

1. Enumerate the last lake that John passed, minus the time required to walk in each case, and the rest is the time of the fishing.

2, every 5 minutes to choose the highest amount of fishing lake fishing, until the time is exhausted.

3, in the case of all enumerations to choose the most amount of fishing, that is, the problem of the optimal solution.

Several issues to be noted in this topic:

1, if the solution is not unique, choose the solution that takes the most time in the first lake; if there is still a inflexible solution, choose the solution that takes the most time in the second lake, and so on.

2, with the increase in time, the number of fishing in a lake may appear negative, at this time, the lake should be updated to zero the number of per-hour units of fishing.

3, in the process of testing data or procedures may appear in each lake the total number of fish is 0, pay attention to special treatment.

4, enumeration, the end of the flag is the remaining time <=0.

#include <stdio.h> struct Node {int max; int time[30];//save on each island spend time}land[30];//save possible solution int fi[30];//initial fish amount int cfi[30];//to fi array save int di[30];//unit time Fish reduction int ti[30]
        ;//To the next Lake time int h;//available time int n;//Lake number int Getmax (int m[],int i,int j)//Return all M islands with the largest number of fishes in the island (int maxn=m[i];
        int loc=i;
        int t;
                        for (t=i+1;t<=j;t++) {if (Maxn<m[t]) {maxn=m[t];
                loc=t;
}} return loc;
        } int getfish () {int t=h*60;
        int copt,count;
        int i,j;
                for (i=1;i<=n;i++)//Initialize struct-body {land[i].max=0;
        for (j=1;j<=n;j++) land[i].time[j]=0;
                } for (i=1;i<=n;i++)//Enumerate over the location of Lake {copt=t;
                count=0;
 for (j=1;j<=n;j++) {cfi[j]=fi[j];//each new lake will have to copy the initial fi                       copt= (i>j)? copt-ti[j]*5:copt;//remove the time to move the island every time} while (Count<co PT) {int K=getmax (cfi,1,i);//Find out the number of lakes that can be transferred to the most fish in the lake LAND[I].M 
                        ax+=cfi[k];//End Lake for I when the total number of fishing plus K Lake can be adjusted to the value of the fish land[i].time[k]+=5;//stop at K Lake time plus 5 count+=5;
                        if (Cfi[k]>=di[k]) cfi[k]-=di[k]; Else cfi[k]=0;//Modify the number of fish that can be transferred per unit time in Lake K}} for (i=1;i<=n;i++
        ) Cfi[i]=land[i].max;
        int Maxfish=getmax (CFI,1,N); for (i=1;i<= n;i++) {(I! = N)? printf ("%d,", Land[maxfish].time[i]):p rintf ("%d", Land[maxfish].ti
        Me[i]);
} printf ("\nnumber of fish expected:%d\n\n", Land[maxfish].max);
        } int main (void) {int i;
        while ((scanf ("%d", &n) &&n)) {        scanf ("%d", &h);
                for (i=1;i<=n;i++) scanf ("%d", &fi[i]);
                for (i=1;i<=n;i++) scanf ("%d", &di[i]);
                for (i=1;i<n;i++) scanf ("%d", &ti[i]);
        Getfish ();
} return 0;
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.