Advertisement
jeff69

Goat rape

Apr 26th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MX = 1e5+69;
  4. typedef long long ll;
  5. #define INF 1e14+69
  6. int n,A,m;
  7. ll air[MX];
  8. struct Edge
  9. {
  10.     int u,v;
  11.     ll w;
  12. } ed[MX];
  13. bool cmp(const Edge a,const Edge b)
  14. {
  15.     if(a.w != b.w)return a.w<b.w;
  16.     if(a.u!=b.u)return a.u<b.u;
  17.     if(a.v!=b.v)return a.v<b.v;
  18. }
  19. int pa[MX],active[MX];
  20. int parent(int x)
  21. {
  22.     if(pa[x]==x)return x;
  23.     int v=pa[x];
  24.     air[v]=min(air[v],air[x]);
  25.     active[v]|=active[x];
  26.     return pa[x]=parent(pa[x]);
  27. }
  28. void join(int x,int y,const Edge& E,ll &edges,ll &airports,ll& cost)
  29. {
  30.  
  31.     int xx=parent(x);
  32.     int yy=parent(y);
  33.  
  34.     if(xx==yy)return;
  35.     if(active[xx]&&active[yy])return;
  36.     ll W1 = 1ll*(active[xx]^1)*air[xx]+1ll*(active[yy]^1)*air[yy];
  37.     ll W2 = E.w;
  38. //    cout<<xx<<' '<<yy<<endl;
  39. //    cout<<W1<<' '<<W2<<endl;
  40.  
  41.     if(W2>W1)
  42.     {
  43.         airports+=1ll*(active[xx]^1)+1ll*(active[yy]^1);
  44.         cost += W1;
  45.         active[yy]=active[xx]=1;
  46.         air[xx]=air[yy]=min(air[xx],air[yy]);
  47.         pa[xx]=yy;
  48.     }
  49.     else
  50.     {
  51.         cost+=W2;
  52.         edges++;
  53.         air[xx]=air[yy]=min(air[xx],air[yy]);
  54.         active[xx]=active[yy]=(active[xx]|active[yy]);
  55.         pa[xx]=yy;
  56.     }
  57. }
  58. int main()
  59. {
  60.     scanf("%d%d",&n,&A);
  61.     for(int i=1; i<=n; i++)
  62.         pa[i]=i,air[i]=INF;
  63.     for(int i=1; i<=A; i++)
  64.     {
  65.         int u;
  66.         scanf("%d",&u);
  67.         scanf("%lld",&air[u]);
  68.     }
  69.     scanf("%d",&m);
  70.     for(int i=1; i<=m; i++)
  71.     {
  72.         int u,v,w;
  73.         scanf("%d%d%d",&u,&v,&w);
  74.         ed[i].u=u;
  75.         ed[i].v=v;
  76.         ed[i].w=w;
  77.     }
  78.     ll cost=0,airp=0,edges=0;
  79.     sort(ed+1,ed+1+n,cmp);
  80.     for(int i=1; i<=m; i++)
  81.     {
  82.         join(ed[i].u,ed[i].v,ed[i],edges,airp,cost);
  83.     }
  84.     ed[0].w=INF;
  85.     for(int i=1; i<=n; i++)
  86.     {
  87.         int u = i, v = i+1;
  88.         if(v==n+1)v=1;
  89.         join(u,v,ed[0],edges,airp,cost);
  90.     }
  91.     if(cost>=INF)return puts("Insufficient");
  92.     else cout<<cost<<'\n'<<edges<<' '<<airp<<endl;
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement