#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 2014;
int T[N];
int B[N*N];
vector<pair<int, pair<int,int> > > V;
int main()
{
int n;
cin >> n;
int w;
for(int i=0;i<n;++i)
{
for(int j=0;j<n-i;++j)
{
cin >> w;
V.push_back(make_pair(w, make_pair(i,i+j+1)));
}
}
sort(V.begin(), V.end());
int wa=V[0].first;
int p=V[0].second.first;
int k=V[0].second.second;
T[p]=T[k]=1;
int c=1;
long long res = wa;
int i=1;
while(1)
{
wa=V[i].first;
p=V[i].second.first;
k=V[i].second.second;
if (T[p]^T[k])
{
res += wa;
++c;
T[p]=T[k]=1;
}
if (c==n)
break;
++i;
}
cout << res << 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 53 54 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const int N = 2014; int T[N]; int B[N*N]; vector<pair<int, pair<int,int> > > V; int main() { int n; cin >> n; int w; for(int i=0;i<n;++i) { for(int j=0;j<n-i;++j) { cin >> w; V.push_back(make_pair(w, make_pair(i,i+j+1))); } } sort(V.begin(), V.end()); int wa=V[0].first; int p=V[0].second.first; int k=V[0].second.second; T[p]=T[k]=1; int c=1; long long res = wa; int i=1; while(1) { wa=V[i].first; p=V[i].second.first; k=V[i].second.second; if (T[p]^T[k]) { res += wa; ++c; T[p]=T[k]=1; } if (c==n) break; ++i; } cout << res << endl; return 0; } |
English