forked from Autoratch/Thailand-Olympiad-in-Informatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toi15_budgets_kruskal_alter_sol.cpp
60 lines (57 loc) · 1.33 KB
/
toi15_budgets_kruskal_alter_sol.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
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
int B,E,S,T,R,P;
long long int C,D,L,ans=0;
priority_queue<pair<int,pii>, vector<pair<int,pii>>, greater<pair<int,pii>> > q;
vector<pii> cost;
int parent[3010];
int root(int n)
{
if(parent[n]==n) return n;
return parent[n] = root(parent[n]);
}
int main()
{
for(int a=0;a<3010;a++){
parent[a]=a;
}
scanf(" %d %d",&B,&E);
for(int a=0;a<E;a++)
{
scanf(" %d %d %lld %d",&S,&T,&L,&R);
if(R==1) L=0;
q.push({L,{S,T}});
}
scanf(" %d",&P);
cost.push_back({0,0});
for(int a=0;a<P;a++)
{
scanf(" %lld %lld",&C,&D);
cost.push_back({C,D});
}
sort(cost.begin(),cost.end());
for(int a=P-1;a>=0;a--)
{
cost[a].second=min(cost[a].second,cost[a+1].second);
}
while(!q.empty())
{
int lt=q.top().first;
int par_a=root(q.top().second.first);
int par_b=root(q.top().second.second);
q.pop();
if(par_a!=par_b)
{
parent[par_a]=par_b;
for(int a=P;a>=0;a--)
{
if(cost[a].first<lt){
ans+=cost[a+1].second;
break;
}
}
}
}
printf("%lld",ans);
}