动态规划01

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AK_97_CT/article/details/88383189

在这里插入图片描述
小明做任务,上午有8个任务,每个任务对应一定的佣金,同一时刻只能做一个任务,求小明最多能获取多少佣金?
在这里插入图片描述

解决方案:

   int n = 8;	//任务个数
   int pre[] = {0, 0, 0, 1, 0, 2, 3, 5};	//选择这个任务之后前驱最近任务下标
   int profit[] = {5, 1, 8, 4, 6, 3, 2, 4};	//任务收益
   int opt[n + 1];
   int res = 0;
   opt[0] = 0;
   for(int i = 1; i < n + 1; i ++)
     {
         opt[i] = max(opt[i - 1], profit[i - 1] + opt[pre[i - 1]]);
         res = max(opt[i],res);
     }
     cout<<res;
     

猜你喜欢

转载自blog.csdn.net/AK_97_CT/article/details/88383189