为了这个作业我把《算法导论》的 22章“图的基本算法”23章“最小生成树”24章“单源最短路径”25章“每对顶点间的最短路径”全部看了一边,光是理论就看了一周的时间(晚上看的啊)
主要内容:
1 城市之间的有向道路
2 两个城市之间的最短路径(经过的城市数量)
3 两个城市之间的最短路程(道路长度之和)
4 顺序遍历所有城市
5 从一个城市出发到另一个城市最短回路
6 自动跑完测试用例
7 打印城市和道路
百度云
完全是对着书上的伪代码实现,不得不承认,此书理论相当棒,伪代码超简单,给实现带来了相当的方便。
头文件:
#pragma once
#include
#include
#include
#include
#include
main文件:
#include "AdjacencyMatrixGraph.h"
#include
#include
#include
实现和输出:
Choose your menu please:
0. Display all airports and flights information
1. Display one airport information
2. Find a cheapest flight from one airport to another airport
3. Add a flight from one airport to another airport
4. Delete a flight from one airport to another airport
5. Find a cheapest roundtrip from one airport to another airport
6. Find a flight with fewest stops from one airport to another airport
9. Add a new airport
10. Run all test cases for developer(not user)
Q. Exit and save data
10
Test case is start running.....
1. Run option 0
All airports in Graph:
0[airport]LAX [cityname]Los Angeles
1[airport]SFO [cityname]San Francisco
2[airport]DFW [cityname]Denver
3[airport]ORD [cityname]Chicago
4[airport]BOS [cityname]Boston
5[airport]JFK [cityname]New York
6[airport]MIA [cityname]Miami
7[airport]MSY [cityname]New Orlean
8[airport]SEA [cityname]Seatle
All flights in Graph:
0 1 2 3 4 5 6 7 8
0 0 0 189 0 0 0 0 0 200
1 79 0 0 0 0 0 0 0 0
2 199 99.99 0 0 0 0 0 0 0
3 0 0 50 0 179 0 0 0 0
4 0 0 0 149 0 99 0 0 0
5 0 0 0 99 0 0 49 220 0
6 0 0 0 0 0 0 0 50 0
7 190 0 109 0 0 0 0 0 0
8 0 0 0 179.5 0 0 0 0 0
2. Display airport information for SFO
1[airport]SFO [cityname]San Francisco
3. Find the cheapest flight from LAX to JFK
cheapest flight(0,5) = $657.5
cheapest flight(0,5) path:
0[airport]LAX [cityname]Los Angeles
8[airport]SEA [cityname]Seatle
3[airport]ORD [cityname]Chicago
4[airport]BOS [cityname]Boston
5[airport]JFK [cityname]New York
4. Find the cheapest flight from JFK to LAX
cheapest flight(5,0) = $289
cheapest flight(5,0) path:
5[airport]JFK [cityname]New York
6[airport]MIA [cityname]Miami
7[airport]MSY [cityname]New Orlean
0[airport]LAX [cityname]Los Angeles
5. Delete a flight between LAX and SFO
All airports in Graph:
0[airport]LAX [cityname]Los Angeles
1[airport]SFO [cityname]San Francisco
2[airport]DFW [cityname]Denver
3[airport]ORD [cityname]Chicago
4[airport]BOS [cityname]Boston
5[airport]JFK [cityname]New York
6[airport]MIA [cityname]Miami
7[airport]MSY [cityname]New Orlean
8[airport]SEA [cityname]Seatle
All flights in Graph:
0 1 2 3 4 5 6 7 8
0 0 0 189 0 0 0 0 0 200
1 79 0 0 0 0 0 0 0 0
2 199 99.99 0 0 0 0 0 0 0
3 0 0 50 0 179 0 0 0 0
4 0 0 0 149 0 99 0 0 0
5 0 0 0 99 0 0 49 220 0
6 0 0 0 0 0 0 0 50 0
7 190 0 109 0 0 0 0 0 0
8 0 0 0 179.5 0 0 0 0 0
6. Add a flight between DFW and JFK for $200.00
All airports in Graph:
0[airport]LAX [cityname]Los Angeles
1[airport]SFO [cityname]San Francisco
2[airport]DFW [cityname]Denver
3[airport]ORD [cityname]Chicago
4[airport]BOS [cityname]Boston
5[airport]JFK [cityname]New York
6[airport]MIA [cityname]Miami
7[airport]MSY [cityname]New Orlean
8[airport]SEA [cityname]Seatle
All flights in Graph:
0 1 2 3 4 5 6 7 8
0 0 0 189 0 0 0 0 0 200
1 79 0 0 0 0 0 0 0 0
2 199 99.99 0 0 0 200 0 0 0
3 0 0 50 0 179 0 0 0 0
4 0 0 0 149 0 99 0 0 0
5 0 0 0 99 0 0 49 220 0
6 0 0 0 0 0 0 0 50 0
7 190 0 109 0 0 0 0 0 0
8 0 0 0 179.5 0 0 0 0 0
7. Find the cheapest roundtrip from LAX to JFK
cheapest flight(0,5) = $389
cheapest flight(0,5) path:
0[airport]LAX [cityname]Los Angeles
2[airport]DFW [cityname]Denver
5[airport]JFK [cityname]New York
cheapest flight(5,0) = $289
cheapest flight(5,0) path:
5[airport]JFK [cityname]New York
6[airport]MIA [cityname]Miami
7[airport]MSY [cityname]New Orlean
0[airport]LAX [cityname]Los Angeles
cheapest total cost = $678
8. Run option 0
All airports in Graph:
0[airport]LAX [cityname]Los Angeles
1[airport]SFO [cityname]San Francisco
2[airport]DFW [cityname]Denver
3[airport]ORD [cityname]Chicago
4[airport]BOS [cityname]Boston
5[airport]JFK [cityname]New York
6[airport]MIA [cityname]Miami
7[airport]MSY [cityname]New Orlean
8[airport]SEA [cityname]Seatle
All flights in Graph:
0 1 2 3 4 5 6 7 8
0 0 0 189 0 0 0 0 0 200
1 79 0 0 0 0 0 0 0 0
2 199 99.99 0 0 0 200 0 0 0
3 0 0 50 0 179 0 0 0 0
4 0 0 0 149 0 99 0 0 0
5 0 0 0 99 0 0 49 220 0
6 0 0 0 0 0 0 0 50 0
7 190 0 109 0 0 0 0 0 0
8 0 0 0 179.5 0 0 0 0 0
9. Quit
SaveRevisedFlightsData to P4FlightsRev1.txt finished!
SaveRevisedAirportsData to P4AirportsRev1.txt finished!
Thank you for your use. Bye!
Test case is finished running .....
请按任意键继续. . .