poj_1042_Gone Fishing

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 



==================================================================================================


枚举+贪心+优先队列


题目意思是John要去钓鱼,他有h小时的空闲时间。总共有n个鱼塘,在一条直线上。第i个鱼塘每五分钟可以钓fi条鱼,从开始在该鱼塘钓鱼开始算起,没过五分钟,该鱼塘每五分钟可以钓的鱼的数量减少di。从第i个鱼塘到第i+1个鱼塘需要ti*5分钟。


要求求出John最多可以掉多少条鱼,并且给出在每个鱼塘停留的时间(必须是5分钟的倍数),如果有多种方案,输出时间大的靠前的方案。


哎~,一开始打算用dfs ,分留在本鱼塘和前往下一鱼塘两种情况,代码都写出来了,交上去TLE ,仔细一想,尼玛这不TLE才怪。

然后发现要用贪心来做。


首先枚举以每一个鱼塘为钓鱼的终点。算出路上要花的时间,剩下的就是钓鱼时间了。因为鱼塘之间不会相互影响,说以贪心fi最大的鱼塘,又因为五分钟后fi-=di,所以,要用到优先队列进行维护,使每次渠道的都是最大的fi。


<span style="font-size:14px;">//由于每个湖中鱼的数量只与钓了多长时间有关,与什么时候钓无关。因此我们可以
//一次将移动的时间全部扣除,这样每时每刻我们都可以选择鱼数量最多的湖

#include<stdio.h>
#include<math.h>
#include<queue>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

struct MyStruct{
    int f,d;
    int sub;
    friend bool operator< (MyStruct a,MyStruct b){     // 定义优先级,fi大的优先,因为时间安排尽可能靠前,
        if (a.f==b.f) return a.sub>b.sub;              // 所以,fi相同,下标小的优先。
        else return a.f<b.f;
    }
}lake[30],lake1[30];

int main(){
    int i,j,flag=0;
    int n,h,t[30];
    int time_Move,time_Left,n_Fish,max_Fish;
    int time_Lake[30],time_Lake0[30];

    while (scanf ("%d",&n)!=EOF && n!=0){

        max_Fish=-1;
        memset (t,0,sizeof(t));
        memset (lake,0,sizeof(lake));
        memset (time_Lake0,0,sizeof(time_Lake0));

        scanf ("%d",&h);
        for (i=0;i<n;i++)
            scanf ("%d",&lake[i].f);
        for (i=0;i<n;i++)
            scanf ("%d",&lake[i].d);
        for (i=0;i<n;i++)
            lake[i].sub=i;
        for (i=0;i<n-1;i++){
            scanf ("%d",&t[i]);
            t[i]*=5;
        }

        priority_queue<MyStruct > que;

        for (int n_Lake=0;n_Lake<n;n_Lake++){                  //枚举到达每一个lake的情况
//          printf ("\n%d: ",n_Lake);

            time_Left=h*60;
            time_Move=0;                                       //初始化
            n_Fish=0;
            memset (time_Lake,0,sizeof(time_Lake));
            while(!que.empty())                                //队列清空
                que.pop();

            for (i=0;i<=n_Lake;i++){
                lake1[i].f=lake[i].f;
                lake1[i].d=lake[i].d;
                lake1[i].sub=lake[i].sub;
                //printf("%d ",lake1[i].f);
            }

            for (i=0;i<n_Lake;i++){                           //求出到第n_Lake路上要花的时间
                time_Move+=t[i];
                // que.push(lake1[i]);
            }
            
            for(i=0;i<=n_Lake;i++)
                que.push(lake1[i]);//printf ("size :%d",que.size());

            time_Left-=time_Move;//printf ("left :%d",time_Left);

            while(1){// printf ("*");

                MyStruct top_Lake=que.top();
                que.pop();
                if (time_Left-5>=0){
                    n_Fish+=top_Lake.f;
                    top_Lake.f=top_Lake.f-top_Lake.d>0?top_Lake.f-top_Lake.d:0;//让fi等于0,而不是负数,因为如果有
                    time_Left=time_Left-5;                                     //时间没鱼钓,可以把它安排在第1鱼塘。
                    time_Lake[top_Lake.sub]+=5;
                    //time_Lake[top_Lake.sub]++;
                    que.push(top_Lake);
                    //printf ("*");printf ("(%d) ",top_Lake.f);
                }
                if (time_Left-5<0){
                    break;
                }

            }
            if (n_Fish>max_Fish){
                max_Fish=n_Fish;
                for (i=0;i<=n_Lake;i++){
                    time_Lake0[i]=time_Lake[i];
                }

            }

        }
        if (flag) {
            printf ("\n");
        }
        flag=1;
        for (i=0;i<n;i++){
            if (i) printf (", ");
            printf ("%d",time_Lake0[i]);
        }
        printf ("\nNumber of fish expected: %d\n",max_Fish);
    }
    return 0;
}</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值