Language: C++

#include<bits/stdc++.h>
using namespace std;

using Weight=int;
const Weight INF=1e9;
enum{SAME,ASC,DES};



struct Edge{
    int to;
    Weight cost;
};


struct Edge2{
    int to;
    Weight cost;
    char label;
};

using Edges=vector<Edge>;
using Graph=array<Edges,50*50*(1<<10)>;

using Edges2=vector<Edge2>;
using Graph2=vector<Edges2>;
priority_queue<pair<Weight,int>,vector<pair<Weight,int>>,greater<>> que;
array<int,50*50*(1<<10)> used;

void dijkstra(int s,Graph& g,array<Weight,50*50*(1<<10)> &d){
    d[s]=0;
    assert(que.empty());

    fill(used.begin(),used.end(),0);
    que.push(make_pair(Weight(0),s));
    //cerr<<"####"<<endl;
    while(!que.empty()){
        auto ret=que.top(); que.pop();
        int v=ret.second;
        //cerr<<v/3/(1<<10)<<" "<<(v/(1<<10)%3)<<" "<<bitset<10>(v&((1<<10)-1))<<endl;
        if(used[v]) continue;
        used[v]=true;
        for(int i=0;i<g[v].size();i++){
            int to=g[v][i].to;
            //cerr<<"to"<<to/3<<" "<<to%3<<endl;
            Weight w=g[v][i].cost+d[v];
            if(w<d[to] && !used[to]){
                d[to]=w;
                que.push(make_pair(w,to));
            }
        }
    }
    return;
}

int vertex(int i,int j,int mask,int n){
    return i*n*(1<<10)+j*(1<<10)+mask;
}

array<Weight,50*50*(1<<10)> dd;
Graph graph;
Weight solve(int n,int m,vector<Weight>& d,vector<Weight>& e,const Graph2 &g){
    map<int,vector<int>> em;
    for(int i=0;i<n;i++){
        em[e[i]].push_back(i);
    }
    map<int,int> posmap;
    for(auto &e:em){
        for(int i=0;i<e.second.size();i++){
            posmap[e.second[i]]=i;
        }
    }
    
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            for(int l=0;l<(1<<10);l++){
                if(e[i]==e[j]){
                    if(!(l & (1<<posmap[i]))){
                        graph[vertex(i,j,l,n)].push_back(Edge{vertex(i,j,(l|(1<<posmap[i])),n),Weight(0)});
                    }
                    else{
                        graph[vertex(i,j,l,n)].push_back(Edge{vertex(i,j,(l^(1<<posmap[i])),n),Weight(0)});
                    }
                    if(!(l & (1<<posmap[j]))){
                        graph[vertex(i,j,l,n)].push_back(Edge{vertex(i,j,(l|(1<<posmap[j])),n),Weight(0)});
                    }
                    else{
                        graph[vertex(i,j,l,n)].push_back(Edge{vertex(i,j,(l^(1<<posmap[j])),n),Weight(0)});
                    }                    
                }
                for(int k=0;k<g[i].size();k++){
                    if(g[i][k].label==DES) continue;
                    int toMask=l;
                    if(g[i][k].label==ASC && l!=0) continue;
                    Weight w=g[i][k].cost+d[g[i][k].to];
                    if(j==g[i][k].to || (toMask & (1<<posmap[g[i][k].to]))){
                        w-=d[g[i][k].to];
                    }
                    graph[vertex(i,j,l,n)].push_back(Edge{vertex(g[i][k].to,j,toMask,n),w});
                }
                for(int k=0;k<g[j].size();k++){
                    if(g[j][k].label==ASC) continue;
                    int toMask=l;
                    if(g[j][k].label==DES && l!=0) continue;
                    Weight w=g[j][k].cost+d[g[j][k].to];
                    if(i==g[j][k].to || (toMask & (1<<posmap[g[j][k].to]))){
                        w-=d[g[j][k].to];
                    }
                    graph[vertex(i,j,l,n)].push_back(Edge{vertex(i,g[j][k].to,toMask,n),w});
                }
            }
        }
    }
    g.clear();
    fill(dd.begin(),dd.end(),INF);
    dijkstra(vertex(0,0,0,n),graph,dd);
    return dd[vertex(n-1,n-1,0,n)];
}

int main(){
    int n,m;
    while(scanf("%d %d",&n,&m),n){
        vector<Weight> d(n);
        vector<Weight> e(n);
        d[0]=0;
        e[0]=0;
        for(int i=0;i<n-2;i++){
            scanf("%d %d",&d[i+1],&e[i+1]);
        }
        d[n-1]=0;
        e[n-1]=1000;
        Graph2 g(n);
        for(int i=0;i<m;i++){
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            a--,b--;
            if(e[a]<e[b]) g[a].push_back(Edge2{b,c,ASC});
            else if(e[a]>e[b]) g[b].push_back(Edge2{a,c,DES});
            else{
                g[a].push_back(Edge2{b,c,SAME});
                g[b].push_back(Edge2{a,c,SAME});
            }
        }
        auto ret=solve(n,m,d,e,g);
        printf("%d\n",(ret>=INF ? Weight(-1) : ret));
    }

    return 0;
}


Compiler output:
a.cc: In function 'Weight solve(int, int, std::vector<int>&, std::vector<int>&, const Graph2&)':
a.cc:114:12: error: passing 'const Graph2' {aka 'const std::vector<std::vector<Edge2> >'} as 'this' argument discards qualifiers [-fpermissive]
  114 |     g.clear();
      |     ~~~~~~~^~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1607:7: note:   in call to 'void std::vector<_Tp, _Alloc>::clear() [with _Tp = std::vector<Edge2>; _Alloc = std::allocator<std::vector<Edge2> >]'
 1607 |       clear() _GLIBCXX_NOEXCEPT
      |       ^~~~~