-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
157 lines (133 loc) · 3.18 KB
/
test.cpp
File metadata and controls
157 lines (133 loc) · 3.18 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <vector>
#include <unordered_set>
#define Max 503
#define INF 0xcffffff
using namespace std;
typedef struct AMGraph
{
//定义图
int vex, arc;
int arcs[Max][Max]; //邻接矩阵
};
int dis[Max], path[Max]; //dis保存最短路径总权值、path通过保存路径的前驱结点来保存路径
bool book[Max]; //已找到最短路集合
void Dijkstra(AMGraph &G) //迪杰斯特拉算法
{
for (int i = 1; i <= G.vex; i++)
{
dis[i] = G.arcs[1][i]; //初始化dis数组
path[i] = dis[i] < INF ? 1 : -1; //初始化路径数组
}
book[1] = true;
dis[1] = 0; //起点初始化
for (int i = 2; i <= G.vex; i++) //遍历G.vex-1次
{
int mins = INF, u = 1;
for (int j = 1; j <= G.vex; j++) //找到当前没加入集合的最短路的后驱点
{
if (!book[j] && mins > dis[j])
{
mins = dis[j];
u = j;
}
}
book[u] = true; //将该点加入集合
for (int j = 1; j <= G.vex; j++) //遍历所有其他点对其最短路进行更新(松弛操作)
{
if (!book[j] && dis[j] > dis[u] + G.arcs[u][j])
{
dis[j] = dis[u] + G.arcs[u][j]; //更新最短路径值
path[j] = u; //修改j的前驱为u
}
}
}
}
void find(int x) //递归输出最短路径
{
if (path[x] == 1)
{
cout << 1;
}
else
{
find(path[x]);
}
cout << " -> " << x;
return;
}
void putin(AMGraph &G) //输入图
{
cin >> G.vex >> G.arc;
for (int i = 1; i <= G.vex; i++) //初始化邻接矩阵
for (int j = 1; j <= G.vex; j++)
G.arcs[i][j] = INF;
for (int i = 1; i <= G.arc; i++)
{
int u, v, w;
cin >> u >> v >> w;
G.arcs[u][v] = w;
}
}
void putout(AMGraph &G) //输出
{
//cout << "起点 v1 到各点的最短路程为: \n";
for (int i = 1; i < G.vex; i++)
{
cout << dis[i] << " ";
}
cout << dis[G.vex] << endl;
/*for (int i = 2; i <= G.vex; i++)
{
cout << "起点 v1 到 v" << i << " 的路径为: ";
find(i);
cout << endl;
}*/
}
int main()
{
string str;
cin >> str;
int vex = 0, arc = 0;
int i = 0;
vector<vector<string>> vec;
unordered_set<char> s;
while (str[i])
{
if (str[i] == '[')
{
++arc;
int start = i + 1;
int end = start;
vector<string> v;
while (str[i] != ']')
{
if (str[i] == ',')
{
v.push_back(str.substr(start, end - start));
}
++i;
}
}
if (str[i] >= 'A' && str[i] <= 'Z')
{
s.insert(str[i]);
}
++i;
}
cout << s.size() << endl;
for (auto a : vec)
{
for (auto b : a)
{
cout << b << "\t";
}
cout << endl;
}
AMGraph G;
putin(G);
Dijkstra(G);
putout(G);
return 0;
}
// [A,B,3] [A,C,7] [C,D,2] [B,C,5]