-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpStudents.cpp
More file actions
285 lines (244 loc) · 8.31 KB
/
Copy pathHelpStudents.cpp
File metadata and controls
285 lines (244 loc) · 8.31 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "HelpStudents.h"
#include <set>
#include<bits/stdc++.h>
using namespace std;
HelpStudents::HelpStudents(int N, int M, int K, vector < pair< pair <int,int> , int > > ways) {
// IMPLEMENT ME!
this->K = K;
this->ways = ways;
this-> N= N;
this->M = M;
adjacencyList.resize(N+1);
for(int i=0; i<this->M;i++) {
long long int x = (ways[i].first).first;
long long int y = (ways[i].first).second;
long long int weight = ways[i].second;
adjacencyList[x].push_back(make_pair(y,weight));
adjacencyList[y].push_back(make_pair(x,weight));
}
}
long long int HelpStudents::firstStudent() {
set<pair<long long int,long long int>,less<>> s;
vector<long long int> dist(N+1,-1);
s.insert(make_pair(0,1));
dist[1]=0;
while(!s.empty()) {
pair<long long int, long long int> tmp = *(s.begin());
s.erase(s.begin());
long long int v = tmp.second;
vector<pair<long long int,long long int>>::iterator i;
for( i=adjacencyList[v].begin(); i!=adjacencyList[v].end();++i) {
long long int node = (*i).first;
long long int weight = (*i).second;
if(dist[node]>dist[v]+weight) {
s.erase(s.find(make_pair(dist[node],node)));
dist[node] = dist[v] + weight;
s.insert(make_pair(dist[node], node));
}
else if(dist[node]<0) {
dist[node] = dist[v]+weight;
s.insert(make_pair(dist[node], node));
}
}
}
long long int a = dist[K];
return a;
// IMPLEMENT ME!
}
long long int HelpStudents::secondStudent() {
set<pair<long long int,long long int>,less<>> s;
vector<long long int> parent(N+1,-1);
vector<bool> visited(N+1,false);
vector<long long int> dist(N+1,-1);
s.insert(make_pair(0,1));
dist[1]=0;
parent[1]=0;
while(!s.empty()) {
pair<long long int, long long int> tmp = *(s.begin());
s.erase(s.begin());
long long int v = tmp.second;
visited[v]=true;
vector<pair<long long int,long long int>>::iterator i;
for( i=adjacencyList[v].begin(); i!=adjacencyList[v].end();++i) {
long long int node = (*i).first;
long long int weight = (*i).second;
if(!visited[node]) {
if (dist[node] > weight) {
s.erase(s.find(make_pair(dist[node], node)));
dist[node] = weight;
s.insert(make_pair(dist[node], node));
parent[node] = v;
} else if (dist[node] < 0) {
dist[node] = weight;
s.insert(make_pair(dist[node], node));
parent[node] = v;
}
}
}
}
long long int max=0;
long long int par = parent[K];
long long int distance = dist[K];
while(true) {
if(par==0) {
break;
}
if(distance>max) {
max = distance;
}
distance = dist[par];
par = parent[par];
}
return max;
// IMPLEMENT ME!
}
long long int HelpStudents::thirdStudent() {
set<pair<long long int,long long int>,less<>> s;
vector<long long int> parent(N+1,-1);
vector<bool> visited(N+1,false);
vector<long long int> dist(N+1,-1);
s.insert(make_pair(0,1));
dist[1]=0;
parent[1]=0;
while(!s.empty()) {
pair<long long int, long long int> tmp = *(s.begin());
s.erase(s.begin());
long long int v = tmp.second;
visited[v]=true;
vector<pair<long long int,long long int>>::iterator i;
for( i=adjacencyList[v].begin(); i!=adjacencyList[v].end();++i) {
long long int node = (*i).first;
if(!visited[node]) {
if (dist[node] > dist[v]+1) {
s.erase(s.find(make_pair(dist[node], node)));
dist[node] = dist[v]+1;
s.insert(make_pair(dist[node], node));
parent[node] = v;
} else if (dist[node] < 0) {
dist[node] =dist[v]+1;
s.insert(make_pair(dist[node], node));
parent[node] = v;
}
}
}
}
return dist[K];
// IMPLEMENT ME!
}
long long int HelpStudents::fourthStudent() {
long long int total = 0;
long long int v = 1;
while(true) {
if(v==K) {
break;
}
vector<pair<long long int,long long int>>::iterator i;
long long int min = LONG_LONG_MAX;
long long int minNode = 0;
long long int index = -1;
long long int minIndex = 0;
if(adjacencyList[v].empty()) {
return -1;
}
for( i=adjacencyList[v].begin(); i!=adjacencyList[v].end();++i) {
index++;
long long int node = (*i).first;
long long int weight = (*i).second;
if (weight < min) {
min = weight;
minNode = node;
minIndex = index;
} else if (weight == min) {
if (node < minNode) {
minNode = node;
minIndex = index;
}
}
}
adjacencyList[v].erase(adjacencyList[v].begin()+minIndex);
for( i=adjacencyList[minNode].begin(); i!=adjacencyList[minNode].end();++i) {
long long int node = (*i).first;
if(node==v) {
adjacencyList[minNode].erase(i);
break;
}
}
v = minNode;
total+=min;
}
return total;
// IMPLEMENT ME!
}
long long int HelpStudents::fifthStudent() {
set<pair<long long int,pair<long long int,long long int>>,less<>> s;
vector<vector<long long int>> dist(N+1);
vector<vector<long long int>> powCount(N+1);
for(int i=0;i<N+1;i++) {
dist[i].resize(3); powCount[i].resize(3);
dist[i][0]=-1; powCount[i][0]=0;
dist[i][1]=-1; powCount[i][1]=0;
dist[i][2]=-1; powCount[i][2]=0;
}
dist[1][0]=0; powCount[1][0]=1;
dist[1][1]=0;
dist[1][2]=0;
s.insert(make_pair(0,make_pair(1,0)));
while(!s.empty()) {
pair<long long int, long long int> tmp = (s.begin())->second;
long long int dis = (s.begin())->first;
long long int pow = tmp.second;
s.erase(s.begin());
long long int v = tmp.first;
vector<pair<long long int,long long int>>::iterator i;
for( i=adjacencyList[v].begin(); i!=adjacencyList[v].end();++i) {
long long int node = (*i).first;
long long int weight = (*i).second;
if(pow==0) {
if(weight>2*stepBack(v)) {
weight = 2*stepBack(v);
}
}
if(pow ==2) {
weight = 0;
}
long long int pow2=pow+1;
if(pow==2) {
pow2=0;
}
if(powCount[node][pow2]!=0) {
if (dist[node][pow2] > dis + weight) {
s.erase(s.find(make_pair(dist[node][pow2], make_pair(node,pow2))));
dist[node][pow2]= dis + weight;
s.insert(make_pair(dist[node][pow2], make_pair(node, pow2)));
} else if (dist[node][pow2] < 0) {
dist[node][pow2]= dis + weight;
powCount[node][pow2]=1;
s.insert(make_pair(dist[node][pow2], make_pair(node, pow2)));
}
} else {
dist[node][pow2]= dis + weight;
powCount[node][pow2]=1;
s.insert(make_pair(dist[node][pow2], make_pair(node, pow2)));
}
}
}
long long int min= LONG_LONG_MAX;
for(int k =0; k<3; k++) {
if(dist[K][k]<min && dist[K][k]!=-1) {
min = dist[K][k];
}
}
return min;
// IMPLEMENT ME!
}
long long int HelpStudents::stepBack(long long int node) {
vector<pair<long long int,long long int>>::iterator i;
long long int min = LONG_LONG_MAX;
for( i=adjacencyList[node].begin(); i!=adjacencyList[node].end();++i) {
if((*i).second < min) {
min = (*i).second;
}
}
return min;
}
// YOU CAN ADD YOUR HELPER FUNCTIONS