ImageVerifierCode 换一换
格式:PPT , 页数:54 ,大小:1.40MB ,
资源ID:6937700      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-6937700.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(4. 简单计算题(三).ppt)为本站会员(mcady)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

4. 简单计算题(三).ppt

1、第四讲,简单计算题(三),ACM算法与程序设计,数学科学学院:汪小平 ,解最短路问题的基本方法,从vi到vj的最短路是指: vi到vj间权和最小的路。 最短路问题有以下几种类型:两个指定顶点间的最短路图中各对顶点间的最短路两个指定顶点间通过某些指定顶点的最短路第二、第三、第k条最短路,解最短路问题的基本方法,一、从一个始点v1到一个终点vn的最短路问题,解决该问题一个比较好的算法是由Dijkstra于1959年提出的。该算法不仅能求出v1到vn的最短路,也能求出v1到其他顶点的最短路。,Dijkstra算法是一种标号算法。赋权图上每个顶点给出一个标号-临时标号(T标号)或固定标号(P标号),P

2、标号表示始点到该顶点的最短距离,T标号存储到始点的路长数据(中间结果,不一定是最短距离),便于计算,减少运算量。,解最短路问题的基本方法-Dijkstra算法,Dijkstra算法步骤:,-固定编号,下标表示前趋顶点,-临时编号,下标表示前趋顶点,5.1 解最短路问题的基本方法-Dijkstra算法,解最短路问题的基本方法-Dijkstra算法,把以顶点及其前趋顶点作为端点的边作为一个集合,该边集导出的子图是一个生成树。,该生成树是否是最小生成树?,解最短路问题的基本方法-Floyd算法,二、求任意两顶点间的最短路问题,第一种算法:运用Dijkstra算法变换n次起始点,可求出任意两顶点间的最

3、短路,但对于稠密图,计算量比较大。Floyd算法(1962年以Floyd提出),计算量较小,形式简单,易于编程。Floyd算法通常用矩阵实现,也称为矩阵求解法。,为了说明Floyd算法,下面定义几个符号:,解最短路问题的基本方法-Floyd算法,解最短路问题的基本方法-Floyd算法,解最短路问题的基本方法-Floyd算法,矩阵算法:,解最短路问题的基本方法-Floyd算法,例 求下图任意两顶点的最短路长。,解最短路问题的基本方法-Floyd算法,解最短路问题的基本方法-Floyd算法,解最短路问题的基本方法-Floyd算法,解最短路问题的基本方法-Floyd算法,解最短路问题的基本方法-Fl

4、oyd算法,解最短路问题的基本方法-Floyd算法,容易看到,v5到v1的最短距离为6,它们之间的路为:,v2到v5的最短距离为4,它们之间的路为:,部分参考代码,#define inf 10000000 class Matrix public:Matrix(int n);/构造函数 Matrix();/析构函数 int row;/矩阵行数与列数int* p; ; Matrix:Matrix(int n) row=n;p=new int*row;for(int i=0;irow;i+)pi=new introw;for(int i=0;in;i+)for(int j=0;jn;j+)pij=(

5、i=j)?0:inf; Matrix:Matrix() for(int i=0;irow;i+)delete pi;delete p; ,部分参考代码,void nodes2nodes(const Matrix ,求ab mod n的值,1、问题描述 求ab mod n,即求ab除以n的余数。 2、输入数据第一行是一个正整数N,表示测试数据的组数,下面依次是n行测试数据,每行由3个数a、b、n组成,之间用一个空格隔开。 3、输出数据对应每行输入,输出一行ab mod n的值。,求ab mod n的值,4、输入样例 1 23 25 7 5、输出样例 2,分析,比如,求,部分参考程序,参考程序:

6、int modexp(int a, int b, int n) int t=1;while(b!=0)if(b%2=1) /if(b ,Ants Run! http:/ Yang likes to play with ants when he is free. What? Are you asking why he plays with ants instead of others? Ah, because ant is the only non-plant living thing which can be found in Qingshuihe Campus of UESTC apart

7、from human beings.This time, Professor Yang caught several ants after finishing his lecture for freshmen. At the beginning of the game, he puts N ants around a plate and numbers them in clockwise order. The ants are so obedient that they run clockwise under the guide of Professor Yang on the boundar

8、y of the plate which is a circle. When one ant catches up with its previous ant, the game is over. Knowing the speed of ants, Professor Yang wants you to help him to adjust the distance between adjacent ants to make the game last longer.,Input The first line of the input is T (no more than 10000), w

9、hich stands for the number of test cases you need to solve.Each test case begins with “N R”(without quotes) representing the number of ants participating the game is N and the radius of the circle is R cm. The next line lists N integers and the i-th number is the speed (cm/s) of the i-th ant in cloc

10、kwise direction. All numbers are positive integer not larger than 20. Output If the game can last forever, print “Inf” in a single line, otherwise please output the longest time in seconds each game can last, which should be printed accurately rounded to three decimals.,Sample Input2 3 1 1 1 1 3 1 3

11、 2 1 Sample OutputInf 3.142,在样例中,3只蚂蚁连环追,第一只追第二只第二只追第三只第三只追不上第一只所有蚂蚁都是相同速度则inf 最最关键的是Pi,如何计算(造成Wrong Answer的主要原因) acos(-1.0),#include #include double pi=acos(-1.0); int v30; int main() int t,p,n,i; double r; scanf(“%d“, ,Archimedes http:/ Wang has got a ball made of a certain kind of metal (assume t

12、he ball is uniform density). He wants to find out what the metal is by calculating the density of the ball. Mr. Wang follows the story of Archimedes and crown. By putting the ball in the liquid (assume the liquid is deep enough). Mr. Wang can measure the height h indicated in the image.And the densi

13、ty of the liquid is d, the radius of the ball is R. Now Mr. Wang asks you to simply tell the density of the ball. You can assume the density of the ball is no greater than the liquid.h measures the height from the bottom of the ball to the liquid surface.,Input The input has many test cases. The fir

14、st line is an integer T indicating the number of test cases. Following T lines there are three real numbers h, R, d.(0 h = 100, 0 R = 50,0 d = 100) Output For each test case, output one line with the answer, the density of the ball. round to 0.01. Please refer to the sample output.,Sample Input2 9 9

15、 1 9 8 1 Sample Output0.50 0.59,整理得:,容易遗漏的是若小球不是漂浮,而是悬浮在水中(造成Wrong Answer的主要原因),#include #include double r; int main() int t,p; double h,d,v; scanf(“%d“, ,Flagstone Walk http:/ is a long flagstone walk on the way from the dormitory to the main hall. This flagstone walk has N lines and four flagstone

16、s arranged in each line. Hongshu always start from rightmost flagstone of the first line. In order to make this walk funny, he would always step onto the left or right flagstone in the next line if there is. For example, if Hongshu stands at the second rightmost flagstone of the third line, he would

17、 choose to step to the first or third rightmost one of the forth line. Note that Hongshu has only one choice when he is at the corner of one line.,Because Hongshu has to go to the main hall every day, he wants to know how many different ways to step across the flagstone walk. Can you help him?,Input

18、 The first line of the input is an integer T (T = 20), which stands for the number of test cases you need to solve. Each case consists of an integer N (1 = N = 20) on a single line, which stands for the length of the walk. Output For each case, print the number of ways on a single line.,Sample Input

19、3 2 3 4 Sample Output1 2 3,DP11=1 DP22=DP31+DP11= 1 DP33=DP42+DP22=1 DP13=DP22=1 DP44=DP33=1 DP24=DP33+DP13=2,DP11=1 DP 1 i = DP 2 i 1 DP 2 i = DP 1 i 1 + DP 3 i 1 DP 3 i = DP 2 i 1 + DP 4 i 1 DP 4 i = DP 3 i 1 ,1,1,1,1,1,1,2,#include int main() int i,j,k,n,m,tot,a2121; scanf(“%d“, ,思考:此题的输出结果有没有什么规

20、律,Fly Through http:/ home of flower fairies is being devastated by a monster called Littlefatcat. They have to leave the place where their generations lived with no other choices. CC, the greatest investigator of flower fairies, found a paradise in the west and will lead all the flower fairies there

21、. This paradise is full of flowers and safe from attack of Littlefatcat. However, there are lots of huge rocks on the way to the paradise.,Flower fairies are of different levels which are determined by their power. Fairies of higher level will fly higher. A fairy will persist in flying at the height

22、 corresponding to his or her level for honor and self-respect. Also, rocks on the way have specific height. When the route of a fairy hit a rock, the fairy will have to use magic to fly through the rock. Being aware of the height of all rocks and the specific height each fairy can fly at, CC want to

23、 know how many rocks each fairy will fly through.,fly through,Input The first line will be “N M”(without quotes), representing for the numbers of rocks and fairies. The following line will give N numbers, giving the height of the rocks from the east to west.Then M lines followed. On the i-th line, a

24、 number representing the specific height of fairy numbered i can fly at will be given. All numbers are positive and not bigger than 100000. Output Please output the number of rocks each fairy has to fly through in order to get to the paradise in a single line.,Sample Input5 3 1 2 3 4 5 2 4 6 Sample

25、Output4 2 0,这个问题相当于问在一组数中有多少个是大于等于i的。 All numbers are positive and not bigger than 100000. Time Limit:1000ms. 普通的方法可能会导致Time Limit Exceed,时间复杂度应该控制在O(nlogn)。 方法:先排序,再二分法找插入位置STL。 看看专业队员的编程,#include #include #include #include #include #include #include #include #include #include #include #include #in

26、clude #include #include #include #include #include #include #include #include #include #include ,这就是专业级别的事先准备工作,using namespace std; int data100005; int main() /全程STL int n, m; while (scanf(“%d%d“, ,The lower_bound() algorithm compares a supplied value to elements in a sorted sequence and returns the first position in the container at which value can be inserted without violating the containers ordering.,再来看看哈希的实现,#include #include int a100001,b100001; int main() int n,m,i,j; while(scanf(“%d%d“, ,

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报