/
shortest.cpp
226 lines (184 loc) · 4.48 KB
/
shortest.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "stdafx.h"
#include <limits.h>
#include <iostream>
using namespace std;
int min(int x, int y) { return (x < y)? x : y; }
struct edge { int x; int y; };
// 크기비교 x를 우선 비교하고 같은 경우는 y를 비교
bool operator == (edge e1, edge e2) {
return e1.x == e2.x && e1.y == e2.y;
}
bool operator < (edge e1, edge e2) {
if (e1.x < e2.x) return true;
if (e1.x == e2.x) return e1.y < e2.y;
return false;
}
bool operator > (edge e1, edge e2) {
if (e1.x > e2.x) return true;
if (e1.x == e2.x) return e1.y > e2.y;
return false;
}
bool operator <= (edge e1, edge e2) {
return e1 < e2 || e1 == e2;
}
bool operator >= (edge e1, edge e2) {
return e1 > e2 || e1 == e2;
}
struct entry { edge key; int value; };
struct list { entry* data; list* next; };
list* cons(entry* e, list* p) {
return new list({e,p});
}
struct tree { entry* data; tree* left; tree* right; };
tree* node(entry* e, tree* tl, tree* tr) {
return new tree({ e, tl, tr });
}
list* append(list* l1, list* l2) {
if (l1 == NULL) return l2;
return cons(l1->data, append(l1->next, l2));
}
list* inorder(tree* t) {
if (t == NULL) return NULL;
list* l1 = inorder(t->left);
list* l2 = inorder(t->right);
return append(l1, cons(t->data, l2));
}
entry* minBST(tree* t) {
if (t == NULL) return NULL;
tree* p = t;
while (p->left) p = p->left;
return p->data;
}
entry* maxBST(tree* t) {
if (t == NULL) return NULL;
tree* p = t;
while (p->right) p = p->right;
return p->data;
}
int isBST(tree* t) {
if (t == NULL) return 1;
if (t->left && maxBST(t->left) >= t->data) return 0;
if (t->right && minBST(t->right) <= t->data) return 0;
return isBST(t->left) && isBST(t->right);
}
/*
void printBST(tree* t) {
if (t == NULL) { cout << "{}"; }
else {
cout << t->data << " -> { ";
printBST(t->left);
cout << ", ";
printBST(t->right);
cout << "}";
}
}*/
entry* searchBST(edge e, tree* t) { // 반복문으로 작성
while (t) // t!=NULL
{
if (e == t->data->key) return t->data; // 있다
if (e < t->data->key)
t = t->left;
else /* if (e > t->data->key) */
t = t->right;
}
return NULL;
}
// 자습: 반복문으로 바꿔보기 (제출은 안해도 됨)
tree* insertBST(entry* e, tree* t) {
if (t == NULL) return node(e, NULL, NULL);
if (e->key < t->data->key) {
if (t->left == NULL)
t->left = node(e, NULL, NULL);
else
insertBST(e, t->left);
return t;
}
if (e->key > t->data->key) {
if (t->right == NULL)
t->right = node(e, NULL, NULL);
else
insertBST(e, t->right);
return t;
}
// if (n == t->data)
return t;
}
tree* deleteBST(edge e, tree* t) {
if (t == NULL) return t;
if (e == t->data->key) { // root를 지움
if (t->right) { // 오른쪽의 최소값 가져오기
entry* m = minBST(t->right);
tree* rt = deleteBST(m->key, t->right);
return node(m, t->left, rt);
}
else if (t->left) { // 왼쪽 최대값 가져오기
entry* m = maxBST(t->left);
tree* lt = deleteBST(m->key, t->left);
return node(m, lt, t->right);
} // t->left == NULL && t->right == NULL
else return NULL;
}
else if (e < t->data->key) {
t->left = deleteBST(e, t->left);
return t;
}
else /*if (e > t->data->key)*/ {
t->right = deleteBST(e, t->right);
return t;
}
}
struct Map {
tree* t;
entry* find(edge k) { return searchBST(k, t); }
entry* update(edge k, int v) {
entry* p = find(k);
if (p) p->value = v;
else {
p = new entry({k,v});
t = insertBST(p, t);
}
return p;
}
// Map을 inorder 리스트로 변환해주는 함수
list* to_list() { return inorder(t); }
};
int main()
{
int N = 3; // V의 크기
Map m = { NULL };
m.update({0,1}, 1);
m.update({1,2}, 2);
m.update({0,2}, 1);
list* e = m.to_list(); // 그래프의 원래 edge 기억
for (int k=1; k<N; ++k) {
// 모든 E의 (u,v)에 대해서
for (list* p = e; p; p = p->next) {
int u = p->data->key.x;
int v = p->data->key.y;
if (u==v) continue;
int n1 = p->data->value;
// w를 V의 범위에 대해서 for
for (int w = 0; w < N; ++w) {
if (u==w || v==w) continue;
entry* e2 = m.find({ v, w });
if (e2==NULL) continue;
int n2 = e2->value;
entry* e3 = m.find({ u, w });
if (e3) {
int n3 = e3->value;
m.update({ u, w }, min(n1+n2, n3));
} else // if (e3==NULL)
m.update({ u, w }, n1+n2);
}
}
}
cout << "Printing m" << endl;
for (list* p = m.to_list(); p; p = p->next) {
cout << "(" << p->data->key.x
<< "," << p->data->key.y << ")->"
<<p->data->value <<" ";
}
cout << endl;
getchar();
return 0;
}