Language: C++


#include<iostream>
#include<vector>
#include<queue>
using namespace std;

Graph creat_graph(int n, int m);
int search_prim(Graph prim, int n);

vector<int> ans;
vector<vector<int>> cost(100,vector<int>(100,0));

class Graph {
private:
  const static int INF = 1000000; 
  std::vector<std::vector<int> > edge_;
  unsigned int size_;
public:
  Graph(unsigned int size);
  Graph& connect(int from, int to, int weight);
  std::vector<int> neighbours(int node) const;
  unsigned int size() const;
};

const int Graph::INF;

Graph::Graph(unsigned int size) {
  size_ = size;
  edge_ = std::vector<std::vector<int> > (size_, std::vector<int>(size_, INF));
}

Graph& Graph::connect(int from, int to, int weight = 1) {
  edge_[from][to] = weight;
  return *this;
}

std::vector<int> Graph::neighbours(int node) const {
  std::vector<int> neighbours;
  for (int i = 0; i < static_cast<int>(size_); ++i)
    if (edge_[node][i] < INF) neighbours.push_back(i);

  return neighbours;
}

unsigned int Graph::size() const {
  return size_;
}

int main(){
  int n,m;
 
  
  while(cin >> n >> m, n){     
    Graph prim = creat_graph(n,m);   /*creat graph*/
    ans.push_back(search_prim(prim,n));   /*make answer*/
  }
  
  for(int i: ans){
    cout << i << endl;
  }
  return 0;
}


 Graph creat_graph(int n,int m){
    Graph prim(n);
    int a,b,cost_t;

    for(int i=0; i<m; i++){
      cin >> a >> b >> cost_t;
      prim.connect(a,b);
      cost[a][b] = cost_t;
      cost[a][b] = cost_t;
    }
    return prim;
  }
    
int search_prim(Graph prim, int n){
  int current,cost_sum;
  typedef pair<int, int> PII;
  vector<bool> frag(n,false);
  priority_queue < PII, vector < pair <int, int> >, greater < pair <int, int> > > que;
  /*cost,next_node*/
  
  cost_sum = 0;
  current = 0;
  frag[0] = true;
  for(int k=0; k<n-1; ++k){
     vector<int> near = prim.neighbours(current);

     for(int j:near){
       que.push(PII(cost[current][j],j));
     }

     while(1){
       if(frag[que.top().second] == false){
	 cost_sum += que.top().first;
	 frag[que.top().second] = true;
	 current = que.top().second;
	 break;
	 }
     }
     que.pop();
  }
  return cost_sum;
}
       

Compiler output:
a.cc:7:1: error: 'Graph' does not name a type
    7 | Graph creat_graph(int n, int m);
      | ^~~~~
a.cc:8:17: error: 'Graph' was not declared in this scope; did you mean 'isgraph'?
    8 | int search_prim(Graph prim, int n);
      |                 ^~~~~
      |                 isgraph
a.cc:8:29: error: expected primary-expression before 'int'
    8 | int search_prim(Graph prim, int n);
      |                             ^~~
a.cc:8:34: error: expression list treated as compound expression in initializer [-fpermissive]
    8 | int search_prim(Graph prim, int n);
      |                                  ^
a.cc: In function 'int main()':
a.cc:54:18: error: 'creat_graph' was not declared in this scope
   54 |     Graph prim = creat_graph(n,m);   /*creat graph*/
      |                  ^~~~~~~~~~~
a.cc:55:30: error: 'search_prim' cannot be used as a function
   55 |     ans.push_back(search_prim(prim,n));   /*make answer*/
      |                   ~~~~~~~~~~~^~~~~~~~
a.cc: At global scope:
a.cc:78:34: error: 'int search_prim(Graph, int)' redeclared as different kind of entity
   78 | int search_prim(Graph prim, int n){
      |                                  ^
a.cc:8:5: note: previous declaration 'int search_prim'
    8 | int search_prim(Graph prim, int n);
      |     ^~~~~~~~~~~