Language: C++

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;

const int MAX_V = 5050;
const int INF = 1 << 15;

short cost[MAX_V][MAX_V];
short d[MAX_V];
bool used[MAX_V];
int V;

void dijkstra(int s)
{
  fill(d, d + V, INF);
  fill(used, used + V, false);
  d[s] = 0;
  while (true){
    int v = -1;
    for (int u = 0; u < V; u++){
      if (!used[u] && (v == -1 || d[v] < d[v])) v = u;
    }
    if (v == -1) break;
    used[v] = true;
    for (int u = 0; u < V; u++){
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}

inline int dist(const P& a, const P& b){
  return abs(a.first - b.first) + abs(a.second - b.second);
}

int main()
{
  int w, h;
  while (scanf("%d %d", &w, &h), w){
    vector<P> ps[5];
    vector<int> is[5];
    P start, goal;
    for (int y = 0; y < h; y++){
      char line[1024];
      scanf("%s", line);
      for (int x = 0; x < w; x++){
        if (line[x] == 'S') start = P(x, y);
        else if (line[x] == 'G') goal = P(x, y);
        else if (line[x] != '.') {
          int id = line[x] - '1';
          ps[id].push_back(P(x, y));
        }
      }
    }
    V = 2;
    for (int i = 0; i < 5; i++){
      for (auto& p : ps[i]){
        is[i].push_back(V++);
      }
    }
    short mini = INF;
    int mini_id = -1;
    for (int i = 0; i < MAX_V; i++){
      for (int j = 0; j < MAX_V; j++){
        cost[i][j] = INF;
      }
    }
    for (int init = 0; init < 5; init++){
      int nxt = (init + 1) % 5;
      for (int i = 0; i < ps[nxt].size(); i++){
        cost[0][is[nxt][i]] = dist(start, ps[nxt][i]);
      }
      for (int i = 0; i < 3; i++){
        int nnxt = (nxt + 1) % 5;
        for (int j = 0; j < ps[nxt].size(); j++){
          for (int k = 0; k < ps[nnxt].size(); k++){
            cost[is[nxt][j]][is[nnxt][k]] = dist(ps[nxt][j], ps[nnxt][k]);
          }
        }
        nxt = nnxt;
      }
      for (int i = 0; i < ps[nxt].size(); i++){
        cost[is[nxt][i]][1] = dist(goal, ps[nxt][i]);
      }
      dijkstra(0);
      if (mini > d[1]){
        mini = d[1];
        mini_id = init;
      }
    }
    if (mini_id == -1) puts("NA");
    else printf("%d %d\n", mini_id + 1, mini);
  }
  return 0;
}

Compiler output:
a.cc: In function 'void dijkstra(int)':
a.cc:28:17: error: no matching function for call to 'min(short int&, int)'
   28 |       d[u] = min(d[u], d[v] + cost[v][u]);
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:28:17: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   28 |       d[u] = min(d[u], d[v] + cost[v][u]);
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:28:17: note:   mismatched types 'std::initializer_list<_Tp>' and 'short int'
   28 |       d[u] = min(d[u], d[v] + cost[v][u]);
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~