Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<cstdio>
- #include<iostream>
- #include<queue>
- #include<cstring>
- using namespace std;
- struct shipping_info
- {
- char label[500];
- int adjacents[1000];
- int size;
- int ancestor;
- }list[1000];
- int depth[1000];
- char start[500],end[500];
- queue<int>q;
- int get_pos(char *city,int size)
- {
- int i;
- for(i=0;i<size;i++)
- if(!strcmp(list[i].label,city))
- return i;
- return -1;
- }
- void print_sequence(int end)
- {
- if(depth[end]==1)
- {
- printf("%s ",list[end].label);
- return;
- }
- print_sequence(list[end].ancestor);
- printf("%s\n%s ",list[end].label,list[end].label);
- }
- void print_shortest_path(int start,int end)
- {
- int temp,temp2,i,level,flag;
- while(!q.empty())
- q.pop();
- q.push(start);
- depth[start]=1;
- for(flag=0;!q.empty();)
- {
- temp=q.front();
- q.pop();
- level=depth[temp];
- for(i=0;i<list[temp].size;i++)
- {
- temp2=list[temp].adjacents[i];
- if(!depth[temp2])
- {
- depth[temp2]=level+1;
- list[temp2].ancestor=temp;
- q.push(temp2);
- if(temp2==end)
- {
- flag=1;
- break;
- }
- }
- }
- if(flag) break;
- }
- if(flag)
- {
- print_sequence(list[end].ancestor);
- printf("%s\n",list[end].label);
- }
- else
- puts("No route");
- }
- int main()
- {
- freopen("a.txt","r",stdin);
- int edges,i,verteces,position1,position2,list_size,s,e;
- char city1[500],city2[500],test;
- for(list_size=0,test=0;scanf("%d",&edges)!=EOF;list_size=0)
- {
- if(test)
- puts("");
- else
- test=1;
- for(i=0;i<edges;i++)
- {
- scanf("%s%s",city1,city2);
- position1=get_pos(city1,list_size);
- if(position1==-1)
- {
- position1=list_size++;
- strcpy(list[position1].label,city1);
- list[position1].size=0;
- }
- position2=get_pos(city2,list_size);
- if(position2==-1)
- {
- position2=list_size++;
- strcpy(list[position2].label,city2);
- list[position2].size=0;
- }
- list[position1].adjacents[list[position1].size]=position2;
- list[position2].adjacents[list[position2].size]=position1;
- list[position2].size++; list[position1].size++;
- }
- for(i=0;i<=verteces;i++)
- depth[i]=0;
- verteces=list_size;
- scanf("%s%s",start,end);
- s=get_pos(start,verteces);
- e=get_pos(end,verteces);
- print_shortest_path(s,e);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment