-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBLINNET.cpp
More file actions
93 lines (81 loc) · 1.48 KB
/
Copy pathBLINNET.cpp
File metadata and controls
93 lines (81 loc) · 1.48 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF (ll)(pow(2, 32))
vector <ll> adj[10001];
map <ll, map <ll, ll> > edgew;
ll n, t, key[10001];
bool visited[10001];
struct compare_keys
{
bool operator()(const pair <ll, ll> &left, const pair <ll, ll> &right)
{
return left.second > right.second;
}
};
void reset()
{
for (ll i = 0; i < 10001; i++)
{
adj[i].clear();
key[i] = INF;
visited[i] = 0;
}
edgew.clear();
}
ll find_min_mst_wgt()
{
ll ans = 0;
vector <pair <ll, ll> > Q;
key[1] = 0;
for (ll i = 1; i <= n; i++)
Q.push_back(make_pair(i, key[i]));
make_heap(Q.begin(), Q.end(), compare_keys());
while (!Q.empty())
{
ll u = Q.front().first;
pop_heap(Q.begin(), Q.end(), compare_keys());
Q.pop_back();
if (!visited[u])
{
visited[u] = 1;
// cout<<"u = "<<u<<", key[u] = "<<key[u]<<endl;
ans += key[u];
}
for (auto v: adj[u])
{
if (edgew[u][v] < key[v])
{
key[v] = edgew[u][v];
Q.push_back(make_pair(v, key[v]));
push_heap(Q.begin(), Q.end(), compare_keys());
}
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin>>t;
while (t--)
{
reset();
cin>>n;
for (ll u = 1; u <= n; u++)
{
string temp;
ll v, c, m;
cin>>temp>>m;
while (m--)
{
cin>>v>>c;
adj[u].push_back(v), adj[v].push_back(u);
edgew[u][v] = (edgew[u][v])?min(c, edgew[u][v]):c;
edgew[v][u] = (edgew[v][u])?min(c, edgew[v][u]):c;
}
}
cout<<find_min_mst_wgt()<<endl;
}
return 0;
}