poj1042

题目大意:john有H个小时,他要去钓鱼。已知有N个池塘,排在一起,池塘间有间隔,从i个池塘走到i+1需要时间ti。john必须从第一个池塘开始,往2-3-4...池塘的顺序钓鱼。每个池塘刚开始每分钟调fi与,往后递减。。。求最多调多少鱼。。

我的思路:dp

  f[i][j]表示前i个池塘用j分钟所能钓到最多的鱼。。

  f[i][j] = max(f[i - 1][k] + a[i][j - k - ti]);

  a[i][j-k-ti] 表示第i个池塘给定j-k-ti分钟所能钓到的鱼、、、

  。。输出比较蛋疼。。

 还有,可知5分钟为一个单位、、先除5再说

  1 /*
  2  State:Accepted
  3  Time:2013.2.28
  4 */
  5 #include <iostream>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <string>
  9 #include <cstdio>
 10 #include <cmath>
 11 #include <algorithm>
 12 #include <vector>
 13 #define  minn -1000000;
 14 using namespace std;
 15 int h , n , first[100], decl[100] ,dt[100] , f[30][200] , a[30][200] , value[30][200];
 16 int map[30][200] , Time[30][200], ans , ansi, ansj , result[30];
 17 
 18 void init(){
 19        scanf("%d",&h);
 20        h *= 12;
 21        int temp ,tm;
 22        memset(a , 0 ,sizeof(a));
 23        memset(value, 0 , sizeof(value));
 24        memset(result, 0 ,sizeof(result));
 25        memset(map , 0 , sizeof(map));
 26        for (int i = 1; i <= n; ++i)
 27              scanf("%d",&first[i]);
 28        for (int i = 1; i <= n; ++i)
 29              scanf("%d",&decl[i]);
 30        for (int i = 1; i <= n - 1; ++i)
 31              scanf("%d",&dt[i]);
 32        for (int i = 1; i <= n; ++i){
 33            temp = tm = first[i]; 
 34            a[i][0] = dt[i - 1];;
 35            for (int j = 1; j <= h; ++j){
 36                 a[i][j] = j  + dt[i - 1]; 
 37                 value[i][j] = temp;
 38                 tm -= decl[i];
 39                 if (tm > 0) temp += tm; 
 40            }
 41        }
 42 }
 43 
 44 bool footprint(int i , int opt1 , int opt2){
 45       if (i == 1 || i == 0){
 46            if ( Time[i][opt1] > Time[i][opt2] ) return true;
 47            return false;
 48       }
 49       bool bo = footprint(i - 1, map[i][opt1], map[i][opt2]);
 50       if ( bo || Time[i][opt1] > Time[i][opt2] ) return true;
 51       return false;
 52 }
 53 
 54 int  findjp(int lasti, int i , int opt){
 55      if (i == lasti) return opt;
 56      return findjp(lasti , i - 1, map[i][opt]);
 57 }
 58 
 59 void dp(){
 60      int tp ,tv , nowj;
 61      ans = 0;
 62      bool bo;
 63      for (int  i = 0; i <= n; ++i)
 64          for (int  j = 0; j <= h; ++j)
 65             f[i][j]= minn;
 66      ans = f[0][0] = 0;
 67      
 68      for (int i = 1; i <= n; ++i)
 69         for (int j = 0; j <= h; ++j)
 70             for (int k = 0 ; k <= h; ++k){
 71                   if (j - a[i][k] < 0) break;
 72                   tp = j - a[i][k];
 73                   tv = value[i][k];
 74                   if  (f[i - 1][tp] + tv > f[i][j]
 75                    || f[i - 1][tp] +tv == f[i][j] &&
 76                       footprint(i - 1 , tp , map[i][j]) ){
 77                         f[i][j] = f[i-1][tp] + tv;
 78                         map[i][j] = tp;
 79                         Time[i][j] = k;
 80                   }
 81             }
 82      for (int i = 1; i <= n; ++i)
 83          for (int j = 1; j <= h; ++j){
 84               bo = false;
 85               if (f[i][j] > ans) bo =true;
 86               if (f[i][j] == ans){
 87                   if (ansi >= i) nowj = findjp(i , ansi ,ansj);
 88                   if (ansi  < i)  nowj = findjp(ansi , i , j);
 89                   if (ansi >= i && footprint(i , j , nowj)) bo =true;
 90                   if (ansi < i && footprint(ansi , nowj ,ansj)) bo = true;
 91               }
 92               if (bo){
 93                    ans = f[i][j];
 94                    ansi = i;
 95                    ansj = j;
 96               }
 97          }
 98        
 99 }
100 
101 void find(int i , int j){
102      if (i == 0) return;
103      result[i] = Time[i][j] * 5;
104      find(i - 1, map[i][j]);
105 }
106 
107 void print(){
108      find(ansi ,ansj); 
109      if (ans == 0) result[1] = h * 5;
110      for (int  i = 1; i <= n - 1; ++i)
111        printf("%d, " , result[i]);
112      printf("%d\n",result[n]);
113      printf("Number of fish expected: %d\n\n", ans);
114      
115 }
116 
117 int main(){
118       freopen("poj1042.in","r",stdin);
119       freopen("poj1042.out","w",stdout);
120       scanf("%d",&n);
121       while (n){
122             init();
123             dp();
124             print();
125             scanf("%d",&n);
126       }
127       fclose(stdin); fclose(stdout);
128 }

 

 

  

posted on 2013-03-23 19:42  yzcstc  阅读(238)  评论(0编辑  收藏  举报