#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #define N 1005 #define MAX 100000 int n, ans, A[N][N], dis[N], vis[N]; void Prim() { memset(vis, 0, sizeof(vis)); int i, j; for (i = 1; i <= n; i++) dis[i] = A[1][i]; vis[1] = 1; ans = 0; for (i = 2; i <= n; i++) { int t = MAX, k = 0; for (j = 2; j <= n; j++) { if (!vis[j] && dis[j] < t) { t = dis[j]; k = j; } } vis[k] = 1; ans += t; for (j = 2; j <= n; j++) { if (A[k][j] < dis[j]) dis[j] = A[k][j]; } } } int main() { int i, j; scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) scanf("%d", &A[i][j]); } Prim(); printf("%d\n", ans); return 0; }