收藏 分享(赏)

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

上传人:mcady 文档编号:6937700 上传时间:2019-04-28 格式:PPT 页数:54 大小:1.40MB
下载 相关 举报
4. 简单计算题(三).ppt_第1页
第1页 / 共54页
4. 简单计算题(三).ppt_第2页
第2页 / 共54页
4. 简单计算题(三).ppt_第3页
第3页 / 共54页
4. 简单计算题(三).ppt_第4页
第4页 / 共54页
4. 简单计算题(三).ppt_第5页
第5页 / 共54页
点击查看更多>>
资源描述

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营业执照举报