-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimumEffort207_1.cpp
More file actions
150 lines (144 loc) · 3.17 KB
/
MinimumEffort207_1.cpp
File metadata and controls
150 lines (144 loc) · 3.17 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
#include <iostream>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <list>
#include <functional>
#define INF 0x5FFFFFFF;//Max Value
using namespace std;
list<int> vertices;
int dist[200] ;
typedef pair<int, int> Edge;//First value represents distance becuase by default first element is compared
string input[200];
list <pair<int, int> > *graph = new list<Edge>[200];
int parent[200];
priority_queue <Edge, vector<Edge>, greater<Edge> > pq;
void dijkstras() {
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
list<pair<int, int> >::iterator i;
for (i = graph[u].begin(); i != graph[u].end(); i++) {
int v = i->second;
int distance = i->first;
if (dist[v] > (dist[u] + distance)) {
dist[v] = dist[u] + distance;
pq.push(make_pair(dist[v], v));
parent[v] = u;
}
}
}
}
int main() {
int ctr = 0;
int n = 0;
string in;
while (getline(cin, in)) {
graph = new list<Edge>[200];
stringstream ss(in);
ss >> n;
int start = 0;
int end = 0;
map <string, int> m;
for (int i = 0; i < n; i++) {
getline(cin, input[i]);
m[input[i]] = i;
parent[i] = -1;
vertices.push_back(i);
if (input[i] == "office") {
dist[i] = 0;
start = i;
continue;
}
else if (input[i] == "hall")
end = i;
dist[i] = INF;
}
int p = 0;
getline(cin, in);
stringstream s1(in);
s1 >> p;
for (int i = 0; i < p; i++) {
bool flag = true;
map <string, int>::iterator it;
map <string, int>::iterator it1;
map <string, int>::iterator it2;
string str;
string p1;
string p2;
int w1 = 0;
int w2 = 0;
getline(cin, str);
int j = 0;
for (; j < (int)str.length(); j++) {
if (str[j] == ':') {
flag = false;
continue;
}
if (flag == true) {
p1 += str[j];
continue;
}
if (str[j] == ' '&& str[j + 1] >= '0'&&str[j + 1] <= '9') {//found a number check if it is a part of the string
it2 = m.find(p2);
if (it2 == m.end()) {
p2 += str[j];
continue;
}
else {
w1 = str[++j] - '0';
while (j <(int)str.length() && str[++j] >= '0'&&str[j] <= '9') {
w1 = (w1 * 10) + str[j] - '0';
}
break;
}
}
else
p2 += str[j];
}
it = m.find(p1);
it1 = m.find(p2);
int u = it->second;
int v = it1->second;
graph[u].push_back(make_pair(w1,v));
while ((++j) < (int)str.length()&& str[j]>='0'&&str[j]<='9') {
w2 = (w2*10) + str[j] - '0';
}
if(w2>0)
graph[v].push_back(make_pair(w2, u));
}
pq.push(make_pair(0, start));
dijkstras();
string output = " -> hall ";
int x = parent[end];
while (parent[x] != -1) {
output = " -> " + input[x] + output;
x = parent[x];
}
output = "office" + output;
int distance = dist[end];
for (int i = 0; i < n; i++) {
if (i == end) {
dist[i] = 0;
}
else
dist[i] = INF;
parent[i] = -1;
}
pq.push(make_pair(0, end));
dijkstras();
string output2;
output2="-> office";
distance += dist[start];
x = parent[x];
while (parent[x] != -1) {
output2 = "-> " + input[x] + " " + output2;
x = parent[x];
}
output += output2;
cout << distance << endl;
cout << output << endl << endl;
}
return 0;
}