Language: C++

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int INF = INT_MAX / 2;

int main()
{
    int w, h, n, sx, sy;
    cin >> w >> h >> n >> sx >> sy;

    vector<vector<bool> > grid(h+2, vector<bool>(w+2, true));
    vector<vector<vector<int> > > fire(h+2, vector<vector<int> >(w+2, vector<int>(n, -1)));
    for(int y=1; y<=h; ++y){
        for(int x=1; x<=w; ++x){
            grid[y][x] = false;
        }
    }

    vector<int> len(n);
    vector<vector<int> > px(n), py(n);
    for(int i=0; i<n; ++i){
        cin >> len[i];
        px[i].resize(len[i]);
        py[i].resize(len[i]);
        for(int j=0; j<len[i]; ++j){
            cin >> px[i][j] >> py[i][j];
            fire[py[i][j]][px[i][j]][i] = j;
        }
        grid[py[i][len[i]-1]][px[i][len[i]-1]] = true;
    }

    vector<vector<vector<int> > > dp(h+2, vector<vector<int> >(w+2, vector<int>(1<<n, INF)));
    multimap<int, tuple<int, int, bitset<5> > > mm;
    dp[sy][sx][0] = true;
    mm.insert(make_pair(0, make_tuple(sy, sx, 0)));

    while(!mm.empty()){
        int cost, y, x;
        bitset<5> bs;
        tie(cost, tie(y, x, bs)) = *mm.begin();
        mm.erase(mm.begin());
        if(dp[y][x][bs.to_ulong()] < cost)
            continue;

        for(int i=0; i<n; ++i){
            if(!bs[i] && cost <= fire[y][x][i]){
                int cost2 = fire[y][x][i];
                bitset<5> bs2 = bs;
                bs2[i] = true;
                if(cost2 < dp[y][x][bs2.to_ulong()]){
                    dp[y][x][bs2.to_ulong()] = cost2;
                    mm.insert(make_pair(cost2, make_tuple(y, x, bs2)));
                }
            }
        }

        for(int i=0; i<9; ++i){
            int y2 = y - 1 + i / 3;
            int x2 = x - 1 + i % 3;
            int cost2 = cost + 1;
            if(!grid[y2][x2] && cost2 < dp[y2][x2][bs.to_ulong()]){
                dp[y2][x2][bs.to_ulong()] = cost2;
                mm.insert(make_pair(cost2, make_tuple(y2, x2, bs)));
            }
        }
    }

    int ans = INF;
    for(int y=1; y<=h; ++y){
        for(int x=1; x<=w; ++x){
            ans = min(ans, dp[y][x][(1<<n)-1]);
        }
    }
    if(ans < INF)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:59:22: error: cannot bind non-const lvalue reference of type 'std::tuple<int&, int&, std::bitset<5>&>&' to an rvalue of type 'std::tuple<int&, int&, std::bitset<5>&>'
   59 |         tie(cost, tie(y, x, bs)) = *mm.begin();
      |                   ~~~^~~~~~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68,
                 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/tuple:2791:19: note:   initializing argument 2 of 'constexpr std::tuple<_Elements& ...> std::tie(_Elements& ...) [with _Elements = {int, tuple<int&, int&, bitset<5>&>}]'
 2791 |     tie(_Elements&... __args) noexcept
      |         ~~~~~~~~~~^~~~~~~~~~