Make It Connected
生活随笔
收集整理的這篇文章主要介紹了
Make It Connected
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://codeforces.com/contest/1095/problem/F
題解:并查集+最小生成樹(shù)
TLE,WA,RE搞死中,就因?yàn)閿?shù)組開(kāi)小了,23333
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=410000; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k,q,v,u; ll a[N],w; int pre[N]; struct node{int u,v;ll w;node(){};node(int _u,int _v,ll _w){u=_u;v=_v;w=_w;}bool operator <(const node&S)const{return w<S.w;} }e[N]; int find(int x){int rt=x;while(pre[rt]!=rt){rt=pre[rt];}pre[x]=rt;//并查集路徑壓縮return rt; } int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifscanf("%d%d",&n,&m);int id=1;for(int i=1;i<=n;i++){pre[i]=i;scanf("%I64d",&a[i]);if(a[i]<=a[id])id=i;}int tot=0;for(int i=1;i<=n;i++){//if(i!=id)e[++tot]=node(i,id,a[i]+a[id]);}for(int i=1;i<=m;i++){scanf("%d%d%I64d",&u,&v,&w);e[++tot]=node(u,v,w);}sort(e+1,e+tot+1);ll ans=0;for(int i=1;i<=tot;i++){int x=find(e[i].u);int y=find(e[i].v);if(x!=y){pre[x]=pre[y];ans+=e[i].w;}}cout << ans << endl;//cout << "Hello world!" << endl;return 0; }?
總結(jié)
以上是生活随笔為你收集整理的Make It Connected的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Almost Regular Brack
- 下一篇: 组合数(Combinatorial_Nu