Language: C++


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>

#define MAX_V 600000
#define LL long long
using namespace std;

const int INF = 1000000000;

struct edge { int to, cost; };
typedef pair<int, int> P;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s){
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d,d+V, INF);
    d[s] = 0;
    que.push(P(0,s));
    while(!que.empty()){
        P p = que.top(); que.pop();
        int v = p.second;
        if(d[v]<p.first) continue;
        for(int i=0;i<G[v].size();i++){
            edge e = G[v][i];
            if(d[e.to]>d[v]+e.cost){
                d[e.to] = d[v]+e.cost;
                que.push(P(d[e.to],e.to));
            }
        }
    }
}

map<P, int> m;
int cnt2 = 0;
int N, M;
void f(P x, P y, int c){
    edge *e = new edge;
    e->to = m[y]; e->cost = m[c];
    G[m[x]].push_back(*e);
}

int main()
{
    
    cin >> N >> M;
    int cnt1 = 1;
    
    for(int i=0;i<N;i++){
        m[P(i,-1)]= cnt1++;
    }
    for(int i=0;i<M;i++){
        int a,b,c;
        scanf(" %d %d %d", &a, &b, &c);
        a--; b--;
        P eg0 = P(a,c), eg1 = P(b,c); 
        P ng0 = P(a,-1), ng1 = P(b,-1);
        if(m[eg0]==0){
            m[eg0] = cnt1;
            cnt1++;
            f(eg0,ng0,0); f(ng0,eg0,1);
        }
        if(m[eg1]==0){
            m[eg1] = cnt1;
            cnt1++;
            f(eg1,ng1,0); f(ng1,eg1,1);
        }
        f(eg0,eg1,0);
        f(eg1,eg0,0);
    }
    int end = m[P(N-1,-1)];
    V = cnt1;
    dijkstra(1);
    cout << (d[end]==INF?-1:d[end]) << endl;
    return 0;
}


Compiler output:
a.cc: In function 'void f(P, P, int)':
a.cc:45:30: error: no match for 'operator[]' (operand types are 'std::map<std::pair<int, int>, int>' and 'int')
   45 |     e->to = m[y]; e->cost = m[c];
      |                              ^
In file included from /usr/include/c++/14/map:63,
                 from a.cc:6:
/usr/include/c++/14/bits/stl_map.h:504:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >; mapped_type = int; key_type = std::pair<int, int>]'
  504 |       operator[](const key_type& __k)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_map.h:504:34: note:   no known conversion for argument 1 from 'int' to 'const std::map<std::pair<int, int>, int>::key_type&' {aka 'const std::pair<int, int>&'}
  504 |       operator[](const key_type& __k)
      |                  ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_map.h:524:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](key_type&&) [with _Key = std::pair<int, int>; _Tp = int; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >; mapped_type = int; key_type = std::pair<int, int>]'
  524 |       operator[](key_type&& __k)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_map.h:524:29: note:   no known conversion for argument 1 from 'int' to 'std::map<std::pair<int, int>, int>::key_type&&' {aka 'std::pair<int, int>&&'}
  524 |       operator[](key_type&& __k)
      |                  ~~~~~~~~~~~^~~