Language: C++

#include <bits/stdc++.h>

using namespace std;

template< typename T >
struct WeightedGraph : vector< pair< int, T > >
{
  using T
  struct edge
  {
    int to;
    T cost;
  };

  vector< vector< edge > > g;

  WeightedGraph() {}

  WeightedGraph(int n) : g(n) {}

  void add_edge(int u, int v, T cost)
  {
    g[u].emplace_back((edge) {v, cost});
  }

  size_t size() const
  {
    return g.size();
  }
};

template< typename T >
vector< T > dijkstra(WeightedGraph< T > &g, int s)
{
  const auto INF = numeric_limits< T >::max();
  vector< T > dist;
  dist.assign(g.size(), INF);

  using Pi = pair< T, int >;
  priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  dist[s] = 0;
  que.emplace(dist[s], s);
  while(!que.empty()) {
    T cost;
    int idx;
    tie(cost, idx) = que.top();
    que.pop();
    if(dist[idx] < cost) continue;
    for(auto &e : g[idx]) {
      auto next_cost = cost + e.cost;
      if(dist[e.to] <= next_cost) continue;
      dist[e.to] = next_cost;
      que.emplace(dist[e.to], e.to);
    }
  }
  return dist;
}

int main()
{
  int V, E, S;
  scanf("%d %d %d", &V, &E, &S);
  WeightedGraph< int > g(V);
  for(int i = 0; i < E; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    g.add_edge(x, y, z);
  }
  auto cost = dijkstra(g, S);
  for(int i = 0; i < V; i++) {
    if(cost[i] == numeric_limits< int >::max()) puts("INF");
    else printf("%d\n", cost[i]);
  }
}


Compiler output:
a.cc:8:9: error: expected nested-name-specifier before 'T'
    8 |   using T
      |         ^
a.cc:15:19: error: 'edge' was not declared in this scope
   15 |   vector< vector< edge > > g;
      |                   ^~~~
a.cc:15:24: error: template argument 1 is invalid
   15 |   vector< vector< edge > > g;
      |                        ^
a.cc:15:24: error: template argument 2 is invalid
a.cc:15:26: error: template argument 1 is invalid
   15 |   vector< vector< edge > > g;
      |                          ^
a.cc:15:26: error: template argument 2 is invalid
a.cc: In member function 'void WeightedGraph<T>::add_edge(int, int, T)':
a.cc:23:6: error: invalid types 'int[int]' for array subscript
   23 |     g[u].emplace_back((edge) {v, cost});
      |      ^
a.cc:23:24: error: 'edge' was not declared in this scope
   23 |     g[u].emplace_back((edge) {v, cost});
      |                        ^~~~
a.cc: In member function 'size_t WeightedGraph<T>::size() const':
a.cc:28:14: error: request for member 'size' in '((const WeightedGraph<T>*)this)->WeightedGraph<T>::g', which is of non-class type 'const int'
   28 |     return g.size();
      |              ^~~~
a.cc: In instantiation of 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]':
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
a.cc:49:5: error: no matching function for call to 'begin(std::pair<int, int>&)'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
In file included from /usr/include/c++/14/bits/algorithmfwd.h:39,
                 from /usr/include/c++/14/bits/stl_algo.h:59,
                 from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/initializer_list:88:5: note: candidate: 'template<class _Tp> constexpr const _Tp* std::begin(initializer_list<_Tp>)'
   88 |     begin(initializer_list<_Tp> __ils) noexcept
      |     ^~~~~
/usr/include/c++/14/initializer_list:88:5: note:   template argument deduction/substitution failed:
a.cc:49:5: note:   'std::pair<int, int>' is not derived from 'std::initializer_list<_Tp>'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/range_access.h:52:5: note: candidate: 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&)'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:52:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/range_access.h: In substitution of 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(_Container&) [with _Container = std::pair<int, int>]':
a.cc:49:5:   required from 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
/usr/include/c++/14/bits/range_access.h:52:50: error: 'struct std::pair<int, int>' has no member named 'begin'
   52 |     begin(_Container& __cont) -> decltype(__cont.begin())
      |                                           ~~~~~~~^~~~~
a.cc: In instantiation of 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]':
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note: candidate: 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&)'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:63:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/range_access.h: In substitution of 'template<class _Container> constexpr decltype (__cont.begin()) std::begin(const _Container&) [with _Container = std::pair<int, int>]':
a.cc:49:5:   required from 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
/usr/include/c++/14/bits/range_access.h:63:56: error: 'const struct std::pair<int, int>' has no member named 'begin'
   63 |     begin(const _Container& __cont) -> decltype(__cont.begin())
      |                                                 ~~~~~~~^~~~~
a.cc: In instantiation of 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]':
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note: candidate: 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])'
   95 |     begin(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~~~
/usr/include/c++/14/bits/range_access.h:95:5: note:   template argument deduction/substitution failed:
a.cc:49:5: note:   mismatched types '_Tp [_Nm]' and 'std::pair<int, int>'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166:
/usr/include/c++/14/valarray:1227:5: note: candidate: 'template<class _Tp> _Tp* std::begin(valarray<_Tp>&)'
 1227 |     begin(valarray<_Tp>& __va) noexcept
      |     ^~~~~
/usr/include/c++/14/valarray:1227:5: note:   template argument deduction/substitution failed:
a.cc:49:5: note:   'std::pair<int, int>' is not derived from 'std::valarray<_Tp>'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
/usr/include/c++/14/valarray:1238:5: note: candidate: 'template<class _Tp> const _Tp* std::begin(const valarray<_Tp>&)'
 1238 |     begin(const valarray<_Tp>& __va) noexcept
      |     ^~~~~
/usr/include/c++/14/valarray:1238:5: note:   template argument deduction/substitution failed:
a.cc:49:5: note:   'std::pair<int, int>' is not derived from 'const std::valarray<_Tp>'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
a.cc:49:5: error: no matching function for call to 'end(std::pair<int, int>&)'
/usr/include/c++/14/initializer_list:99:5: note: candidate: 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:   template argument deduction/substitution failed:
a.cc:49:5: note:   'std::pair<int, int>' is not derived from 'std::initializer_list<_Tp>'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note: candidate: 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/range_access.h: In substitution of 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&) [with _Container = std::pair<int, int>]':
a.cc:49:5:   required from 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
/usr/include/c++/14/bits/range_access.h:74:48: error: 'struct std::pair<int, int>' has no member named 'end'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |                                         ~~~~~~~^~~
a.cc: In instantiation of 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]':
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
/usr/include/c++/14/bits/range_access.h:85:5: note: candidate: 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/range_access.h: In substitution of 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&) [with _Container = std::pair<int, int>]':
a.cc:49:5:   required from 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
/usr/include/c++/14/bits/range_access.h:85:54: error: 'const struct std::pair<int, int>' has no member named 'end'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |                                               ~~~~~~~^~~
a.cc: In instantiation of 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int) [with T = int]':
a.cc:69:23:   required from here
   69 |   auto cost = dijkstra(g, S);
      |               ~~~~~~~~^~~~~~
/usr/include/c++/14/bits/range_access.h:106:5: note: candidate: 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:   template argument deduction/substitution failed:
a.cc:49:5: note:   mismatched types '_Tp [_Nm]' and 'std::pair<int, int>'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note: candidate: 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:   template argument deduction/substitution failed:
a.cc:49:5: note:   'std::pair<int, int>' is not derived from 'std::valarray<_Tp>'
   49 |     for(auto &e : g[idx]) {
      |     ^~~
/usr/include/c++/14/valarray:1265:5: note: candidate: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |