Advertisement
MAGCARI

Budget

Dec 10th, 2022
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct A{
  4.     int s,t,r,l;
  5.     bool operator < (const A&o) const{
  6.         return l<o.l;
  7.     }
  8. }st[500010];
  9. pair<int ,int > rf[300010];
  10. int f[3010];
  11. int fr(int now){
  12.     if(f[now] == now)   return now;
  13.     else                return f[now] = fr(f[now]);
  14. }
  15. int main(){
  16.     int i,p,b,e;
  17.     scanf("%d %d",&b,&e);
  18.     for(i=0;i<b;i++)
  19.         f[i] = i;
  20.     for(i=0;i<e;i++){
  21.         scanf("%d %d %d %d",&st[i].s,&st[i].t,&st[i].l,&st[i].r);
  22.         if(st[i].r == 1){
  23.             int rs = fr(st[i].s);
  24.             int rt = fr(st[i].t);
  25.             f[rs] = rt;
  26.         }
  27.     }
  28.     scanf("%d",&p);
  29.     for(i=0;i<p;i++){
  30.         scanf("%d %d",&rf[i].first,&rf[i].second);
  31.     }
  32.     sort(rf,rf+p);
  33.     for(i=p-2;i>=0;i--){
  34.         rf[i].second = min(rf[i].second,rf[i+1].second);
  35.     }
  36.     for(i=0;i<e;i++){
  37.         st[i].l=rf[lower_bound(rf,rf+p,make_pair(st[i].l,0))-rf].second;
  38.     }
  39.     sort(st,st+e);
  40.  
  41.     long long ans = 0;
  42.     for(i=0;i<e;i++){
  43.         int rs = fr(st[i].s);
  44.         int rt = fr(st[i].t);
  45.         if(st[i].r == 1){
  46.             f[rs] = rt;
  47.             continue;
  48.         }
  49.         if(rs == rt)    continue;
  50.         // printf("%d %d %d %d\n",st[i].s,st[i].t,st[i].l,st[i].r);
  51.         ans+=st[i].l;
  52.         f[rs] = rt;
  53.     }
  54.     printf("%lld\n",ans);
  55. }
  56. /*
  57. 6 8
  58. 0 1 19 0
  59. 1 2 50 1
  60. 1 3 5 0
  61. 2 3 18 0
  62. 0 4 32 0
  63. 3 4 22 0
  64. 2 5 70 0
  65. 4 5 20 1
  66. 8
  67. 5 60
  68. 50 200
  69. 75 350
  70. 20 100
  71. 40 145
  72. 15 50
  73. 35 150
  74. 8 60
  75. */
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement