-
Notifications
You must be signed in to change notification settings - Fork 4
/
ant-challenge.cpp
122 lines (110 loc) · 2.59 KB
/
ant-challenge.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
#include <iostream>
#include <vector>
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/prim_minimum_spanning_tree.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
using namespace std;
using namespace boost;
typedef adjacency_list<
vecS,
vecS,
undirectedS,
no_property,
property<edge_weight_t, int>
> Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::edge_descriptor Edge;
typedef property_map<Graph, edge_weight_t>::type WeightMap;
void ant_challenges() {
// Initialization
int nV, nE, nS, startV, endV;
cin >> nV >> nE >> nS >> startV >> endV;
vector<Graph> G(nS, Graph(nV));
vector<WeightMap> lenEdge(nS);
vector<int> hive(nS);
vector<int> v1(nE);
vector<int> v2(nE);
for (int i = 0; i < nS; i++) {
lenEdge[i] = get(edge_weight, G[i]);
}
for (int i = 0; i < nE; i++) {
cin >> v1[i] >> v2[i];
for (int j = 0; j < nS; j++) {
int len;
cin >> len;
Edge e;
bool success;
tie(e, success) = add_edge(v1[i], v2[i], G[j]);
lenEdge[j][e] = len;
}
}
for (int i = 0; i < nS; i++) {
cin >> hive[i];
}
// Prim Minimum Spanning Tree
vector< vector<Vertex>> primPreMap(nS, vector<Vertex>(nV));
for (int i = 0; i < nS; i++) {
prim_minimum_spanning_tree(
G[i],
make_iterator_property_map(
primPreMap[i].begin(),
get(vertex_index, G[i])
),
root_vertex(hive[i])
);
}
// Combined Graph
Graph combinedG(nV);
WeightMap combinedLenEdge = get(edge_weight, combinedG);
for (int i = 0; i < nE; i++) {
Edge e;
bool success;
tie(e, success) = add_edge(v1[i], v2[i], combinedG);
int minLen = INT_MAX;
for (int j = 0; j < nS; j++) {
Edge tempE;
bool tempSuccess;
if (primPreMap[j][v1[i]] == v2[i]) {
tie(tempE, tempSuccess) = edge(v1[i], v2[i], G[j]);
} else if (primPreMap[j][v2[i]] == v1[i]) {
tie(tempE, tempSuccess) = edge(v2[i], v1[i], G[j]);
} else {
continue;
}
int tempLen = lenEdge[j][tempE];
if (tempLen < minLen) {
minLen = tempLen;
}
}
combinedLenEdge[e] = minLen;
}
// Dijkstra Shortest Paths
vector<Vertex> dijkPreMap(nV);
vector<int> distMap(nV);
dijkstra_shortest_paths(
combinedG,
startV,
predecessor_map(
make_iterator_property_map(
dijkPreMap.begin(),
get(vertex_index, combinedG)
)
).distance_map(
make_iterator_property_map(
distMap.begin(),
get(vertex_index, combinedG)
)
)
);
cout << distMap[endV] << endl;
}
int main(void) {
int T;
cin >> T;
for (int t = 0; t < T; t++) {
ant_challenges();
}
return 0;
}