谁说不能这样做题之关于一道题的四种实现,POJ 2263
来源:互联网 发布:2017双十一化妆品数据 编辑:程序博客网 时间:2024/05/21 06:32
http://acm.pku.edu.cn/JudgeOnline/problem?id=2263
Heavy Cargo
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1296 Accepted: 754
Description
Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their latest model, the Godzilla V12, is so big that the amount of cargo you can transport with it is never limited by the truck itself. It is only limited by the weight restrictions that apply for the roads along the path you want to drive.
Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.
Given start and destination city, your job is to determine the maximum load of the Godzilla V12 so that there still exists a path between the two specified cities.
Input
The input will contain one or more test cases. The first line of each test case will contain two integers: the number of cities n (2<=n<=200) and the number of road segments r (1<=r<=19900) making up the street network.
Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
The last line of the test case contains two city names: start and destination.
Input will be terminated by two values of 0 for n and r.
Then r lines will follow, each one describing one road segment by naming the two cities connected by the segment and giving the weight limit for trucks that use this segment. Names are not longer than 30 characters and do not contain white-space characters. Weight limits are integers in the range 0 - 10000. Roads can always be travelled in both directions.
The last line of the test case contains two city names: start and destination.
Input will be terminated by two values of 0 for n and r.
Output
For each test case, print three lines:
- a line saying "Scenario #x" where x is the number of the test case
- a line saying "y tons" where y is the maximum possible load
- a blank line
Sample Input
4 3Karlsruhe Stuttgart 100Stuttgart Ulm 80Ulm Muenchen 120Karlsruhe Muenchen5 5Karlsruhe Stuttgart 100Stuttgart Ulm 80Ulm Muenchen 120Karlsruhe Hamburg 220Hamburg Muenchen 170Muenchen Karlsruhe0 0
Sample Output
Scenario #180 tons Scenario #2170 tons
Source
Ulm Local 1998
题意:给出各路径的限制载重,求从A求到B地的货车最大承载的货物
以下为四种解法,按效率由小到大~
1、分析:采用广搜加剪枝,用map[][]表示所给出的图,用mark[][]标记达到某点时所能承重的最高值,记录,每加入一个新结点之前,判断是否可以剪去~
2、分析:采用经典算法floyd实现,采用邻接矩阵表示结果,可以求出从出发点到达任何点能承载的最高货物重量;
floyd补充:
Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法。
通过一个图的权值矩阵求出它的每两点间的最短路径矩阵。
核心思路:
从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按一个公式,构造出矩阵D(1);又用同样地公式由D(1)构造出D(2);……;最后又用同样的公式由D(n-1)构造出矩阵D(n)。矩阵D(n)的i行j列元素便是i号顶点到j号顶点的最短路径长度,称D(n)为图的距离矩阵,同时还可引入一个后继节点矩阵path来记录两点间的最短路径。
采用的是松弛技术,对在i和j之间的所有其他点进行一次松弛。所以时间复杂度为O(n^3);
其状态转移方程如下: map[i,j]:=min{map[i,k]+map[k,j],map[i,j]}
map[i,j]表示i到j的最短距离
K是穷举i,j的断点
map[n,n]初值应该为0,或者按照题目意思来做。
当然,如果这条路没有通的话,还必须特殊处理,比如没有map[i,k]这条路
实现过程:把图用邻接矩阵G表示出来,如果从Vi到Vj有路可达,则G[i,j]=d,d表示该路的长度;否则G[i,j]=空值。 定义一个矩阵D用来记录所插入点的信息,D[i,j]表示从Vi到Vj需要经过的点,初始化D[i,j]=j。 把各个顶点插入图中,比较插点后的距离与原来的距离,G[i,j] = min( G[i,j], G[i,k]+G[k,j] ),如果G[i,j]的值变小,则D[i,j]=k。 在G中包含有两点之间最短道路的信息,而在D中则包含了最短通路径的信息。 比如,要寻找从V5到V1的路径。根据D,假如D(5,1)=3则说明从V5到V1经过V3,路径为{V5,V3,V1},如果D(5,3)=3,说明V5与V3直接相连,如果D(3,1)=1,说明V3与V1直接相连。
时间复杂度:O(n^3)
优缺点:
Floyd算法适用于APSP(All Pairs Shortest Paths),是一种动态规划算法,稠密图效果最佳,边权可正可负。此算法简单有效,由于三重循环结构紧凑,对于稠密图,效率要高于执行|V|次Dijkstra算法。 优点:容易理解,可以算出任意两个节点之间的最短距离,代码编写简单; 缺点:时间复杂度比较高,不适合计算大量数据。
以下是此题floyd算法实现代码:
3、分析:采用优先队列,每次取出当前队列中结点的minheavy最大值出队,对它的连接结点搜索入队,这样,从出发点开始就可以
在到达终点时求出结果,即最大载货物重~
4、分析:类似最小生成树,用并查集实现,对所有的路径进行降序排序,然后按顺序向树中插入边,直到一条边的插入连通了出发地和目的地,返回,这条边的权值即为所求结果~
- 谁说不能这样做题之关于一道题的四种实现,POJ 2263
- C++优先队列详解《谁说不可以这样做题》
- 强大的SWT-谁说Java不能做窗体
- poj上的一道题
- POJ 3233 矩阵快速幂(做的快速幂的第一道题你敢信?
- 一道关于图种的题
- 关于数据库的一道题??
- 一道关于信号的题
- 关于继承的一道题
- 关于 static 的一道题
- 关于一道题的思考
- 关于C++的一道题
- 关于jvm的一道题
- 一道关于信号的题
- 关于蓝桥杯的一道题
- 关于textarea的一道题
- mooc中做的一道题
- 最近做的一道交互题
- RHEL5下用Xmanager配置远程连接
- 10-07-22
- 究竟是天气作怪还是人的懒惰心理
- Hello, MatrixWorld!
- 字符指针与字符串变量的转换
- 谁说不能这样做题之关于一道题的四种实现,POJ 2263
- Oracle常用脚本
- Linux下用vsftpd构建FTP服务器
- cannot simultaneously fetch multiple bags 解决方式
- 嵌入式Linux中使用动态和静态编译的有趣现象
- EVC编程知识点
- Win7下用户界面特权隔离下低等级进程向高等级进程发送消息的实现
- CSS中A标签的a:link a:visited a:hover a:active 原来是有严格的顺序的
- 学习笔记之使用域名创建虚拟主机