-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtp2.cpp
162 lines (115 loc) · 4.25 KB
/
tp2.cpp
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
#include <iostream>
#include <vector>
#include <limits.h>
#include <queue>
using namespace std;
struct Connection {
int destVillage, year, time, cost;
};
struct Village {
long int dFromOrigin;
int year, villageIndex;
bool primYearVisited, primCostVisited;
};
int main () {
int villagesNum, connectionsNum;
int originVillage, destVillage, year, cost, time;
vector<vector<Connection>> graph;
Connection newConnection;
vector<Village> villages;
scanf("%d %d", &villagesNum, &connectionsNum);
villages.resize(villagesNum);
graph.resize(villagesNum);
for (int i = 0; i < connectionsNum; i++) {
scanf("%d %d %d %d %d", &originVillage, &destVillage, &year, &time, &cost);
newConnection.destVillage = destVillage - 1;
newConnection.year = year;
newConnection.time = time;
newConnection.cost = cost;
graph[originVillage - 1].push_back(newConnection);
newConnection.destVillage = originVillage - 1;
graph[destVillage - 1].push_back(newConnection);
}
// Dijkstra
auto compareDistance = [](Village a, Village b) { return a.dFromOrigin > b.dFromOrigin; };
priority_queue<int, vector<Village>, decltype(compareDistance)> djQueue(compareDistance);
for (int i = 0; i < villages.size(); i++) {
villages[i].dFromOrigin = LONG_MAX;
villages[i].villageIndex = i;
villages[i].primYearVisited = false;
villages[i].primCostVisited = false;
}
villages[0].dFromOrigin = 0;
djQueue.push(villages[0]);
while (!djQueue.empty()) {
Village currentVillage = djQueue.top();
int v = currentVillage.villageIndex;
long int w = currentVillage.dFromOrigin;
djQueue.pop();
if (w != villages[v].dFromOrigin) continue;
for (int i = 0; i < graph[v].size(); i++) {
Connection currentConnection = graph[v][i];
int u = currentConnection.destVillage;
int cost = currentConnection.time;
int year = currentConnection.year;
if (villages[u].dFromOrigin > villages[v].dFromOrigin + cost) {
villages[u].dFromOrigin = villages[v].dFromOrigin + cost;
villages[u].year = year;
djQueue.push(villages[u]);
}
}
}
// Prim Year
int lowestYear = 0;
auto compareYear = [](Connection a, Connection b) { return a.year > b.year; };
priority_queue<int, vector<Connection>, decltype(compareYear)> primYearQueue(compareYear);
villages[0].primYearVisited = true;
for (Connection connection: graph[0]) {
primYearQueue.push(connection);
}
while (!primYearQueue.empty()) {
Connection connectionLowestYear = primYearQueue.top();
primYearQueue.pop();
if (villages[connectionLowestYear.destVillage].primYearVisited == true) continue;
villages[connectionLowestYear.destVillage].primYearVisited = true;
if (connectionLowestYear.year > lowestYear) {
lowestYear = connectionLowestYear.year;
}
for (Connection connection: graph[connectionLowestYear.destVillage]) {
if (villages[connection.destVillage].primYearVisited == false) {
primYearQueue.push(connection);
}
}
}
// Prim Cost
long int sumCost = 0;
auto compareCost = [](Connection a, Connection b) { return a.cost > b.cost; };
priority_queue<int, vector<Connection>, decltype(compareCost)> primCostQueue(compareCost);
villages[0].primCostVisited = true;
for (Connection connection: graph[0]) {
primCostQueue.push(connection);
}
while (!primCostQueue.empty()) {
Connection connectionLowestCost = primCostQueue.top();
primCostQueue.pop();
if (villages[connectionLowestCost.destVillage].primCostVisited == true) continue;
sumCost += connectionLowestCost.cost;
villages[connectionLowestCost.destVillage].primCostVisited = true;
for (Connection connection: graph[connectionLowestCost.destVillage]) {
if (villages[connection.destVillage].primCostVisited == false) {
primCostQueue.push(connection);
}
}
}
int firstYearOfCompleteConnections = 0;
for (int i = 0; i < villages.size(); i++) {
if (villages[i].year > firstYearOfCompleteConnections) {
firstYearOfCompleteConnections = villages[i].year;
}
cout << villages[i].dFromOrigin << "\n";
}
cout << firstYearOfCompleteConnections << "\n";
cout << lowestYear << "\n";
cout << sumCost << "\n";
return 0;
}