-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarbie.cpp
More file actions
415 lines (370 loc) · 13 KB
/
barbie.cpp
File metadata and controls
415 lines (370 loc) · 13 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
#include <cassert>
using namespace std;
struct Path {
vector<int> path;
int costo ;
int numberOfLinks;
bool isOccupied ;
bool isNull = false; //usata per ritornare nel caso in cui non siano trovati path possibili
};
struct link{
int nodoTo;
int costo;
};
struct nodo{
vector<link> vic;
bool occupied = false;
int costDistance = -1;
int linkDistance = -1;
};
struct minimumPathPassingThroughOccupiedNodes {
int indiceNodo;
int costoOccupiedPath = -1;
int lenghtOccupiedPath = -1;
};
vector<nodo> grafo;
vector<int> parents;
vector<int> minPath;
vector<minimumPathPassingThroughOccupiedNodes> occupiedNodes;
int C, S, M;
//setta le distanze da 0 a C-1 di grafo globale
void setDistance( ) ;
void printPath( ofstream&, const vector<int> & ) ;
//return true se il path contiene nodi occupied
bool isPathOccupied( const vector<int> ) ;
void printGraph( const vector<nodo> & ) ;
Path minimumCostNonOccupiedPath() ;
Path minimumCostPath() ;
Path minimumDistanceNonOccupiedPath() ;
Path minimunDistancePath() ;
vector<int> getPath(const vector<int> & , const int , int ) ;
void occupiedPath(vector<nodo> &);
int main() {
ifstream input("input.txt") ;
ofstream output("output.txt") ;
input >> C >> S ;
grafo.resize(C) ;
//il primo nodo non ha parent
parents.resize(C) ;
parents[0] = -1;
//get grafo
for (int i = 0; i < S ; i++) {
int from, to, costo;
input >> from >> to >> costo;
grafo[from].vic.push_back({ to, costo }) ;
grafo[to].vic.push_back({from, costo}) ;
}
input >> M;
//se M = 0 vuol dire che non ci sono città occupate quindi printiamo -1 e il minpath
setDistance() ;
minPath = getPath( parents, 0 , C-1 ) ;
if ( M == 0 ) { // GB aggiunto caso per M=0
output << "-1" << endl;
output << minPath.size() << endl;
printPath( output, minPath ) ;
} else if(M==C-2) { //LS aggiunto caso in cui tutte città occupate tranne partenza e arrivo
output << "-2" << endl;
output << minPath.size() << endl;
printPath( output, minPath ) ;
}else{
//get dei nodi occupati
for (int i = 0; i < M; i++) {
int tmp;
input >> tmp ;
grafo[tmp].occupied = true;
minimumPathPassingThroughOccupiedNodes nodeToAdd;
nodeToAdd.indiceNodo = tmp;
occupiedNodes.push_back(nodeToAdd);
}
//printGraph( grafo ) ;
if ( isPathOccupied(minPath) ) { //caso in cui non abbiamo trovato un cammino minimo non occupato
output << "-2" << endl;
output << minPath.size() << endl;
printPath( output, minPath ) ;
} else { //abbiamo trovato un minPath non occupato
int k = 100000*C;
occupiedPath(grafo);
// k = (OccupiedCost - minimumPathCost) / (minimumPathLenght - OccupiedLenght)
for(minimumPathPassingThroughOccupiedNodes pathToConsider : occupiedNodes)
{
/* cout << " i : " << pathToConsider.indiceNodo
<< "costoOccupiedPath: " << pathToConsider.costoOccupiedPath
<< " path.costo: " << grafo[C-1].costDistance
<< " path.numberOfLinks: " << grafo[C-1].linkDistance
<< " lenghtOccupiedPath: " << pathToConsider.lenghtOccupiedPath << endl; */
int tmp = (pathToConsider.costoOccupiedPath - grafo[C-1].costDistance)/(grafo[C-1].linkDistance - pathToConsider.lenghtOccupiedPath);
if(tmp > 0)
{
k = min(k,tmp);
}
}
output << k-1 << endl;
output << minPath.size() << endl;
printPath( output, minPath ) ;
}
}
return 0;
}
Path minimunDistancePath() {
vector<int> localParent;
localParent.resize(C) ;
queue<int> q;
grafo[0].costDistance = 0;
grafo[0].linkDistance = 0;
q.push(0) ;
while ( !q.empty() ) {
int nodoAtt = q.front();
q.pop();
for( link linkVic : grafo[nodoAtt].vic ) {
int nodoVic = linkVic.nodoTo;
int costo = linkVic.costo;
if ( (grafo[nodoVic].linkDistance == -1 ||
grafo[nodoVic].linkDistance > grafo[nodoAtt].linkDistance + 1)
) {
grafo[nodoVic].costDistance = grafo[nodoAtt].costDistance + costo;
grafo[nodoVic].linkDistance = grafo[nodoAtt].linkDistance + 1;
localParent[nodoVic] = nodoAtt;
q.push(nodoVic) ;
}
}
}
vector<int> path;
path = getPath( localParent, 0, C-1 ) ;
Path returnPath ; // isNull è a false di default
returnPath.path = path;
returnPath.costo = grafo[C-1].costDistance;
returnPath.numberOfLinks = grafo[C-1].linkDistance;
returnPath.isOccupied = isPathOccupied( path ) ;
return returnPath;
}
Path minimumDistanceNonOccupiedPath() {
vector<int> localParent;
localParent.resize(C) ;
queue<int> q;
grafo[0].costDistance = 0;
grafo[0].linkDistance = 0;
grafo[C-1].linkDistance = -1; //per sapere se non troveremo path possibili
q.push(0) ;
while ( !q.empty() ) {
int nodoAtt = q.front();
q.pop();
for( link linkVic : grafo[nodoAtt].vic ) {
int nodoVic = linkVic.nodoTo;
int costo = linkVic.costo;
if ( (grafo[nodoVic].linkDistance == -1 ||
grafo[nodoVic].linkDistance > grafo[nodoAtt].linkDistance + 1) &&
!grafo[nodoVic].occupied //ricerco solo nodi non occupati
) {
grafo[nodoVic].costDistance = grafo[nodoAtt].costDistance + costo;
grafo[nodoVic].linkDistance = grafo[nodoAtt].linkDistance + 1;
localParent[nodoVic] = nodoAtt;
q.push(nodoVic) ;
}
}
}
if ( grafo[C-1].linkDistance == -1 ) { //non esiste path non occupato
Path returnPath ;
returnPath.isNull = true;
return returnPath ;
} else { //esiste path non occupato
vector<int> path;
path = getPath( localParent, 0, C-1 ) ;
Path returnPath ; // isNull è a false di default
returnPath.path = path;
returnPath.costo = grafo[C-1].costDistance;
returnPath.numberOfLinks = grafo[C-1].linkDistance;
returnPath.isOccupied = isPathOccupied( path ) ;
return returnPath;
}
}
Path minimumCostPath() {
vector<int> localParent;
localParent.resize(C) ;
queue<int> q;
grafo[0].costDistance = 0;
grafo[0].linkDistance = 0;
q.push(0) ;
while ( !q.empty() ) {
int nodoAtt = q.front();
q.pop();
for( link linkVic : grafo[nodoAtt].vic ) {
int nodoVic = linkVic.nodoTo;
int costo = linkVic.costo;
if ( (grafo[nodoVic].costDistance == -1 ||
grafo[nodoVic].costDistance > grafo[nodoAtt].costDistance + costo)
) {
grafo[nodoVic].costDistance = grafo[nodoAtt].costDistance + costo;
grafo[nodoVic].linkDistance = grafo[nodoAtt].linkDistance + 1 ;
localParent[nodoVic] = nodoAtt;
q.push(nodoVic) ;
}
}
}
vector<int> path;
path = getPath( localParent, 0, C-1 ) ;
Path returnPath ; // isNull è a false di default
returnPath.path = path;
returnPath.costo = grafo[C-1].costDistance;
returnPath.numberOfLinks = grafo[C-1].linkDistance;
returnPath.isOccupied = isPathOccupied( path ) ;
return returnPath;
}
Path minimumCostNonOccupiedPath() {
vector<int> localParent;
localParent.resize(C) ;
queue<int> q;
grafo[0].costDistance = 0;
grafo[0].linkDistance = 0;
grafo[C-1].costDistance = -1; //per sapere se non troveremo path possibili
q.push(0) ;
while ( !q.empty() ) {
int nodoAtt = q.front();
q.pop();
for( link linkVic : grafo[nodoAtt].vic ) {
int nodoVic = linkVic.nodoTo;
int costo = linkVic.costo;
if ( (grafo[nodoVic].costDistance == -1 ||
grafo[nodoVic].costDistance > grafo[nodoAtt].costDistance + costo) &&
!grafo[nodoVic].occupied //ricerco solo nodi non occupati
) {
grafo[nodoVic].costDistance = grafo[nodoAtt].costDistance + costo;
grafo[nodoVic].linkDistance = grafo[nodoAtt].linkDistance + 1;
localParent[nodoVic] = nodoAtt;
q.push(nodoVic) ;
}
}
}
if ( grafo[C-1].costDistance == -1 ) { //non esiste path non occupato
Path returnPath ;
returnPath.isNull = true;
return returnPath ;
} else { //esiste path non occupato
vector<int> path;
path = getPath( localParent, 0, C-1 ) ;
Path returnPath ; // isNull è a false di default
returnPath.path = path;
returnPath.costo = grafo[C-1].costDistance;
returnPath.numberOfLinks = grafo[C-1].linkDistance;
returnPath.isOccupied = isPathOccupied( path ) ;
return returnPath;
}
}
void setDistance( ) {
queue<int> q;
q.push(0) ;
grafo[0].costDistance = 0;
grafo[0].linkDistance = 0;
while ( !q.empty() ) {
int nodoAtt = q.front();
q.pop();
for( link linkVic : grafo[nodoAtt].vic ) {
int nodoVic = linkVic.nodoTo;
int costo = linkVic.costo;
if ( grafo[nodoVic].costDistance == -1 ||
grafo[nodoVic].costDistance > grafo[nodoAtt].costDistance + costo
) {
grafo[nodoVic].linkDistance = grafo[nodoAtt].linkDistance + 1;
grafo[nodoVic].costDistance = grafo[nodoAtt].costDistance + costo;
parents[nodoVic] = nodoAtt;
q.push(nodoVic) ;
}
}
}
}
bool isANeighbour( vector<int> & neighbours, int x ) {
for ( int i : neighbours ) {
if ( i == x ) {
return true;
}
}
return false;
}
void occupiedPath(vector<nodo> & tmp_grafo) {
vector<int> neighboursOf0;
//controlla i nodi adiacenti a 0 funziona solo con cammini disgiunti
for( link l : grafo[0].vic ) {
neighboursOf0.push_back( l.nodoTo ) ;
}
for (int j = 0; j < occupiedNodes.size(); j++)
//minimumPathPassingThroughOccupiedNodes mppton : occupiedNodes
{
int i = occupiedNodes[j].indiceNodo;
if ( isANeighbour( neighboursOf0, i ) ) {
queue<int> q;
q.push(C-1) ;
vector<int> distances(C,-1);
vector<int> costs(C,0);
distances[C-1] = 0;
costs[C-1] = 0;
while ( !q.empty() ) {
int nodoAtt = q.front();
q.pop();
for( link linkVic : tmp_grafo[nodoAtt].vic ) {
int nodoVic = linkVic.nodoTo;
int costo = linkVic.costo;
if ( (distances[nodoVic] == -1 ||
costs[nodoVic] > costs[nodoAtt] + costo) &&
nodoVic != 0
) {
distances[nodoVic] = distances[nodoAtt] + 1;
costs[nodoVic] = costs[nodoAtt] + costo;
//if(nodoVic == i) break;
q.push(nodoVic) ;
}
}
}
occupiedNodes[j].costoOccupiedPath = costs[i] + grafo[i].costDistance;
occupiedNodes[j].lenghtOccupiedPath = distances[i] + grafo[i].linkDistance;
}
}
}
bool isPathOccupied( const vector<int> path ) {
for( int i : path ) {
if ( grafo[i].occupied ) {
return true;
}
}
return false;
}
//dato un vettore di parent, costruisce il path
vector<int> getPath(const vector<int> & localParent, const int start , int stop ) {
vector<int> path;
stack<int> s;
do {
s.push( stop ) ;
stop = localParent[stop] ;
} while ( stop != start ) ;
s.push(stop) ;
while ( !s.empty() ) {
path.push_back(s.top()) ;
s.pop();
}
return path;
}
void printPath( ofstream &output, const vector<int> & path ) {
for ( int i = 0; i < path.size(); i++ ) {
//cout << path[i] << " " ;
output << path[i] << " " ;
}
}
//GB stampa il nodo, se è occupato un asterisco, e tutti i vicini con il costo
void printGraph( const vector<nodo> & grafo ) {
for ( int i = 0; i < grafo.size() ; i++ ) {
cout << "i: " << i ;
if ( grafo[i].occupied ) {
cout << "*" ;
}
for ( link l : grafo[i].vic ) {
cout << " --> (" << l.nodoTo << " - " << l.costo << ")" ;
}
cout << endl;
}
}
// Casi di test:
//In 6 casi su 20, tutti gli archi hanno lo stesso tempo di percorrenza;
//In 4 casi su 20, o nessuna città è occupata, o tutte le città sono occupate (B ed A escluse)