企业培训-ACM培训03动态规划 精品
ACM培训-动态规划

memset(save,0,sizeof(save )); int n; cin>>n; cout<<f(n );
动态规划的实质
可以看出动态规划的 实质就是
这就是为什么我们常说动态规划必须满 足重叠子问题的 原因.
记 忆 化 ,正符合了这个要求.
用动态规划法求解的问题具有特征: 能够分解为相互重叠的若干子问题; 满足最优性原理(也称子结构质):该问题的解中包含着其。
因此,在解这类问题时,一般采用动 态规划法。
动态规划法与贪心法的区别
区别1
动态规划法先求子问题的解,然后通过求解子问题构造原 问 题的解;
贪心算法是直接地解原问题。
区别2
动态规划法通过对若干局部最优解的比较,去掉了次优解 , 从而产生了更高一层次的局部最优解。
相当于对较低层次的局部最优解进行贪心的选择而得到高 一 级的局部最优解,因而最终产生一个最高层次的局部 最优 解。
记忆化搜索
Opt[i, j] - 每产生一个f(i, j),将f(i, j)的值放入opt中,以 后 再次调用到f(i, j)的时候,直接从opt[i, j]来取就可以 了。
于是动态规划的状态转移方程被直观地表示出来了,这 样 节省了思维的难度,减少了编程的技巧,而运行时间 只是 相差常数的复杂度,
则按子串sub的长度从大到小依次进行递推 求 解:
利用数字三角的思路
cost[i-2][ j+1] 若st[ j]=st[i+j1] Cost[i][ j]= min{cost[i-1][ j]+1,cost[i1][ j+1]+1}
ACM 培 训
第21讲动态规划(投资分配问题)-PPT精选文档

xi a i1 x 0 i 1 .2 . .n i
投资分配问题
f1(x) = g1(x)
(因为只给一个工厂)
当1<k≤n 时,其递推关系如下: 设:y 为分给第k 个工厂的资金(其中 0≤y ≤ x ),此时还剩 x - y (万元)的资金需要分配给前 k-1 个工厂,如果采取最优策略,则得到的最大 利润为fk-1(x-y) ,因此总的利润为: gk(y) + fk-1(x-y)
y 0 , 1 0 , 2 0 , 3 0
最优策略为(20,10),此时最大利润为70万元。
河南理工大学ACM-ICPC培训
投资分配问题
f ( 2 0 ) m a x g ( yf ) ( 2 0) y 5 0 2 2 1
y 0 , 1 0 , 2 0
最优策略为(20,0),此时最大利润为50万元。
最优策略为(40,20),此时最大利润为120万元。 同理可求得其它 f2(x) 的值。
河南理工大学ACM-ICPC培训
投资分配问题
f 2 (5 0 ) m ax g2 ( y) ,5 0 f 1 (5 0 y )
y 0 ,1 0 ,
g 2 (0 ) f 1 (5 0 ) g (1 0 ) f ( 4 0 ) 1 2 g 2 ( 2 0 ) f 1 (3 0 ) 105 g 2 (3 0 ) f 1 ( 2 0 ) g 2 ( 4 0 ) f 1 (1 0 ) g 2 (5 0 ) f 1 (0 )
g2 (0) f1 (60) 0 85 g (10) f (50) 20 85 1 2 g2 (20) f1 (40) 40 80 max g2 (30) f1 (30) max50 65 120 g (40) f (20) 55 50 1 2 g2 (50) f1 (10) 60 20 65 0 g2 (60) f1 (0)
pascal算法讲义-第十一讲

第十一讲 动态规划一、动态规划总述动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法。
20世纪50年代初美国数学家R.E.Bellman等人在研究多阶段决策过程(multistep decision process)的优化问题时,提出了著名的最优化原理(principle of optimality),把多阶段过程转化为一系列单阶段问题,利用各阶段之间的关系,逐个求解,创立了解决这类过程优化问题的新方法——动态规划。
1957年出版了他的名著《Dynamic Programming》,这是该领域的第一本著作。
跟分治法一样,动态规划也是通过组合子问题的解而解决整个问题的。
分治法可以把问题划分成一些独立的子问题,递归的求解各个子问题,然后合并子问题的解而得到原问题的解。
与此不同,动态规划适用于子问题不独立的情况,也就是各子问题包含公共的子子问题。
这种情况下,分治法就会有大量的重复计算,即重复求解公共子子问题。
动态规划对每个子子问题只求解一次,将结果存在一张表里,从而避免每次遇到各个子问题时重新计算答案。
动态规划通常应用于最优化问题。
此类问题一般有很多很多种可行解,每个解有一个值,而我们希望找出一个具有最优(最大或最小)值的解。
称这样的解为该问题的最优解(而不是确定的最优解),因为可能存在多个取值最优的解。
动态规划算法的设计可以分为如下4个步骤:1)描述最优解的结构2)递归定义最优解的值,这个递归方程称为状态转移方程3)按自底向上的方式计算最优解的值4)由计算结果构造一个最优解(一般竞赛时无需构造,如果有要求则有时需要在第三步的计算中记录一些附加信息)。
下面介绍一些术语,希望读者在阅读完整节内容后进行理解:阶段:把所给求解问题的过程恰当地分成若干个相互联系的阶段,以便于求解,过程不同,阶段数就可能不同.描述阶段的变量称为阶段变量。
acm培训计划

acm培训计划导言ACM (Association for Computing Machinery) 是一个国际性的计算机学会,旨在为计算机专业人士提供交流学习和培训的平台。
ACM 培训计划旨在帮助学生提升他们的算法和编程能力,从而更好地参与 ACM 竞赛。
本培训计划将围绕算法与数据结构、编程语言、数学及竞赛技巧展开,以帮助学生提升专业知识、提高团队合作能力和竞赛技能。
一、培训目标1. 提升学生算法和数据结构基础知识,使其能够灵活运用于解决实际问题。
2. 培养学生对编程语言的深刻理解和应用能力。
3. 加强学生的数学基础,提高解决问题的抽象能力。
4. 提高学生的 ACM 竞赛技巧,培养解决问题的思考和团队合作能力。
二、培训内容1. 算法与数据结构1.1. 基本算法:递归、分治、贪心、动态规划1.2. 基本数据结构:栈、队列、链表、树、图1.3. 高级算法:最短路径、最小生成树、网络流、字符串算法1.4. 算法分析与设计:时间复杂度、空间复杂度和算法优化2. 编程语言2.1. C/C++/Java/Python 等主流编程语言的基本语法和特性2.2. 编程范例分析和练习2.3. 算法实现与调试技巧3. 数学基础3.1. 离散数学基础知识3.2. 数论、组合数学和图论基础3.3. 动态规划数学建模4. ACM 竞赛技巧4.1. ACM 竞赛规则和常见题型分析4.2. 模拟训练和解题技巧分享4.3. 队伍协作与策略分享三、培训方式1. 理论授课1.1. 定期组织专家授课,系统讲解培训内容,由资深ACM 竞赛选手分享解题技巧和经验。
1.2. 组织学习交流会,鼓励学生积极提问和讨论。
2. 实践训练2.1. 组织编程实践训练,引导学生独立完成算法实现和调试。
2.2. 选派导师进行一对一指导,帮助学生解决练习中遇到的难点问题。
3. 竞赛准备3.1. 组织模拟 ACM 竞赛,帮助学生提前适应竞赛环境和节奏。
3.2. 参与区域赛和国际赛前的模拟训练,为学生提供更加真实的竞赛体验。
ACM培训计划

ACM培训计划书制作人:xxx2008/10/21一、什么是ACMACM/ICPC ( ACM International Collegiate Programming Contest)国际大学生程序设计竞,ACM/ICPC 是由国际计算机界历史悠久、颇具权威性的组织ACM (Association for Computing Machinery ,美国计算机协会) 主办的,世界上公认的规模最大、水平最高的国际大学生程序设计竞赛,其目的旨在使大学生运用计算机来充分展示自己分析问题和解决问题的能力。
该项竞赛从1970 年举办至今已历31 届,一直受到国际各知名大学的重视,并受到全世界各著名计算机公司的高度关注,在过去十几年中,APPLE 、AT&T 、MICROSOFT 和IBM 等世界著名信息企业分别担任了竞赛的赞助商。
可以说,ACM 国际大学生程序设计竞赛已成为世界各国大学生最具影响力的国际级计算机类的赛事,是广大爱好计算机编程的大学生展示才华的舞台,是著名大学计算机教育成果的直接体现,是信息企业与世界顶尖计算机人才对话的最好机会。
该项竞赛分区域预赛和国际决赛两个阶段进行,各预赛区第一名自动获得参加世界决赛的资格,世界决赛安排在每年的 3 ~ 4 月举行,而区域预赛安排在上一年的9 ~12 月在各大洲举行。
ACM/ICPC 的区域预赛是规模很大、范围很广的赛事。
仅在2003 年参加区域预赛的队伍就有来自75 个国家(地区),1411 所大学的3150 支代表队,他们分别在127 个赛场中进行比赛,以争夺全球总决赛的73 个名额,其激烈程度可想而知。
2004 年第29 届ACM/ICPC 亚洲赛区预赛共设了北京、上海、台北、高雄、汉城、德黑兰、爱媛(日本)、达卡(孟加拉国)、马尼拉、坎普尔(印度)等10 个赛站,来自亚洲各国知名高校的各个代表队进行了激烈的角逐。
中国内地从1996 年开始参加ACM/ICPC 亚洲区预赛,至今已历九届。
《ACM新生培训讲座》课件

准备。
培训内容和安排
1
第一周️数据Leabharlann 型和控制结构2第二周️
高级编程概念和技巧
3
第三周️
竞赛策略和实践
4
第四周️
代码审查和提高
参与者的期望和要求
自律
求知
依靠自己的学习热情和时间管理技能。
有意愿和激情学习计算机科学和解决问题的能力。
团队合作
积极
愿意搭档工作和合作成为更棒的程序员。
积极主动并主动学习并与讲师和同学互相学习交
ACM新生培训讲座
欢迎来到ACM新生培训!在这个讲座中,我们将带领你了解计算机科学和
ACM组织,帮助你启动成功的计算机科学生涯。
培训目的和重要性
1
提高技能
2
培养领导力
3
扩大交际圈
教授核心计算机科学知识和
帮助学生提高领导力和团队
拓展社交网络,与其他志同
编程技能,为ACM竞赛做好
合作能力。
道合的人一起成长。
流。
讲座的互动环节
讨论小组
编程挑战
团队协作
和同学们小组工作讨论和交流你在
接受挑战并在最短时间内解决计算
和其他团队一起展示你的技能和代
计算机科学上的思考。
机科学难题。
码。
讲座结束的总结和展望
总结✔️
展望️
回顾所学知识和数字,并讨论学习惊喜和收获。
提供ACM组织入门,推荐其他不同领域的学习,开放
一个继续学习计算机科学的交流平台。
答疑和交流环节
"如果你想要更多的培训,我们可以探讨额外计算机科学的资源和帮助。"
欢迎提出你的问题和超越讲座中提到的主题的任何新的想法!让我们一起共同学习和成长。
acm培训专题规划书_动态规划

ACM-ICPC(重邮赛区)参赛队培训专题计划培训专题及其所占比例:1)搜索:10%2)动态规划:15%3)贪心算法:约5%4)构造:约5%5)图论:约10%6)计算几何:约5%7)纯数学问题:约20%8)数据结构:约5%9)其它:约25%第二次专题——动态规划(1)Jury CompromiseDescriptionIn Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is done as follows. First, several people are drawn randomly from the public. For each person in this pool, defence and prosecution assign a grade from 0 to 20 indicating their preference for this person. 0 means total dislike, 20 on the other hand means that this person is considered ideally suited for the jury.Based on the grades of the two parties, the judge selects the jury. In order to ensure a fair trial, the tendencies of the jury to favour either defence or prosecution should be as balanced as possible. The jury therefore has to be chosen in a way that is satisfactory to both parties.We will now make this more precise: given a pool of n potential jurors and two values di (the defence's value) and pi (the prosecution's value) for each potential juror i, you are to select a jury of m persons. If J is a subset of {1,..., n} with m elements, then D(J ) = sum(dk) k belong to Jand P(J) = sum(pk) k belong to J are the total values of this jury for defence and prosecution.For an optimal jury J , the value |D(J) - P(J)| must be minimal. If there are several jurys with minimal |D(J) - P(J)|, one which maximizes D(J) + P(J) should be selected since the jury should be as ideal as possible for both parties.Y ou are to write a program that implements this jury selection process and chooses an optimal jury given a set of candidates.InputThe input file contains several jury selection rounds. Each round starts with a line containing two integers n and m. n is the number of candidates and m the number of jury members.These values will satisfy 1<=n<=200, 1<=m<=20 and of course m<=n. The following n lines contain the two integers pi and di for i = 1,...,n. A blank line separates each round from the next. The file ends with a round that has n = m = 0.OutputFor each round output a line containing the number of the jury selection round ('Jury #1', 'Jury #2', etc.).On the next line print the values D(J ) and P (J ) of your jury as shown below and on another line print the numbers of the m chosen candidates in ascending order. Output a blank before each individual candidate number.Output an empty line after each test case.Sample Input4 21 22 34 16 20 0Sample OutputJury #1Best jury has value 6 for prosecution and value 4 for defence:2 3HintIf your solution is based on an inefficient algorithm, it may not execute in the allotted time.(2)Number SequenceDescriptionA single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another.For example, the first 80 digits of the sequence are as follows: 1121231234123451234561234567123456781234567891234567891012345678910111234567891 0InputThe first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 ≤ i ≤ 2147483647)OutputThere should be one output line per test case containing the digit located in the position i. Sample Input283Sample Output22(3)CodeDescriptionTransmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that the words are made only of small characters of the English alphabet a,b,c, ..., z (26 characters). From all these words we consider only those whose letters are in lexigraphical order (each character is smaller than the next character).The coding system works like this:• The words are arranged in the increasing order of their length.• The words with the same length are arranged in lexicographical order (the order from the dictionary).• We codify these words by their numbering, starting with a, as follows:a - 1b - 2...z - 26ab - 27...az - 51bc - 52...vwxyz - 83681...Specify for a given word if it can be codified according to this coding system. For the affirmative case specify its code.InputThe only line contains a word. There are some constraints:• The word is maximum 10 letters length• The English alphabet has 26 characters.OutputThe output will contain the code of the given word, or 0 if the word can not be codified.Sample InputbfSample Output55(4)Check the difficulty of problemsDescriptionOrganizing a programming contest is not an easy job. To avoid making the problems too difficult, the organizer usually expect the contest result satisfy the following two terms:1. All of the teams solve at least one problem.2. The champion (One of those teams that solve the most problems) solves at least a certain number of problems.Now the organizer has studied out the contest problems, and through the result of preliminary contest, the organizer can estimate the probability that a certain team can successfully solve a certain problem.Given the number of contest problems M, the number of teams T, and the number of problems N that the organizer expect the champion solve at least. We also assume that team i solves problem j with the probability Pij (1 <= i <= T, 1<= j <= M). Well, can you calculate the probability that all of the teams solve at least one problem, and at the same time the champion team solves at least N problems?InputThe input consists of several test cases. The first line of each test case contains three integers M (0 < M <= 30), T (1 < T <= 1000) and N (0 < N <= M). Each of the following T lines contains M floating-point numbers in the range of [0,1]. In these T lines, the j-th number in the i-th line is just Pij. A test case of M = T = N = 0 indicates the end of input, and should not be processed.OutputFor each test case, please output the answer in a separate line. The result should be rounded to three digits after the decimal point.Sample Input2 2 20.9 0.91 0.90 0 0Sample Output0.972(5)Heroes Of Might And MagicDescriptionIn the new version of the famous game "Heroes of Might and Magic" heroes themselves take active part in battles. More of that, hero can defeat some monsters alone, without any supporting army. In this problem you are asked to develop the program which would find the strategy for a mage hero fighting face to face with a pack of monsters.Each hero initially has HP H hit points and MP H mana points. Heroes can use different spells. Y our hero knows three spells: Lighting Bolt, Teleport and Heal. Each spell costs one mana point.Each monster has HP M hit points. Pack of monsters is a single group of several monsters who act as one. Therefore if initially the pack consists of N M monsters, they have N M × HP M hit points. As the battle proceeds, monsters' number of hit points decreases. If monsters have H hit points, that means that the group consists of ceiling(H / HP M) monsters (ceiling is a function that returns the smallest integer number not less its argument).The battle runs on a one-dimensional battlefield consisting of N + 1 squares, numbered starting from 0. Y our hero resides on the square number 0 and does not move. Monsters initially reside on Nth square and can move. Monsters can move at most V squares a turn.The battle consists of turns. First your hero makes a turn, then the monsters, and so on. Monsters' strategy is very easy - they move in the direction of your hero min(V, P - 1) squares where P is the square number where they were in the beginning of their turn. If the monsters are on the square number 1 in the end of the movement, then they strike your hero. If there are K monsters left in a pack, their strike decreases hit points of the hero by K. If your hero has non-positive hit points, then the hero is defeated.Y our hero's turn is always the casting of some spell. Lighting Bolt spell removes L P hit pointsfrom a pack of monsters, where P is the square number on which the monsters reside. Teleport spell moves monsters to any desired square (except 0 where your hero resides). Heal spell adds dH hit points to hero. However, his hit points never exceed HP H, so if after using Heal spell his hit points are greater then HP H, they are decreased to HP H. If your hero has zero mana points and there is at least one monster left in the pack, then the hero is defeated.Find the strategy which would allow your hero to defeat monsters. Monsters are defeated if their hit points are non-positive.InputThe first line of the input contains positive integer numbers separated by spaces in the following order: N, HP H, MP H, HP M, N M, V, dH. (1 ≤ N ≤ 10, 2 ≤ HP H≤ 100, 1 ≤ MP H≤ 50, 1 ≤ HP M≤ 10, 1 ≤ N M≤ 10, 1 ≤ V ≤ N, 1 ≤ dH < HP H). The second line of the input file contains N integer numbers L1, L2, ..., L N(1 ≤ LP ≤ 10), separated by spaces.OutputIf the hero cannot win the battle, write the word DEFEA TED on the first line of the output. In the other case write the word VICTORIOUS on the first line of the output file and then write any sequence of hero's actions that leads to victory, where each line of the output file starting from the second one must correspond to one hero's turn. The first character of the line must be one of the following:∙L - Cast Lighting Bolt spell.∙T - Cast Teleport spell.∙H - Cast Heal spell.If the hero casts Teleport spell then T character must be followed by a space and an integer number from 1 to N - the square number where the monsters should be teleported to.Sample Input1 6 5 1 4 1 31Sample OutputVICTORIOUSLLHLL。
动态规划-动态规划

过程指标函数是指过程所包含的各阶段的状 态和决策所产生的总效益值,记为
Vkn (sk , Pkn ) Vkn (sk , dk (sk ), sk1, dk1(sk1), , sn , dn (sn ), sn1) k 1, 2, , n
动态规划所要求的过程指标函数应具有可分 离性,即可表达为它所包含的各阶段指标函数的 函数形式。
能用动态规划方法求解的多阶段决策过程是一 类特殊的多阶段决策过程,即状态具有无后效性 的多阶段决策过程。
无后效性(马尔可夫性):是指如果某阶段状 态给定后,则在这个阶段以后过程的发展不受 这个阶段以前各段状态的影响;构造动态规划 模型时,要充分注意是否满足无后效性的要求; 状态变量要满足无后效性的要求;如果状态变 量不能满足无后效性的要求,应适当改变状态 的定义或规定方法。
3、决策(decision)
决策:在某一阶段,当状态给定后,往往可以 作出不同的决定,从而确定下一阶段的状态,这种 决定称为决策。
决策变量:描述决策的变量。dk(sk) :第k阶段 的决策变量(状态变量sk的函数)。
允许决策集合:决策变量的取值范围。常用 Dk(sk)表示。显然dk(sk)∈Dk(sk)。
3 3*
3
4
6 决策点为D1
第二阶段,由Bj到Ci分别均有三种选择
f2
B1
min
B1C1 B1C2
B1C3
f3 f3 f3
C1 C2
C3
min
7 6 4 7* 6 6
11决策点为C2
f2
B2
min
BB22CC21
f3 f3
C1 C2
min
3 6* 2 7*
min
4
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
对于阶段4
P[1][3] = min{ P[0][3]+v[0][3], P[1][2]+h[1][2]} = min{8+4, 5+4} = 9
P[2][2] = min{ P[1][2]+v[1][2], P[2][1]+h[2][1]} = min{5+2, 5+4} = 7
P[0][j]=P[0][j-1]+h[0][j-1] ( i=0, j>0)
P[i][0]=P[i-1][0]+v[i-1][0] ( i>0, j=0)
画出用动态规划思想求出的各个路口对P点 的最小距离。图中圆圈里就是这个距离。 箭头表示所寻得的最佳行走路径。(图3)
7
3
1
6
3
4
2
2
2
0
3
P点
151 2 5 234 5 6
151 2 5 234 5 6
151 2 5 234 5 6
151 2 5 234 5 6
p( q+1,r,k-1 )
d ( l , q )= s[l] s[l+1]…s[q]
q的变化范围:从q+1到r之间所包含 的数字个数应大于k-1(乘号个数)。
r-(q+1)+1>k-1 q<r-k+1
p( l,r,k )=max{ d( l, q ) * p( q+1, r ,k-1 ) }
最后
P[3][3] = min{ P[2][3]+v[2][3], P[3][2]+h[3][2]}
= min{12+3, 8+2} = 10
综上,数组P的通项表示为
P[i][j]= min( ( p[i-1][j]+v[i-1][j]), (p[i][j-1]+h[i][j-1]) ) (i, j>0)
P[0][j]=P[0][j-1]+h[0][j-1]
( i=0, j>0)
P[i][0]=P[i-1][0]+v[i-1][0]
下面给出P数组中的数据
( i>0, j=0)
37
7
8
10
26
5
7
12
12
4
5
9
00
3
5
8
0
1
2
3
数组P的通项表示为 P[i][j]= min( ( p[i-1][j]+v[i-1][j]), (p[i][j-1]+h[i][j-1]) ) (i, j>0)
int v[3][4]={ {2,2,3,4},{4,1,2,4},{1,2,2,3} };
int p[4][4]={ {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0} };
p[0][0]=0;
for(int j=1;j<4;j++)
//y轴上的点
p[0][j]=p[0][j-1]+h[0][j-1];
25 56
d( l,q)=d(0,3)=3215 p( q+1,r,k-1 )=p( 4,6,2 )
( p( 0,6,3)|q=3) = 3215 * p( 4,6,2 )
p( 0,6,3)= max{ 3 * p( 1,6,2 ) , // q=0 32 * p( 2,6,2 ) , // q=1 321* p( 3,6,2 ) , // q=2 3215 * p( 4,6,2 ) } // q=3
3
31223451
2
1
4
0
3
2
3
0
1
2
h[4][3] = { {3,2,3}, {2,1,4}, {3,4,5}, {3,1,2} };
南北方向上道路长度存至数组v[3][4]中,也规定 第一维为行号,第二维为列号。
2
1
2
2
3
1
4
1
2
4
02
2
3
4
0
1
2
3
v[3][4] = {{2, 2, 3, 4},{4, 1, 2, 4},{1, 2, 2, 3}};
10 动态规划
任务:
P是出发点,从P到A,求最短路径(图1)
G
3 D 1B 2
A
1
2
2
3
K
3 H
4 E
5
4
1
2
N
2 L
1 I
4
2
2
3
3
2
3
C
4
阶段5
F
4 阶段4
P
O
M
J
阶段1
阶段2 阶段3
图1
思路
1. 先看第5阶段,到达A点有两条路
– B A,需要2km – C A,需要3km
2. 令
– 从P A的最短路径为P(A); – 从P B的最短路径为P(B); – 从P C的最短路径为P(C) …… – P(A) = min{P(B)+2, P(C)+3}; – P(B) = min{P(D)+1, P(E)+2}; – P(C) = min{P(E)+5, P(F)+4};
P(A) = min{P(B)+2, P(C)+3};
P(B) = min{P(D)+1, P(E)+2};
P(C) = min{P(E)+5, P(F)+4};
D1 B 2 A
2
3
5
P(B)
E
C
4 P(C)
F
…
P(N) = 2;
P(O) = 3;
上述递推公式告诉我们,要求P(A)需要先 求出阶段5中的P(B)和P(C);要求 P(B) (或者 P(C)),又要先求出阶段4中的 P(D) 和 P(E) (或P(F)和P(E))……
两种算法的差别在于,贪心法产生一个 按贪心策略形成的判定序列,该序列不 保证解是全局最优的。而动态规划会产 生许多判定序列,再按最优性原理对这 些序列加以筛选,去除那些非局部最优 的子序列。
举例说明动态规划思路
问题: 在数字串中插入若干乘号使 总的乘积最大
* s
3215 0123
**
125 456
if (a<=b ) return a; else return b; }
动态规划的几个概念:
阶段:据空间顺序或时间顺序对问题的求 解划分阶段。
状态:描述事物的性质,不同事物有不同 的性质,因而用不同的状态来刻画。对 问题的求解状态的描述是分阶段的。
决策:根据题意要求,对每个阶段所做出 的某种选择性操作。
状态转移方程:用数学公式描述与阶段相 关的状态间的演变规律。
动态规划是运筹学的一个重要分支,是解 决多阶段决策过程最优化的一种方法。
所谓多阶段决策过程,是将所研究的过程 划分为若干个相互联系的阶段,在求解 时,对每一个阶段都要做出决策,前一 个决策确定以后,常常会影响下一个阶 段的决策。
动态规划所依据的是“最优性原理”。
q=l=0
q=l,l+1,…,r-k *
32151 2 5 01234 5 6
d( l,q)=d(0,0)=3 p( q+1,r,k-1 )=p( 1,6,2 )
(p( 0,6,3)|q=0) = 3 * p( 1,6,2 )
q= l+1=1 * 32151 2 5 01234 5 6
d(l,q)=d(0,1)= 32 p( q+1,r,k-1 )=p( 2,6,2 )
for(int i=1;i<4;i++)
//x轴上的点
p[i][0]=p[i-1][0]+v[i-1][0];
for(int i=1;i<4;i++)
//内部的点
for(int j=1;j<4;j++)
p[i][j]=min( ( p[i-1][j]+v[i-1][j]),
(p[i][j-1]+h[i][j-1]) );
P[0][0] = 0;
对于阶段1:
P[0][1] = P[0][0]+h[0][0] = 0+3 = 3;
P[1][0] = P[0][0]+v[0][0] = 0+2 = 2;
对于阶段2
P[1][1] = min{ P[0][1]+v[0][1], P[1][0]+h[1][0]} = min{3+1, 2+2} = 4
4. 为了计算方便,将图1改为图2
x
3 h[3][0]
h[3][1]
h[3][2] (3, 3)
v[2][0]
v[2][1]
v[2][2]
v[2][3]
2 h[2][0]
h[2][1]
h[2][2]
v[1][0]
v[1][1]
v[1][2]
v[1][3]
1 h[1][0]
h[1][1]
h[1][2]
显然,要依照上述递推过程求解,需要倒 过来,从P(P)出发,先求出第一阶段的P(O) 和P(N),再求第二阶段的P(K),P(L), P(M);……,最后得到P(A)。
3. 选择数据结构,将每条路经的长度存在数 组中。
东西方向上的道路长度存在两维数组h[4][3]中规 定数组的第一维为行号,第二维为列号。