-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
113 lines (105 loc) · 2.34 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "graph.h"
#include "dijkstra.h"
#include "bellmanFord.h"
#include "floydWarshall.h"
#include "johnson.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
//测试dijkstra算法
graph G(5);
G.addEdge(10, 0, 1);
G.addEdge(1, 1, 2);
G.addEdge(2, 1, 4);
G.addEdge(3, 4, 1);
G.addEdge(9, 4, 2);
G.addEdge(4, 2, 3);
G.addEdge(6, 3, 2);
G.addEdge(7, 3, 0);
G.addEdge(5, 0, 4);
G.addEdge(2, 4, 3);
vector<double>dist;
vector<int>pre;
dist = dijkstra(G, 0, pre);
cout << "Dijkstra:" << endl;
for (auto iter = dist.begin(); iter != dist.end(); iter++)
{
cout << (*iter) << " ";
}
cout << endl;
for (auto iter = pre.begin(); iter != pre.end(); iter++)
{
cout << (*iter) << " ";
}
cout << endl;
//dijkstra 示例输出结果正确
G = graph(5);
G.addEdge(6, 0, 1);
G.addEdge(5, 1, 2);
G.addEdge(-2, 2, 1);
G.addEdge(8, 1, 4);
G.addEdge(7, 3, 2);
G.addEdge(-3, 4, 2);
G.addEdge(-4, 1, 3);
G.addEdge(2, 3, 0);
G.addEdge(7, 0, 4);
G.addEdge(9, 4, 3);
bellmanFord(G, 0, dist, pre);
cout << "BellmanFord:" << endl;
for (auto iter = dist.begin(); iter != dist.end(); iter++)
{
cout << (*iter) << " ";
}
cout << endl;
for (auto iter = pre.begin(); iter != pre.end(); iter++)
{
cout << (*iter) << " ";
}
cout << endl;
//bellmanFord 示例输出结果正确
G = graph(5);
G.addEdge(3, 0, 1);
G.addEdge(8, 0, 2);
G.addEdge(-4, 0, 4);
G.addEdge(1, 1, 3);
G.addEdge(7, 1, 4);
G.addEdge(4, 2, 1);
G.addEdge(2, 3, 0);
G.addEdge(-5, 3, 2);
G.addEdge(6, 4, 3);
vector<vector<int> >pre_allPair;
auto matrix = floydWarshall(G, pre_allPair);
cout << "floyd:" << endl;
for (int i = 0; i < G.size(); i++)
{
for (int j = 0; j < G.size(); j++)
cout << matrix[i][j] << " ";
cout << endl;
}
cout << "pre:" << endl;
for (int i = 0; i < G.size(); i++)
{
for (int j = 0; j < G.size(); j++)
cout << pre_allPair[i][j] << " ";
cout << endl;
}
johnson(G, matrix, pre_allPair);
cout << "johnson:" << endl;
for (int i = 0; i < G.size(); i++)
{
for (int j = 0; j < G.size(); j++)
cout << matrix[i][j] << " ";
cout << endl;
}
cout << "pre:" << endl;
for (int i = 0; i < G.size(); i++)
{
for (int j = 0; j < G.size(); j++)
cout << pre_allPair[i][j] << " ";
cout << endl;
}
system("pause");
return 0;
}