#include <iostream>
#include <algorithm>
using namespace std;
struct K
{
int pocz, kon, cena;
bool operator<(const K &k) const
{
return cena<k.cena;
}
} ky[4000000];
int re[2002];
int rep(int x)
{
if (re[x]==0)
return x;
return re[x]=rep(re[x]);
}
int main()
{
ios_base::sync_with_stdio(false);
int n, lk=0, bylo=0;
cin>>n;
for (int i=1; i<=n; ++i)
for (int j=i; j<=n; ++j)
{
cin>>ky[lk].cena;
ky[lk].pocz=i;
ky[lk].kon=j;
++lk;
}
sort(ky, ky+lk);
long long wyn=0;
for (int i=0; bylo<n; ++i)
{
int re1=rep(ky[i].pocz), re2=rep(ky[i].kon+1);
if (re1==re2)
continue;
wyn+=ky[i].cena;
re[re1]=re2;
++bylo;
}
cout<<wyn<<endl;
return 0;
}
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 | #include <iostream> #include <algorithm> using namespace std; struct K { int pocz, kon, cena; bool operator<(const K &k) const { return cena<k.cena; } } ky[4000000]; int re[2002]; int rep(int x) { if (re[x]==0) return x; return re[x]=rep(re[x]); } int main() { ios_base::sync_with_stdio(false); int n, lk=0, bylo=0; cin>>n; for (int i=1; i<=n; ++i) for (int j=i; j<=n; ++j) { cin>>ky[lk].cena; ky[lk].pocz=i; ky[lk].kon=j; ++lk; } sort(ky, ky+lk); long long wyn=0; for (int i=0; bylo<n; ++i) { int re1=rep(ky[i].pocz), re2=rep(ky[i].kon+1); if (re1==re2) continue; wyn+=ky[i].cena; re[re1]=re2; ++bylo; } cout<<wyn<<endl; return 0; } |
English