Short-circuits template seek short-circuits between any two points

.

. 1 #include <bits / STDC ++ H.>
 2  #define MP (I, J) the make_pair (I, J)
 . 3  #define P pair <int, int>
 . 4  the using  namespace STD;
 . 5  const  int INF = 0x3f3f3f3f ;
 . 6  const  int = 5E3 + MAXN 10 ;;
 . 7  int n-, T;
 . 8  int DIS1 [MAXN], CDIS [MAXN];
 . 9  int Judge [MAXN];   // used to mark the single-source shortest point (the second shortest) path traveled whether 
10 Vector <P> E [MAXN], VIS [MAXN];
 . 11  void Storage ( int A,int B, int C)
 12 is  {
 13 is      E [A] .push_back (mp (C, B));     // where similar manner matrix deposit side, the first element mp is weight; 
14  }
 15  void Book ( int x)
 16  {
 . 17      for ( int I = . 1 ; I <= n-; I ++)   // single source path x update; 
18 is          VIS [x] .push_back (MP (I, CDIS [I]));
 . 19  }
 20 is  void Dij ( int A)
 21 is  {
 22 is      int to, W;
 23 is      The priority_queue <P, Vector <P>, Greater <P>> Q;  // priority queue is updated every time the minimum 
24      Memset (DIS1, INF, the sizeof (DIS1));
 25      Memset (CDIS, INF, the sizeof (CDIS));   // initialize 
26 is  
27      q.push (MP ( 0 , a));   // the first entry point 
28      DIS1 [a] = 0 ;
 29  
30      the while ! ( q.empty ()) {
 31 is          P = U q.top ();
 32          q.pop ();
 33 is          IF (u.first> CDIS [u.second])   // this is one of optimization, if the value of this point is greater than it short-circuits, 
34 is              Continue ;                //就跳过
35         for(int i=0;i<e[u.second].size();++i){
36             //dis[v]>dis[u]+w    //这里类似于这种;
37             int next=e[u.second][i].first+u.first;  //dis[u]+w;  
38             int now=e[u.second][i].second;  //dis[v];
39             if(next<dis1[now]){
40                 swap(dis1[now],next);
41                 q.push(mp(dis1[now],now));
42             }
43             if(next<cdis[now]) {
44                 swap(cdis[now],next);
45                 q.push(mp(cdis[now],now));
46             }
47         }
48     }
49     book(a);
50 }
51 int main()
52 {
53     int m;
54     scanf("%d%d",&n,&m);
55     while(m--){
56         int t1,t2,t3;
57         scanf("%d%d%d",&t1,&t2,&t3);
58         storage(t1,t2,t3);
59     }
60     int q;
61     scanf("%d",&q);
62     while(q--){
63         int x,y;
64         scanf("%d%d",&x,&y);
65         if(!judge[x]) dij(x),judge[x]=1;
66         for(int i=0;i<vis[x].size();i++){
67             if(vis[x][i].first==y){
68                 if(vis[x][i].second==inf)
69                     printf("no route\n");
70                 else printf("%d\n",vis[x][i].second);
71                 break;
72             }
73         }
74     }
75     return 0;
76 }

 

Guess you like

Origin www.cnblogs.com/pangbi/p/11845683.html