Guest User

Untitled

a guest
Jul 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<queue>
  4. #include<cstring>
  5.  
  6. using namespace std;
  7.  
  8. struct shipping_info
  9. {
  10.     char label[500];
  11.     int adjacents[1000];
  12.     int size;
  13.     int ancestor;
  14. }list[1000];
  15.  
  16. int depth[1000];
  17. char start[500],end[500];
  18.  
  19. queue<int>q;
  20.  
  21. int get_pos(char *city,int size)
  22. {
  23.     int i;
  24.     for(i=0;i<size;i++)
  25.     if(!strcmp(list[i].label,city))
  26.     return i;
  27.     return -1;
  28. }
  29.  
  30. void print_sequence(int end)
  31. {
  32.     if(depth[end]==1)
  33.     {
  34.         printf("%s ",list[end].label);
  35.         return;
  36.     }
  37.     print_sequence(list[end].ancestor);
  38.     printf("%s\n%s ",list[end].label,list[end].label);
  39. }
  40.  
  41. void print_shortest_path(int start,int end)
  42. {
  43.     int temp,temp2,i,level,flag;
  44.     while(!q.empty())
  45.     q.pop();
  46.     q.push(start);
  47.     depth[start]=1;
  48.     for(flag=0;!q.empty();)
  49.     {
  50.         temp=q.front();
  51.         q.pop();
  52.         level=depth[temp];
  53.         for(i=0;i<list[temp].size;i++)
  54.         {
  55.             temp2=list[temp].adjacents[i];
  56.             if(!depth[temp2])
  57.             {
  58.                 depth[temp2]=level+1;
  59.                 list[temp2].ancestor=temp;
  60.                 q.push(temp2);
  61.                 if(temp2==end)
  62.                 {
  63.                     flag=1;
  64.                     break;
  65.                 }
  66.             }
  67.         }
  68.         if(flag) break;
  69.     }
  70.     if(flag)
  71.     {
  72.         print_sequence(list[end].ancestor);
  73.         printf("%s\n",list[end].label);
  74.     }
  75.     else
  76.     puts("No route");
  77. }
  78.  
  79. int main()
  80. {
  81.     freopen("a.txt","r",stdin);
  82.     int edges,i,verteces,position1,position2,list_size,s,e;
  83.     char city1[500],city2[500],test;
  84.     for(list_size=0,test=0;scanf("%d",&edges)!=EOF;list_size=0)
  85.     {
  86.         if(test)
  87.         puts("");
  88.         else
  89.         test=1;
  90.         for(i=0;i<edges;i++)
  91.         {
  92.             scanf("%s%s",city1,city2);
  93.             position1=get_pos(city1,list_size);
  94.             if(position1==-1)
  95.             {
  96.                 position1=list_size++;
  97.                 strcpy(list[position1].label,city1);
  98.                 list[position1].size=0;
  99.             }
  100.             position2=get_pos(city2,list_size);
  101.             if(position2==-1)
  102.             {
  103.                 position2=list_size++;
  104.                 strcpy(list[position2].label,city2);
  105.                 list[position2].size=0;
  106.             }
  107.             list[position1].adjacents[list[position1].size]=position2;
  108.             list[position2].adjacents[list[position2].size]=position1;
  109.             list[position2].size++; list[position1].size++;
  110.         }
  111.         for(i=0;i<=verteces;i++)
  112.         depth[i]=0;
  113.         verteces=list_size;
  114.         scanf("%s%s",start,end);
  115.         s=get_pos(start,verteces);
  116.         e=get_pos(end,verteces);
  117.         print_shortest_path(s,e);
  118.     }
  119.     return 0;
  120. }
Add Comment
Please, Sign In to add comment