Uvalive 6302 Star Travel

Source: Internet
Author: User

Idea: To find out all the shortest, and then run once just fine.

To find out all the shortest possible is, the opposite side, and then from the e-run the quickest way.

Then it is running the shortest way from S. Then start with S, and follow the shortest path (the smallest dictionary order) every time.

Determine if an edge is the shortest, i.e. Dis[u]+d[v]+hehe=d

#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<map>#include<cmath>#include<queue>#include<cstring>#include<Set>#include<stack>#include<string>#include<ctime>#defineLL Long Long#defineU64 unsigned long Long#defineMAXN 5010#defineMAX 500010#defineINF 0x3f3f3f3f#defineEPS 1e-6using namespacestd;structnode{intnext,to; Charstr[Ten] ;} Edge[max];intHEAD[MAXN],DIS[MAXN],TOP,D[MAXN];intmat[ -][ -], POS[MAXN];BOOLVI[MAXN]; vector<int>QE[MAXN];voidUnit (intXintYChar*s) {edge[top].to=y;edge[top].next=Head[x]; strcpy (edge[top].str,s); Head[x]=top++;}voidSPFA1 (intSintEintdis[]) {memset (vi,0,sizeof(vi)); Vi[s]=true; Queue<int>Q;    Q.push (s); Dis[s]=0; intI,v,u,hehe;  while(!Q.empty ()) {u=Q.front (); Q.pop ();  for(i =0; I < qe[u].size (); i++) {v=Qe[u][i]; if(Mat[pos[u]][pos[v]]) hehe=0; ElseHehe=1; if(dis[v]>hehe+Dis[u]) {Dis[v]=hehe+Dis[u]; if(!Vi[v]) {Vi[v]=true;                Q.push (v); }}} Vi[u]=false; }}voidSPFA (intSintEintdis[]) {memset (vi,0,sizeof(vi)); Vi[s]=true; Queue<int>Q;    Q.push (s); Dis[s]=0; intI,v,u,hehe;  while(!Q.empty ()) {u=Q.front (); Q.pop ();  for(i = head[u]; I! =-1; i=Edge[i].next) {v=edge[i].to; if(Mat[pos[u]][pos[v]]) hehe=0; ElseHehe=1; if(dis[v]>hehe+Dis[u]) {Dis[v]=hehe+Dis[u]; if(!Vi[v]) {Vi[v]=true;                Q.push (v); }}} Vi[u]=false; }}voidSolveintSintEintD) {    intI, id, hehe, v; Charstr[Ten] ; BOOLflag=false;  while(s! =e) {ID=-1;  for(i = head[s]; I! =-1; i =Edge[i].next) {v=edge[i].to; if(Mat[pos[s]][pos[v]]) hehe=0; ElseHehe=1; if(hehe+dis[s]+d[v]==D) {if(id==-1) {ID=v;                strcpy (STR,EDGE[I].STR); }                Else if(strcmp (STR,EDGE[I].STR) >1) {ID=v;                strcpy (STR,EDGE[I].STR); }            }        }      //cout<<s<< "" << id<<endl;        if(id==-1)return ; if(!Mat[pos[s]][pos[id]]) {            if(flag) printf ("%s", str); Elseprintf"%s", str); Flag=true; } s=ID; } puts ("");}void  out(intNint*dis) {     for(intI=0; i<n;i++) cout<<dis[i]<<" ";p UTS ("");}intMain () {intI,n,m,j,len; intt,k,x; Charstr[ +]; CIN>>T;  while(t--) {scanf ("%d",&N); Top=0; memset (Head,-1,sizeof(head));  for(i=0; i<maxn;i++) qe[i].clear ();  for(i =1; I <= N; i++) {scanf ("%d%s%d",&k,str,&m); POS[K]=str[0]-'A';  while(m--) {scanf ("%s%d",str,&j);                Unit (K,J,STR);            Qe[j].push_back (k); }} scanf ("%d",&m);  while(m--) {scanf ("%d%d%s",&i,&j,str); Memset (Mat,0,sizeof(MAT)); Len=strlen (str);  for(inti =0; i < Len; i++)                 for(intj =0; J < Len; J + +) Mat[str[i]-'A'][str[j]-'A']=true; memset (Dis,inf,sizeof(DIS)); memset (D,inf,sizeof(d));            SPFA1 (J,I,D);            SPFA (I,j,dis);        Solve (i,j,dis[j]); }    }    return 0 ;}/*a 2 T0 1 T2 a 1 T1 1 a 1 T3 1 T4 4 T5 a 2 A60 1 T0 a 1 T2 a 2 T1 3 T3 B 1 T6 b 1 T5 1 T7 420 4 A5 3 a*/
View Code

Uvalive 6302 Star Travel

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.