贪心与动态规划(一)9.pptVIP

  • 14
  • 0
  • 约1.29万字
  • 约 51页
  • 2018-07-09 发布于江苏
  • 举报
贪心与动态规划(一)9

导引问题:FatMouse Trade 所谓“贪心算法”是指: 在对问题求解时,总是作出在当前看来是最好的选择。也就是说,不从整体上加以考虑,它所作出的仅仅是在某种意义上的局部最优解(是否是全局最优,需要证明)。 当一个问题的最优解包含着它的子问题的最优解时,称此问题具有最优子结构性质。 当一个问题具有最优子结构性质时,我们会想到用动态规划法去解它。但有时会有更简单有效的算法。顾名思义,贪心算法总是作出在当前看来是最好的选择。也就是说贪心算法并不从整体最优上加以考虑,它所作出的选择只是在某种意义上的局部最优选择。 用贪心算法更简单,更直接且解题效率更高。虽然贪心算法不是对所有问题都能得到整体最优解,但对范围相当广的许多问题它能产生整体最优解。如图的单源最短路径问题,最小生成树问题等。在一些情况下,即使贪心算法不能得到整体最优解,但其最终结果却是最优解的很好的近似解。 特别说明: 若要用贪心算法求解某问题的整体最优解,必须首先证明贪心思想在该问题的应用结果就是最优解!! Moving Tables /showproblem.php?pid=1050 Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure. Input? The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case begins with a line containing an integer N , 1=N=200 , that represents the number of tables to move. Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t (each room number appears at most once in the N lines). From the N+3-rd line, the remaining test cases are listed in the same manner as above. Output? The output should contain the minimum time in minutes to complete the moving, one per line. Sample Input 3 4 10 20 30 40 50 60 70 80 2 1 3 2 200 3 10 100 20 80 30 50 Sample Output 10 20 30 算法分析: 1、如果没有交叉,总时间应该是多少? 2、影响搬运时间的因素是什么? 3、如果每趟处理都包含最大重叠,处理后的效果是什么? 4、得出什么结论? 贪心算法的基本步骤 1、从问题的某个初始解出发。 2、采用循环语句,当可以向求解目标前进一步时,就根据局部最优策略,得到一个部分解,缩小问题的范围或规模。 3、将所有部分解综合起来,得到问题的最终解。 贪心算法都很简单吗? 看一道难一些的。 (2004年上海赛区:正式赛是简单题) 示意图: 谈谈自己的想法吧~ Case 1: King: 200 180 160 Tianji: 190 170 150 Case 2: King: 200 180 160 Tianji: 180 170 150 Case 3: King: 200 180 160 Tianji: 180 155 150 总体的思路是什么? 1 对田忌和王的马的速度按从大到小排序 2.1如果田忌最慢的马比大王最慢的马快 那么先赢一局 2.2如果田忌最慢的马比大王最慢的马慢 那么用最慢的马和大王最快的马比赛 2.3如果田忌最慢的马和大王最

文档评论(0)

1亿VIP精品文档

相关文档