Language: C++


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1 << 30;
struct Edge
{
    int to;
    int cost;
};

int main()
{
    int n;
    vector<vector<Edge>> ab(n);
    for (int i = 0; i < n - 1; i++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        ab[a].emplace_back(b, c);
        ab[b].emplace_back(a, c);
    }
    ll q, k;
    cin >> q >> k;
    k--;
    vector<P> xy;
    for (int i = 0; i < q; i++)
    {
        ll x, y;
        cin >> x >> y;
        xy.emplace_back(x, y);
    }
    priority_queue<P, vector<P>, greater<P>> Q;
    Q.push(make_pair(k, 0));
    vector<ll> dist(n + 5, INF);
    while (!Q.empty())
    {
        auto item = Q.top();
        Q.pop();
        for (int i = 0; i < (int)ab[item.first].size(); i++)
        {
            ll temp_to = ab[item.first][i].to;
            ll temp_cost = ab[item.first][i].cost;
            if (dist[temp_to] <= dist[item.first] + temp_cost)
            {
                continue;
            }
            dist[temp_to] = dist[item.first] + temp_cost;
            Q.emplace(dist[temp_to], temp_to);
        }
    }
    for (int i = 0; i < q; i++)
    {
        cout << dist[xy[i].first] + dist[xy[i].second] << endl;
    }
    return 0;
}


Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Edge; _Args = {long long int&, long long int&}; _Tp = Edge]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = Edge; _Args = {long long int&, long long int&}; _Tp = Edge; allocator_type = std::allocator<Edge>]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {long long int&, long long int&}; _Tp = Edge; _Alloc = std::allocator<Edge>; reference = Edge&]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:29:27:   required from here
   29 |         ab[a].emplace_back(b, c);
      |         ~~~~~~~~~~~~~~~~~~^~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: new initializer expression list treated as compound expression [-fpermissive]
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'Edge::Edge(long long int&)'
a.cc:13:8: note: candidate: 'Edge::Edge()'
   13 | struct Edge
      |        ^~~~
a.cc:13:8: note:   candidate expects 0 arguments, 1 provided
a.cc:13:8: note: candidate: 'constexpr Edge::Edge(const Edge&)'
a.cc:13:8: note:   no known conversion for argument 1 from 'long long int' to 'const Edge&'
a.cc:13:8: note: candidate: 'constexpr Edge::Edge(Edge&&)'
a.cc:13:8: note:   no known conversion for argument 1 from 'long long int' to 'Edge&&'