-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hermits (Nested Vectors).cpp
44 lines (38 loc) · 1.21 KB
/
Hermits (Nested Vectors).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
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <vector>
using namespace std;
bool sortcolumn(const vector<int>& v1, const vector<int>& v2) //Compare function
{
return v1[1] < v2[1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int position = 0;
int N; cin >> N;
vector<vector<int>>weights;
vector<vector<pair<int,int>>>AL(N);
for (int i = 0 ; i < N ; i++){ //Adding 2d vector of [Street Number, Weights]
position ++ ;
int K; cin >> K;
vector<int>urmum;
urmum.push_back(position);
urmum.push_back(K);
weights.push_back(urmum);
}
int J; cin>> J; //Creating AL
for (int i = 0; i < J; i++){
int K; int L; cin>> K >> L;
AL[K-1].push_back({L,weights[L-1][1]});
AL[L-1].push_back({K,weights[K-1][1]});
}
for (int i = 0 ; i < N ; i++){ //Adding the weights of the crossed st. to the original st.
for (int j = 0; j < AL[i].size(); j++){
weights[i][1] += AL[i][j].second;
}
}
stable_sort(weights.begin(), weights.end(), sortcolumn); //Sorting to find smallest
cout << weights[0][0];
}