Language: C++

#include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <vector>
#include <string>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG


const int kMaxN = 100;
const int kInvalid = INT_MAX;
int g_weightOfVij[kMaxN][kMaxN];
int g_numOfVertices;


struct Edge {
  Edge (int v0, int v1)
    : v0_(v0)
    , v1_(v1)
  { }

  int v0_;
  int v1_;
  int getWeight() { return g_weightOfVij[v0_][v1_]; }
};


struct VisitData {
  VisitData(int num)
    : numOfData_(num)
    , totalWeight_(0)
    {
      memset(visited_, 0, kMaxN);
    }

  bool visited_[kMaxN];
  int numOfData_;
  int totalWeight_;

  bool isAllVisited();

#if DEBUG
  vector<int> path_; // for history of pathes
#endif // DEBUG
};


bool VisitData::isAllVisited()
{
  for (int i = 0; i < numOfData_; i++)
    if (visited_[i] == false)
      return false;

  return true;
}


void printAllWeights()
{
  DPRINTF("Number of vertices %d\n", g_numOfVertices);

  for (int i = 0; i < g_numOfVertices; i++)
  {
    for (int j = 0; j < g_numOfVertices; j++)
    {
      if (j == 0)  DPRINTF("");
      printf(" %d", g_weightOfVij[i][j]);
    }
    cout << endl;
  }
}


void printAllPath()
{
  DPRINTF("Number of vertices %d\n", g_numOfVertices);
  DPRINTF("$src -> $dst ($weight)\n");

  for (int i = 0; i < g_numOfVertices; i++)
  {
    for (int j = 0; j < g_numOfVertices; j++)
    {
      if (g_weightOfVij[i][j] > 0)
        DPRINTF("%d -> %d (%d)\n", i, j, g_weightOfVij[i][j]);
    }
  }
}


void getConnectedVertices(int pos, vector<int> &vertices)
{
  for (int i = 0; i < g_numOfVertices; i++)
    if (g_weightOfVij[pos][i] > 0)
      vertices.push_back(i);

#if 0
  DPRINTF("connected points from %d :", pos);
  for (int i = 0; i < vertices.size(); i++)
    printf(" %d", vertices[i]);

  cout << endl;
#endif
}


/* find unvisited vertices that are connected from the vertices already visited */
void getNextCandidates(const bool visited[kMaxN], vector<Edge> &candidates)
{
#if 0
  DPRINTF("visited:\n");
  for (int i = 0; i < g_numOfVertices; i++)
    DPRINTF("  [%d] = %d\n", i, visited[i]);
#endif

  for (int startV = 0; startV < g_numOfVertices; startV++)
  {
    if (visited[startV] == true)
    {
      // Get all connected vertices
      vector<int> vertices;
      getConnectedVertices(startV, vertices);

      // Remove vertices that are already visited
      for (int nextV : vertices)
      {
        if (visited[nextV] == false)
          candidates.push_back(Edge(startV, nextV));
      }
    }
  }

#if 0
  DPRINTF("Next candidates:\n");
  for (int i = 0; i < candidates.size(); i++)
    printf("  %d -> %d\n", candidates[i].v0_, candidates[i].v1_);
#endif
}


//int findSpanningTree(int vertex, VisitData info /* Not pointer nor reference */)
int findSpanningTree(int vertex, VisitData &info)
{

  assert(info.visited_[vertex] == false);
  info.visited_[vertex] = true;

#if DEBUG
  info.path_.push_back(vertex);
#endif

#if 1 & DEBUG
  DPRINTF("Path:");
  for (int i = 0; i < info.path_.size(); i++)
    printf(" %d", info.path_[i]);
  cout << endl;
#endif

  if (info.isAllVisited())
  {
    DPRINTF("all vertices visited. total weight %d\n", info.totalWeight_);
    return info.totalWeight_;
  }

  vector<Edge> nextPosCandidates;
  getNextCandidates(info.visited_, nextPosCandidates);

  int minWeight = kInvalid;

  for (int i = 0; i < nextPosCandidates.size(); i++)
  {
    const int startV = nextPosCandidates[i].v0_;
    const int nextV = nextPosCandidates[i].v1_;

    VisitData nextInfo = info;
    nextInfo.totalWeight_ += g_weightOfVij[startV][nextV];

    const int weight = findSpanningTree(nextV, nextInfo);

    if (weight != kInvalid && weight < minWeight)
      minWeight = weight;
  }

  DPRINTF("minWeight %d\n", minWeight);
#if 1 & DEBUG
  DPRINTF("  Path:");
  for (int i = 0; i < info.path_.size(); i++)
    printf(" %d", info.path_[i]);
  cout << endl;
#endif

  return minWeight;
}


int main()
{
  cin >> g_numOfVertices; cin.ignore();

  DPRINTF("Number of vertices %d\n", g_numOfVertices);

  for (int i = 0; i < g_numOfVertices; i++)
  {
    for (int j = 0; j < g_numOfVertices; j++)
      cin >> g_weightOfVij[i][j];

    cin.ignore();
  }

#if DEBUG
  DPRINTF("Number of vertices %d\n", g_numOfVertices);
  printAllWeights();
  //printAllPath();
#endif // DEBUG

  const int startPos = 0;
  VisitData info(g_numOfVertices);

  cout << findSpanningTree(startPos, info) << endl;

  return 0;
}



Compiler output:
a.cc: In constructor 'VisitData::VisitData(int)':
a.cc:47:7: error: 'memset' was not declared in this scope
   47 |       memset(visited_, 0, kMaxN);
      |       ^~~~~~
a.cc:9:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    8 | #include <vector>
  +++ |+#include <cstring>
    9 | #include <string>