Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int MX = 1e5+69;
- typedef long long ll;
- #define INF 1e14+69
- int n,A,m;
- ll air[MX];
- struct Edge
- {
- int u,v;
- ll w;
- } ed[MX];
- bool cmp(const Edge a,const Edge b)
- {
- if(a.w != b.w)return a.w<b.w;
- if(a.u!=b.u)return a.u<b.u;
- if(a.v!=b.v)return a.v<b.v;
- }
- int pa[MX],active[MX];
- int parent(int x)
- {
- if(pa[x]==x)return x;
- int v=pa[x];
- air[v]=min(air[v],air[x]);
- active[v]|=active[x];
- return pa[x]=parent(pa[x]);
- }
- void join(int x,int y,const Edge& E,ll &edges,ll &airports,ll& cost)
- {
- int xx=parent(x);
- int yy=parent(y);
- if(xx==yy)return;
- if(active[xx]&&active[yy])return;
- ll W1 = 1ll*(active[xx]^1)*air[xx]+1ll*(active[yy]^1)*air[yy];
- ll W2 = E.w;
- // cout<<xx<<' '<<yy<<endl;
- // cout<<W1<<' '<<W2<<endl;
- if(W2>W1)
- {
- airports+=1ll*(active[xx]^1)+1ll*(active[yy]^1);
- cost += W1;
- active[yy]=active[xx]=1;
- air[xx]=air[yy]=min(air[xx],air[yy]);
- pa[xx]=yy;
- }
- else
- {
- cost+=W2;
- edges++;
- air[xx]=air[yy]=min(air[xx],air[yy]);
- active[xx]=active[yy]=(active[xx]|active[yy]);
- pa[xx]=yy;
- }
- }
- int main()
- {
- scanf("%d%d",&n,&A);
- for(int i=1; i<=n; i++)
- pa[i]=i,air[i]=INF;
- for(int i=1; i<=A; i++)
- {
- int u;
- scanf("%d",&u);
- scanf("%lld",&air[u]);
- }
- scanf("%d",&m);
- for(int i=1; i<=m; i++)
- {
- int u,v,w;
- scanf("%d%d%d",&u,&v,&w);
- ed[i].u=u;
- ed[i].v=v;
- ed[i].w=w;
- }
- ll cost=0,airp=0,edges=0;
- sort(ed+1,ed+1+n,cmp);
- for(int i=1; i<=m; i++)
- {
- join(ed[i].u,ed[i].v,ed[i],edges,airp,cost);
- }
- ed[0].w=INF;
- for(int i=1; i<=n; i++)
- {
- int u = i, v = i+1;
- if(v==n+1)v=1;
- join(u,v,ed[0],edges,airp,cost);
- }
- if(cost>=INF)return puts("Insufficient");
- else cout<<cost<<'\n'<<edges<<' '<<airp<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement