UVA 11374 Airport Express (single source shortest, Dijkstra, deformed)

Source: Internet
Author: User

Test instructions: To a picture, from the s point to the e point, the figure has two non-edge in two sets, the first set can be used indefinitely, the second set of edges can only pick 1. Q How to make the shortest distance? The output path, whichever edge in the second set is used, the shortest distance.

Ideas:

(1) Simple and easy to operate method: Since the second set of the side can only have 1, on the poor lift these edges, the possible edge set to find the shortest path, while recording 3 answers. The complexity is O (m*k).

(2) The time complexity is low: may wish to ask from S to each other point distance d1[i], then the distance from E to each other point d2[i], next to the second set of each edge u-v, the shortest distance is d1[u]+dis[u][v]+d2[v], notice the edge is non-direction. Trouble in the record path, or very easy to operate. The complexity is O (2m+k).

Here's the code for the first method:

1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn=1010;7 intN, M, K;8vector<int>Vect[n];9 intedge_cnt;Ten structnode One { A     int  from, to, dis, tag; - node () {}; -Nodeint  from,intTo,intDisintTAG): from( from), to, dis (dis), tag (tag) {}; the}edge[n*5]; -  -  -  + voidAdd_node (int  from,intTo,intDisinttag) - { +Edge[edge_cnt]=node ( from, To,dis,tag); Avect[ from].push_back (edge_cnt++); at } -  -  - intTag[n], dist[n], vis[n], path[n]; - intDijkstraintSinte) - { inmemset (Dist,0x7f,sizeof(Dist)); -memset (Tag,0,sizeof(tag)); tomemset (Vis,0,sizeof(Vis)); +memset (Path,0,sizeof(path)); -  thepriority_queue<pii,vector<pii>,greater<pii> >que; *Que.push (Make_pair (0, s)); $dist[s]=0;Panax Notoginseng  -      while(!que.empty ()) the     { +         intx=Que.top (). Second; A Que.pop (); the         if(Vis[x])Continue; +vis[x]=1; -          for(intI=0; I<vect[x].size (); i++) $         { $Node e=Edge[vect[x][i]]; -             if(e.tag>0&& dist[e.to]>dist[x]+E.dis) -             { thedist[e.to]=dist[x]+E.dis; -path[e.to]=x;Wuyi                 if(e.tag==2) the                 { -tag[e.to]=true;//We have a quick line at this point. Wu                 } - Que.push (Make_pair (dist[e.to],e.to)); About             } $         } -     } -     returnDist[e]; - } A  + voidCalintS,inte) the { -vector<int>ans; $     intquick=0, Big=dijkstra (s,e), d=e;//run first, no quick lines. the  the      while(d) the     { the Ans.push_back (d); -D=Path[d]; in     } the  the      for(inti=m*2; i< (m+k) *2; i+=2)//Every single quick line you can use. About     { theedge[i].tag=2; theedge[i+1].tag=2; the  +         intdis=Dijkstra (s,e); -         if(dis<big) the         {Bayibig=dis; the ans.clear (); the             intEd=e, tmp=0; -              while(ed) -             { the                 if(Tag[ed]) tmp=path[ed];//It might not have been used . the Ans.push_back (ed); theEd=path[ed]; the             } -quick=tmp;//if not, it will be updated to 0 in time. the         } theedge[i].tag=0; theedge[i+1].tag=0;94     } the  the Reverse (Ans.begin (), Ans.end ()); theprintf"%d", ans[0]);//Be careful with the output format98      for(intI=1; I<ans.size (); i++) printf ("%d", Ans[i]); Aboutprintf"\ n"); - 101     if(quick) printf ("%d\n", quick);102     ElsePuts"Ticket not used");//don't forget.103 104printf"%d\n", big); the }106 107 intMain ()108 {109Freopen ("Input.txt","R", stdin); the     ints=0, E, A, B, C, ttt=0;111      while(~SCANF ("%d%d%d", &n, &s, &e)) the     {113         if(TTT) printf ("\ n"); ttt++;//format Ah!  theEdge_cnt=0; theMemset (Edge,0,sizeof(Edge)); the          for(intI=0; i<=n; i++) vect[i].clear ();117scanf"%d",&m);118          for(intI=0; i<m; i++)119         { -scanf"%d%d%d",&a,&b,&c);121Add_node (A,b,c,1);122Add_node (B,a,c,1);123         }124scanf"%d",&k); the          for(intI=0; i<k; i++)126         {127scanf"%d%d%d",&a,&b,&c); -Add_node (A,b,c,0);129Add_node (B,a,c,0); the         }131 cal (s,e); the     }133     return 0;134}
AC Code

UVA 11374 Airport Express (single source shortest, Dijkstra, deformed)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.