-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAL Homework.cpp
More file actions
258 lines (224 loc) · 7.87 KB
/
AL Homework.cpp
File metadata and controls
258 lines (224 loc) · 7.87 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
int V, E;
// V: 도시 개수 (정점 개수)
// E: 도로 개수 (간선 개수)
unordered_map<string, int> cityIdx;
vector<string> IdxtoCity;
// 도시 이름을 인덱스에 매핑
// IdxtoCity[i] 는 인덱스 i 에 해당하는 도시 이름을 저장
// cityIdx[city_name] 는 해당 도시 이름의 인덱스를 반환
vector<int> cityHeight;
// 도시별 고도
struct Edge {
int to; // 연결된 도시의 인덱스
int length; // 도로 길이
int timestamp; // 도로 개통 시기 (YYYYMM)
int heightDiff; // 도시 간 고도차 절댓값
};
// 도로(간선) 정보 저장
vector<vector<Edge>> adjList;
// 인접 리스트, adjList[u] 는 도시 u와 연결된 모든 도로 정보 저장
vector<bool> visited;
// 방문 여부 표시 변수
int parseTimestamp(string inputStr) {
try {
int y = stoi(inputStr.substr(0, 4));
int m = stoi(inputStr.substr(5, 2));
return y * 100 + m;
}
catch (const std::invalid_argument& e) {
cerr << "invalid_argument : " << e.what() << std::endl;
}
catch (const std::out_of_range& e) {
cerr << "out_of_range : " << e.what() << std::endl;
}
};
// 문자열 "YYYY-MM" 형태를 정수 YYYYMM으로 변환
pair<string, string> getCityLexPair(int U, int V) {
string NameofU = IdxtoCity[U];
string NameofV = IdxtoCity[V];
if (NameofU < NameofV) {
return { NameofU, NameofV };
}
else {
return { NameofV, NameofU };
}
};
// 두 도시 인덱스 U, V에 대해 사전순으로 작은 도시 이름과 큰 도시 이름 반환
struct EdgePQ {
int U, V;
// MST에 포함된 정점 U, 인접한 정점 V
int Length;
int timeStamp;
int heightDiff;
// 간선 가중치 정보: 길이, 개통 시기, 고저차
EdgePQ(int _u, int _v, int _length, int _timestamp, int _heightDiff) {
U = _u;
V = _v;
Length = _length;
timeStamp = _timestamp;
heightDiff = _heightDiff;
}
// 생성자
};
// EdgePQ: Prim 알고리즘에서 우선순위 큐에 넣을 간선 정보 구조체
struct EdgeCompare {
bool operator()(const EdgePQ& a, const EdgePQ& b) const {
if (a.timeStamp != b.timeStamp) {
return a.timeStamp > b.timeStamp;
}
// 오래된 간선일수록 우선순위 높음
if (a.Length != b.Length) {
return a.Length > b.Length;
}
// 짧은 길이일수록 우선순위 높음
if (a.heightDiff != b.heightDiff) {
return a.heightDiff > b.heightDiff;
}
// 고저차가 작은 간선이 우선순위 높음
string cityA_a, cityB_a;
string cityA_b, cityB_b;
tie(cityA_a, cityB_a) = getCityLexPair(a.U, a.V);
tie(cityA_b, cityB_b) = getCityLexPair(b.U, b.V);
// 사전순으로 도시 이름 비교
if (cityA_a != cityA_b) {
return cityA_a > cityA_b;
}
// cityA가 다르면 cityA 비교
else if (cityB_a != cityB_b) {
return cityB_a > cityB_b;
}
// cityA가 같으면 cityB 비교
else {
return false;
}
// 모든 조건이 같으면 우선순위 동일
};
};
// EdgeCompare : EdgePQ를 우선순위 큐에 넣을 때 사용할 비교자
void PrimMST(int startIdx) {
visited.resize(V);
for (int i = 0; i < V; i++) {
visited[i] = false;
}
// visited 초기화
struct ResultEdge {
int timestamp_rs = 0;
string cityA_rs, cityB_rs = "";
int length_rs = 0;
};
vector<ResultEdge> resultEdges;
long long totalLength = 0;
// resultEdges: MST 결과값 저장 벡터
priority_queue<EdgePQ, vector<EdgePQ>, EdgeCompare> PQ;
// EdgePQ를 저장할 우선순위 큐
visited[startIdx] = true;
for (const Edge& edg : adjList[startIdx]) {
PQ.push(EdgePQ(startIdx, edg.to, edg.length, edg.timestamp, edg.heightDiff));
};
// 시작 도시의 인접한 모든 간선을 우선순위 큐에 추가
int edgesNeeded = V - 1;
while (edgesNeeded > 0 && !PQ.empty()) {
EdgePQ topEdge = PQ.top();
PQ.pop();
int u = topEdge.U;
int v = topEdge.V;
if (visited[v]) continue;
// 이미 방문한 도시라면 건너뜀
visited[v] = true;
edgesNeeded--;
// 새로운 도시 v를 MST에 추가
string cityA_output, cityB_output;
tie(cityA_output, cityB_output) = getCityLexPair(u, v);
cout << cityA_output << " " << cityB_output << "\n";
// 도시 이름 출력
ResultEdge res;
res.timestamp_rs = topEdge.timeStamp;
res.cityA_rs = cityA_output;
res.cityB_rs = cityB_output;
res.length_rs = topEdge.Length;
resultEdges.push_back(res);
// 결과 간선 정보 저장
totalLength += topEdge.Length;
// 총 길이 업데이트
for (const Edge& edg2 : adjList[v]) {
int next = edg2.to;
if (!visited[next]) {
PQ.push(EdgePQ(v, next, edg2.length, edg2.timestamp, edg2.heightDiff));
}
}
// 도시와 연결된 모든 간선을 우선순위 큐에 추가
};
// edgesNeeded = 0이 되거나, 우선순위 큐가 빌때까지 반복
cout << totalLength << "\n";
// 총 길이 출력
sort(resultEdges.begin(), resultEdges.end(), [](const ResultEdge& a, const ResultEdge& b) {
if (a.cityA_rs != b.cityA_rs)
return a.cityA_rs < b.cityA_rs;
// cityA가 다르면 cityA 오름차순
else
return a.cityB_rs < b.cityB_rs;
}); // cityA가 같으면 cityB 오름차순
// 간선들을 cityA 오름차순, cityB 오름차순으로 정렬
for (const ResultEdge& results : resultEdges) {
int ts = results.timestamp_rs;
int year = ts / 100;
int month = ts % 100;
ostringstream oss{};
oss << std::setw(4) << std::setfill('0') << year << "-"
<< std::setw(2) << std::setfill('0') << month;
cout << oss.str()
<< " " << results.cityA_rs
<< " " << results.cityB_rs
<< " " << results.length_rs
<< "\n";
};
// 결과 출력: YYYY-MM cityA cityB length
};
// PrimMST: Prim의 알고리즘을 사용하여 MST 계산 및 출력
int main() {
cin >> V;
IdxtoCity.resize(V);
cityHeight.resize(V);
// 도시 개수 V 입력
for (int i = 0; i < V; i++) {
string cityName;
int height;
cin >> cityName >> height;
cityIdx[cityName] = i; // 도시 이름과 인덱스 매핑
IdxtoCity[i] = cityName; // 인덱스 i에 해당하는 도시 이름 저장
cityHeight[i] = height; // 도시 고도 저장
};
// 도시별 정보 입력
cin >> E;
adjList.resize(V);
// 도로 개수 E 입력
for (int i = 0; i < E; i++) {
string cityA, cityB, inputStr;
int length_main = 0;
cin >> cityA >> cityB >> length_main >> inputStr;
int u_main = cityIdx[cityA];
int v_main = cityIdx[cityB];
int timestamp_main = parseTimestamp(inputStr);
int heightDiff_main = abs(cityHeight[u_main] - cityHeight[v_main]);
// 도시 이름을 인덱스로 변환, 도로 개통 시기와 고도차 계산
adjList[u_main].push_back(Edge{ v_main, length_main, timestamp_main, heightDiff_main });
adjList[v_main].push_back(Edge{ u_main, length_main, timestamp_main, heightDiff_main });
};
// 도로 정보 입력
string startCity;
cin >> startCity;
int startIdx = cityIdx[startCity];
// 시작 도시 입력
PrimMST(startIdx);
// Prim의 알고리즘 수행
return 0;
};