#include <stdio.h>
#include <stdlib.h>

#define NIL_VERTEX -1
#define DENOTE_INF_COST -1
#define COMP_INF_COST 2001

// translate 1-origin into 0-origin
int i0O(int index1Origin) {
    return index1Origin-1;
}


#include <stdio.h>
#include <stdlib.h>

#define MAX_NUM_STACK_VALUES 100
#define STR_MAX_LENGTH 100

typedef enum {
    WHITE,
    GRAY,
    BLACK
} Color;

typedef struct Vtx {
//    int * adjacencyList;
    Color color;
    int discoveryCost;
    struct Vtx * discoverFrom;
    int key;
} Vertex;

//void copyVertex(Vertex * p_out, Vertex in) {
////    p_out->adjacencyList = in.adjacencyList;
//    // dangourous!! what if the malloc is free-ed?!!
//    p_out->color = in.color;
//    p_out->discoveryCost = in.discoveryCost;
//    p_out->discoverFrom = in.discoverFrom;
//    p_out->key = in.key;
//};

void prim(
        Vertex vertices[], 
        int nVertices,
        int ** iawMatrix, // indicativeAdjWeightMatrix
        int startingVertexIndex
) {
    int mincost, u, v, i;
    
    for (i = 0; i < nVertices; i++) {
        vertices[i].discoveryCost = COMP_INF_COST;
        vertices[i].discoverFrom = NULL;
        vertices[i].color = WHITE;
    }
    
    vertices[startingVertexIndex].discoveryCost = 0;
    
    while (1) {
        mincost = COMP_INF_COST;
        
        for (i = 0; i < nVertices; i++) 
            if (vertices[i].color != BLACK && vertices[i].discoveryCost < mincost) {
                mincost = vertices[i].discoveryCost;
                u = i;
            }
        
        if (mincost == COMP_INF_COST) break;
        
        vertices[u].color = BLACK;
        
        for (v = 0; v < nVertices; v++) 
            if ((iawMatrix)[u][v] < COMP_INF_COST)
                if (vertices[v].color != BLACK && (iawMatrix)[u][v] < vertices[v].discoveryCost) {
                    vertices[v].discoverFrom = &vertices[u];
                    vertices[v].discoveryCost = (iawMatrix)[u][v];
                }; 
    }
}

int main(int argc, char** argv) {
    int nVertices;
    
    // scan number of already existed events
    scanf("%d", &nVertices); 
    
    
    Vertex vertices[nVertices];
    int weight;
    int i,j;
    
    int ** adjMatrix;
    
    adjMatrix = (int **)malloc(nVertices * sizeof(int *));
    
    for (i = 0; i < nVertices; i++) {
        adjMatrix[i] = (int *)malloc(nVertices * sizeof(int));
    }
    // -------------------------------------------
    
    for (i = 0; i < nVertices; i++) {
        vertices[i].key = i;
        for (j = 0; j < nVertices; j++) {
            scanf("%d", &weight); 
            adjMatrix[i][j] = (weight == DENOTE_INF_COST ? COMP_INF_COST : weight);
        }
    }
    
//    // print result
//    printf("Scan result: \n");
//    for (i = 0; i < nVertices; i++) {
//        for (j = 0; j < nVertices; j++) {
//            if (j > 0) printf(" ");
//            printf("%d", adjMatrix[i][j]); 
//        }
//        printf("\n");
//    }
    
    prim(vertices, nVertices, adjMatrix, 0);
    
    int totalCost = 0;
    
    for (i = 0; i < nVertices; i++) {
        totalCost += vertices[i].discoveryCost;
    }
    
    printf("%d\n", totalCost);

    return (EXIT_SUCCESS);
}




Measured memory_bytes: 2112