-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoi15_budget.cpp
58 lines (46 loc) · 1.11 KB
/
toi15_budget.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
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
const int N = 3e3;
int n,m,t,ans;
vector<pair<int,int> > adj[N];
vector<pair<int,int> > cost;
priority_queue<pii,vector<pii>,greater<pii> > q;
bool visited[N];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i = 0;i < m;i++)
{
int a,b,d,e;
cin >> a >> b >> d >> e;
if(e) d = 0;
adj[a].push_back({d,b});
adj[b].push_back({d,a});
}
cin >> t;
while(t--)
{
int a,b;
cin >> a >> b;
cost.push_back({a,b});
}
sort(cost.begin(),cost.end());
for(int i = cost.size()-2;i >= 0;i--) cost[i].second = min(cost[i].second,cost[i+1].second);
q.push({0,0});
while(!q.empty())
{
int w = q.top().first,p = q.top().second;
q.pop();
if(visited[p]) continue;
visited[p] = true;
if(w)
{
auto it = lower_bound(cost.begin(),cost.end(),make_pair(w,INT_MIN));
ans+=it->second;
}
for(pair<int,int> i : adj[p]) q.push(i);
}
cout << ans;
}