-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path207.cpp
More file actions
175 lines (164 loc) · 3.53 KB
/
Copy path207.cpp
File metadata and controls
175 lines (164 loc) · 3.53 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
#include<iostream>
#include<queue>
#include<string>
using namespace std;
struct plc {
string name = "";
int index;
bool confirmed;
int adj[200];
int adjDist[200];
int adjCount = 0;
int previous;
int dist;
};
struct compare {
bool operator()(const plc &a, const plc &b){
return a.dist > b.dist;
}
};
plc* place;
int n;
int office;
int hall;
int ans;
string routine;
void init() {
for (int i = 0; i < n; i++) {
place[i].index = i;
place[i].confirmed = false;
place[i].dist = 2147483647;
}
}
void print(int start, int end) {
if (place[end].previous != start) {
print(start, place[end].previous);
}
routine = routine + " -> " + place[end].name;
}
void seperate(const string& line, int& place1, int& place2, int& dist1, int& dist2) {
for (int i = 0; i < n; i++) {
bool isThis = true;
for (int j = 0; j < place[i].name.length(); j++) {
if (place[i].name[j] != line[j]) {
isThis = false;
break;
}
}
if (isThis) {
place1 = i;
break;
}
}
for (int i = 0; i < n; i++) {
bool isThis = true;
for (int j = 0; j < place[i].name.length(); j++) {
if (place[i].name[j] != line[j + place[place1].name.length() + 1]) {
isThis = false;
break;
}
}
if (isThis) {
place2 = i;
break;
}
}
int pos = place[place1].name.length() + place[place2].name.length() + 2;
dist1 = 0;
dist2 = 0;
while (pos < line.length() && line[pos] != ' ') {
dist1 = dist1 * 10 + int(line[pos++]) - 48;
}
if (pos < line.length()) {
pos++;
while (pos < line.length()) {
dist2 = dist2 * 10 + int(line[pos++]) - 48;
}
}
else {
dist2 = -1;
}
}
int main() {
cin >> caseNum;
for (int c=0;c<caseNum;c++) {
ans = 0;
place = new plc[n];
for (int i = 0; i < n; i++) {
getline(cin, place[i].name);
if (place[i].name == "office") {
office = i;
}
if (place[i].name == "hall") {
hall = i;
}
}
init();
int m;
cin >> m;
getline(cin, whyOnEarthMustGetlineGetTheFuckingNewLineCharacter);
for (int i = 0; i < m; i++) {
string line;
int place1, place2, dist1, dist2;
getline(cin, line);
seperate(line, place1, place2, dist1, dist2);
place[place1].adj[place[place1].adjCount] = place2;
place[place1].adjDist[place[place1].adjCount++] = dist1;
if (dist2 > -1) {
place[place2].adj[place[place2].adjCount] = place1;
place[place2].adjDist[place[place2].adjCount++] = dist2;
}
}
priority_queue<plc, vector<plc>, compare> pq;
place[office].dist = 0;
pq.push(place[office]);
while (!pq.empty()) {
plc t = pq.top();
pq.pop();
if (t.index == hall) {
print(office, hall);
ans += t.dist;
break;
}
if (place[t.index].confirmed) {
continue;
}
place[t.index].confirmed = true;
for (int i = 0; i < t.adjCount; i++) {
if (t.adjDist[i] + t.dist < place[t.adj[i]].dist) {
place[t.adj[i]].dist = t.adjDist[i] + t.dist;
place[t.adj[i]].previous = t.index;
pq.push(place[t.adj[i]]);
}
}
}
while (!pq.empty()) {
pq.pop();
}
init();
place[hall].dist = 0;
pq.push(place[hall]);
while (!pq.empty()) {
plc t = pq.top();
pq.pop();
if (t.index == office) {
print(hall, office);
ans += t.dist;
cout << ans << endl << routine << endl << endl;
break;
}
if (place[t.index].confirmed) {
continue;
}
place[t.index].confirmed = true;
for (int i = 0; i < t.adjCount; i++) {
if (t.adjDist[i] + t.dist < place[t.adj[i]].dist) {
place[t.adj[i]].dist = t.adjDist[i] + t.dist;
place[t.adj[i]].previous = t.index;
pq.push(place[t.adj[i]]);
}
}
}
delete[] place;
}
}