Language: C++

//做法1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define sp cout<<"---------------------------------------------------"<<endl;
struct node {
    int x,y,c;
}e[2000100];
int id[2000100],fa[2000100],n,m,newn,d[2000100],used[2000100];
vector<int>v[2000100];
inline bool cmp(const node a,const node b){
      return a.c<b.c;
}
inline int sf(int x){return fa[x]==x?x:fa[x]=sf(fa[x]);}
inline void deal(int le,int ri){
      int i;
      for(i=le;i<=ri;i++){
          fa[e[i].x]=e[i].x;
        fa[e[i].y]=e[i].y;
        used[e[i].x]=used[e[i].y]=0;
        id[e[i].x]=id[e[i].y]=-1;
      }
      for(i=le;i<=ri;i++)
        if(sf(e[i].x)!=sf(e[i].y)){
          fa[sf(e[i].x)]=sf(e[i].y);
        }
      for(i=le;i<=ri;i++){
          int xx=sf(e[i].x),yy=sf(e[i].y);
          if(!used[e[i].x]){
            if(id[xx]==-1)id[xx]=++newn;
            v[e[i].x].push_back(id[xx]);
            v[id[xx]].push_back(e[i].x);
            used[e[i].x]=1;
        }
        if(!used[e[i].y]){
            if(id[yy]==-1)id[yy]=++newn;
            v[e[i].y].push_back(id[yy]);
            v[id[yy]].push_back(e[i].y);
            used[e[i].y]=1;
        }
      }
}
queue<int>q;
int main(){
      int i,l=1;
      scanf("%d%d",&n,&m);
      newn=n;
      for(i=1;i<=m;i++)scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].c);
      sort(e+1,e+m+1,cmp);
      for(i=2;i<=m;i++)
        if(e[i].c!=e[i-1].c){
          deal(l,i-1);
          l=i;
        }
      deal(l,m);
      memset(d,-1,sizeof(d));
      d[1]=0;
      q.push(1);
      while(!q.empty()){
        int x=q.front();
        q.pop();
        for(i=0;i<(int)v[x].size();i++)
          if(d[v[x][i]]==-1){
            d[v[x][i]]=d[x]+1;
            q.push(v[x][i]);
          }
      }
      if(d[n]==-1)puts("-1");
        else cout<<d[n]/2<<endl;
      return 0;
}
//做法2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define sp cout<<"---------------------------------------------------"<<endl;
#define foreach(i,dat) for(__typeof(dat.begin()) i = dat.begin();i!=dat.end();++i)
const int inf=1e9+7;
struct node {
      int p,b,c;
};
inline bool operator < (node x,node y){return x.c>y.c;}
map<int,vector<int> >e[100100];
vector<int>v[100100];
map<int,int>d[100100];
priority_queue<node>q;
int main(){
      int n,m,i,x,y,z;
      scanf("%d%d",&n,&m);
      for(i=1;i<=m;i++){
          scanf("%d%d%d",&x,&y,&z);
          x--,y--;
          e[x][z].push_back(y);
          e[y][z].push_back(x);
          v[x].push_back(z);
          v[y].push_back(z);
      }
      q.push({0,0,0});;
      d[0][0]=0;
      while(!q.empty()){
          node p=q.top();
          q.pop();
          if(d[p.p][p.b]!=p.c)continue;
          if(d[p.p].find(0)==d[p.p].end()||d[p.p][0]>p.c){
            d[p.p][0]=p.c;
            q.push({p.p,0,p.c});
        }
        if(!p.b){
          for(i=0;i<(int)v[p.p].size();i++)
            if(d[p.p].find(v[p.p][i])==d[p.p].end()||d[p.p][v[p.p][i]]>p.c+1){
              d[p.p][v[p.p][i]]=p.c+1;
              q.push({p.p,v[p.p][i],p.c+1});
            }
          continue;
        }
        for(i=0;i<(int)e[p.p][p.b].size();i++)
          if(d[e[p.p][p.b][i]].find(p.b)==d[e[p.p][p.b][i]].end()
             ||d[e[p.p][p.b][i]][p.b]>p.c){
              d[e[p.p][p.b][i]][p.b]=p.c;
              q.push({e[p.p][p.b][i],p.b,p.c});
          }
      }
      int ans=inf;
      for(map<int,int>::iterator it=d[n-1].begin();it!=d[n-1].end();it++)
        ans=min(ans,it->second);
      if(ans==inf)puts("-1");
        else printf("%d\n",ans);
      return 0;
}

Compiler output:
a.cc:103:8: error: redefinition of 'struct node'
  103 | struct node {
      |        ^~~~
a.cc:18:8: note: previous definition of 'struct node'
   18 | struct node {
      |        ^~~~
a.cc:107:22: error: conflicting declaration 'std::map<int, std::vector<int> > e [100100]'
  107 | map<int,vector<int> >e[100100];
      |                      ^
a.cc:20:2: note: previous declaration as 'node e [2000100]'
   20 | }e[2000100];
      |  ^
a.cc:108:12: error: conflicting declaration 'std::vector<int> v [100100]'
  108 | vector<int>v[100100];
      |            ^
a.cc:22:12: note: previous declaration as 'std::vector<int> v [2000100]'
   22 | vector<int>v[2000100];
      |            ^
a.cc:109:13: error: conflicting declaration 'std::map<int, int> d [100100]'
  109 | map<int,int>d[100100];
      |             ^
a.cc:21:38: note: previous declaration as 'int d [2000100]'
   21 | int id[2000100],fa[2000100],n,m,newn,d[2000100],used[2000100];
      |                                      ^
a.cc:110:21: error: conflicting declaration 'std::priority_queue<node> q'
  110 | priority_queue<node>q;
      |                     ^
a.cc:55:11: note: previous declaration as 'std::queue<int> q'
   55 | queue<int>q;
      |           ^
a.cc:111:5: error: redefinition of 'int main()'
  111 | int main(){
      |     ^~~~
a.cc:56:5: note: 'int main()' previously defined here
   56 | int main(){
      |     ^~~~
a.cc: In function 'int main()':
a.cc:117:15: error: no match for 'operator[]' (operand types are 'node' and 'int')
  117 |           e[x][z].push_back(y);
      |               ^
a.cc:118:15: error: no match for 'operator[]' (operand types are 'node' and 'int')
  118 |           e[y][z].push_back(x);
      |               ^
a.cc:122:13: error: no matching function for call to 'std::queue<int>::push(<brace-enclosed initializer list>)'
  122 |       q.push({0,0,0});;
      |       ~~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/queue:66,
                 from a.cc:10:
/usr/include/c++/14/bits/stl_queue.h:283:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >; value_type = int]'
  283 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:283:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::queue<int>::value_type&' {aka 'const int&'}
  283 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:288:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(value_type&&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >; value_type = int]'
  288 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:288:25: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::queue<int>::value_type&&' {aka 'int&&'}
  288 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc:123:11: error: invalid types 'int[int]' for array subscript
  123 |       d[0][0]=0;
      |           ^
a.cc:125:20: error: 'class std::queue<int>' has no member named 'top'; did you mean 'pop'?
  125 |           node p=q.top();
      |                    ^~~
      |                    pop
a.cc:127:18: error: 'struct node' has no member named 'p'
  127 |           if(d[p.p][p.b]!=p.c)continue;
      |                  ^
a.cc:127:23: error: 'struct node' has no member named 'b'
  127 |           if(d[p.p][p.b]!=p.c)continue;
      |                       ^
a.cc:128:18: error: 'struct node' has no member named 'p'
  128 |           if(d[p.p].find(0)==d[p.p].end()||d[p.p][0]>p.c){
      |                  ^
a.cc:128:34: error: 'struct node' has no member named 'p'
  128 |           if(d[p.p].find(0)==d[p.p].end()||d[p.p][0]>p.c){
      |                                  ^
a.cc:128:48: error: 'struct node' has no member named 'p'
  128 |           if(d[p.p].find(0)==d[p.p].end()||d[p.p][0]>p.c){
      |                                                ^
a.cc:129:17: error: 'struct node' has no member named 'p'
  129 |             d[p.p][0]=p.c;
      |                 ^
a.cc:130:23: error: 'struct node' has no member named 'p'
  130 |             q.push({p.p,0,p.c});
      |                       ^
a.cc:130:19: error: no matching function for call to 'std::queue<int>::push(<brace-enclosed initializer list>)'
  130 |             q.push({p.p,0,p.c});
      |             ~~~~~~^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:283:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >; value_type = int]'
  283 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:283:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::queue<int>::value_type&' {aka 'const int&'}
  283 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:288:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(value_type&&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >; value_type = int]'
  288 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:288:25: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::queue<int>::value_type&&' {aka 'int&&'}
  288 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc:132:15: error: 'struct node' has no member named 'b'
  132 |         if(!p.b){
      |               ^
a.cc:133:30: error: 'struct node' has no member named 'p'
  133 |           for(i=0;i<(int)v[p.p].size();i++)
      |                              ^
a.cc:134:20: error: 'struct node' has no member named 'p'
  134 |             if(d[p.p].find(v[p.p][i])==d[p.p].end()||d[p.p][v[p.p][i]]>p.c+1){
      |                    ^
a.cc:134:32: error: 'struct node' has no member named 'p'
  134 |             if(d[p.p].find(v[p.p][i])==d[p.p].end()||d[p.p][v[p.p][i]]>p.c+1){
      |                                ^
a.cc:134:44: error: 'struct node' has no member named 'p'
  134 |             if(d[p.p].find(v[p.p][i])==d[p.p].end()||d[p.p][v[p.p][i]]>p.c+1){
      |                                            ^
a.cc:134:58: error: 'struct node' has no member named 'p'
  134 |             if(d[p.p].find(v[p.p][i])==d[p.p].end()||d[p.p][v[p.p][i]]>p.c+1){
      |                                                          ^
a.cc:134:65: error: 'struct node' has no member named 'p'
  134 |             if(d[p.p].find(v[p.p][i])==d[p.p].end()||d[p.p][v[p.p][i]]>p.c+1){
      |                                                                 ^
a.cc:135:19: error: 'struct node' has no member named 'p'
  135 |               d[p.p][v[p.p][i]]=p.c+1;
      |                   ^
a.cc:135:26: error: 'struct node' has no member named 'p'
  135 |               d[p.p][v[p.p][i]]=p.c+1;
      |                          ^
a.cc:136:25: error: 'struct node' has no member named 'p'
  136 |               q.push({p.p,v[p.p][i],p.c+1});
      |                         ^
a.cc:136:31: error: 'struct node' has no member named 'p'
  136 |               q.push({p.p,v[p.p][i],p.c+1});
      |                               ^
a.cc:136:21: error: no matching function for call to 'std::queue<int>::push(<brace-enclosed initializer list>)'
  136 |               q.push({p.p,v[p.p][i],p.c+1});
      |               ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:283:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >; value_type = int]'
  283 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:283:30: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const std::queue<int>::value_type&' {aka 'const int&'}
  283 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:288:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(value_type&&) [with _Tp = int; _Sequence = std::deque<int, std::allocator<int> >; value_type = int]'
  288 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:288:25: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'std::queue<int>::value_type&&' {aka 'int&&'}
  288 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc:140:28: error: 'struct node' has no member named 'p'
  140 |         for(i=0;i<(int)e[p.p][p.b].size();i++)
      |                            ^
a.cc:140:33: error: 'struct node' has no member named 'b'
  140 |         for(i=0;i<(int)e[p.p][p.b].size();i++)
      |                                 ^
a.cc:141:20: error: 'struct node' has no member named 'p'
  141 |           if(d[e[p.p][p.b][i]].find(p.b)==d[e[p.p][p.b][i]].end()
      |                    ^
a.cc:141:25: error: 'struct node' has no member named 'b'
  141 |           if(d[e[p.p][p.b][i]].find(p.b)==d[e[p.p][p.b][i]].end()
      |                         ^
a.cc:141:39: error: 'struct node' has no member named 'b'
  141 |           if(d[e[p.p][p.b][i]].find(p.b)==d[e[p.p][p.b][i]].end()
      |                                       ^
a.cc:141:49: error: 'struct node' has no member named 'p'
  141 |           if(d[e[p.p][p.b][i]].find(p.b)==d[e[p.p][p.b][i]].end()
      |                                                 ^
a.cc:141:54: error: 'struct node' has no member named 'b'
  141 |           if(d[e[p.p][p.b][i]].find(p.b)==d[e[p.p][p.b][i]].end()
      |                                                      ^
a.cc:142:22: error: 'struct node' has no member named 'p'
  142 |              ||d[e[p.p][p.b][i]][p.b]>p.c){
      |                      ^
a.cc:142:27: error: 'struct node' has no member named 'b'
  142 |              ||d[e[p.p][p.b][i]][p.b]>p.c){
      |                           ^
a.cc:142:36: error: 'struct node' has no member named 'b'
  142 |              |