#include <cstdio>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX_N = 2000 + 44;
int C[MAX_N][MAX_N];
int N;
int main()
{
scanf("%d", &N);
for(int i=0;i<N;i++)
for(int j=i+1;j<=N;j++)
{
scanf("%d", &C[i][j]);
C[j][i] = C[i][j];
}
long long result = 0;
int join_cost[N];
for(int i=0;i<=N;i++)
join_cost[i] = 1999999999;
join_cost[0] = 0;
for(int z=0;z<=N;z++)
{
int bestj=-1;
for(int j=0;j<=N;j++)
{
if(join_cost[j]!=-1 && (bestj==-1 || join_cost[j]<join_cost[bestj]))
{
bestj = j;
}
}
result += join_cost[bestj];
join_cost[bestj] = -1;
for(int k=0;k<=N;k++)
if(k!=bestj)
{
join_cost[k] = min(join_cost[k], C[bestj][k]);
}
}
printf("%lld\n", result);
}
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 | #include <cstdio> #include <queue> #include <set> #include <vector> #include <algorithm> using namespace std; const int MAX_N = 2000 + 44; int C[MAX_N][MAX_N]; int N; int main() { scanf("%d", &N); for(int i=0;i<N;i++) for(int j=i+1;j<=N;j++) { scanf("%d", &C[i][j]); C[j][i] = C[i][j]; } long long result = 0; int join_cost[N]; for(int i=0;i<=N;i++) join_cost[i] = 1999999999; join_cost[0] = 0; for(int z=0;z<=N;z++) { int bestj=-1; for(int j=0;j<=N;j++) { if(join_cost[j]!=-1 && (bestj==-1 || join_cost[j]<join_cost[bestj])) { bestj = j; } } result += join_cost[bestj]; join_cost[bestj] = -1; for(int k=0;k<=N;k++) if(k!=bestj) { join_cost[k] = min(join_cost[k], C[bestj][k]); } } printf("%lld\n", result); } |
English