poj 1042 Gone Fishing(DP)

来源:互联网 发布:蚁群算法解决tsp问题 编辑:程序博客网 时间:2024/06/04 23:25
【题目大意】:john现有h个小时的空闲时间,他打算去钓鱼。john钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(john每在一个湖钓完鱼后,他只能走到下一个湖继续钓),john必须从1号湖开始钓起,但是他可以在任何一个湖结束他此次钓鱼的行程。输入给出john在每个湖中每5分钟钓的鱼数(此题中以5分钟作为单位时间),随时间的增长而线性递减。而每个湖中头5分钟可以钓到的鱼数以及每个湖中相邻5分钟钓鱼数的减少量,John从任意一个湖走到它下一个湖的时间。 

【解题思路】:1)设fish[i][j]表示到了第i个池塘用了j*5分钟所钓到的雨的数目
               2)转移:fish[i][j]=Max{fish[i-1][j-k-t[i]],fish[i][j]+sum}

                     sum是呆着这个池塘经过k*5分钟所钓到的鱼的数目~sum=f[i]*(k+1)-(k+1)*k/2*d[i]

                                     -_-!去年写的题目了,刚发现...一样的...


【代码】:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <cmath>#include <string>#include <cctype>#include <map>#include <iomanip>                   using namespace std;                   #define eps 1e-8#define pi acos(-1.0)#define inf 1<<30#define linf 1LL<<60#define pb push_back#define lc(x) (x << 1)#define rc(x) (x << 1 | 1)#define lowbit(x) (x & (-x))#define ll long longint f[30],d[30],t[30],path[30];int fish[30][400];int n,h,maxx,k;void init(){    memset(path, 0, sizeof(path));    memset(fish,-1,sizeof(fish));}void find_Path(int i, int time){if(i==0) return;int summ=0;for(int k=0; k<=12*h; ++k){if(fish[i-1][time-k-t[i-1]]+summ==fish[i][time]){path[i]=k*5;return find_Path(i-1,time-k-t[i-1]);}        if (f[i]-k*d[i]>0)            summ=f[i]*(k+1)-(k+1)*k/2*d[i];}    return;}void get_DP(){fish[0][0]=0;for(int i=0; i<=n-1; ++i)for(int j=0; j<=12*h; ++j){            int summ=0;            for(int k=0; k<=12*h && fish[i][j]!=-1; ++k){                if(j+k+t[i]<=12*h)                    fish[i+1][j+k+t[i]]=max(fish[i+1][j+k+t[i]],fish[i][j]+summ);                else                    break;                if (f[i+1]-k*d[i+1]>0)                   summ=f[i+1]*(k+1)-(k+1)*k/2*d[i+1];            }        }        maxx=0;        k=1;        for(int i = 1; i <= n; ++i)            if(maxx<fish[i][12*h])            {                maxx=fish[i][12*h];                k=i;            }}int main(){    while (~scanf("%d",&n)){        if (n==0) break;        scanf("%d",&h);        for (int i=1; i<=n; i++) scanf("%d",&f[i]);        for (int i=1; i<=n; i++) scanf("%d",&d[i]);        for (int i=1; i<=n-1; i++) scanf("%d",&t[i]);        init();        get_DP();        find_Path(k, 12*h);        for(int i = 1; i <= n-1; ++i)            printf("%d, ",path[i]);        printf("%d\n",path[n]);        printf("Number of fish expected: %d\n", fish[k][12*h]);        printf("\n");    }    return 0;}