-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUVA_762.cpp
98 lines (96 loc) · 2.66 KB
/
UVA_762.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
#define sz 10000000
map < string,int > mymap;
map < int,string > mymap2;
map < string,int > mark;
vector <int> adj[sz];
int dis[sz];
int visited[sz];
int parent[sz];
int printpath(int start , int goal ,int index) {
for ( int i = 0 ; i < index ; i++) {
if ( i != start) {
dis[i] = 10000;
parent[i] = -1;
visited[i] = 0 ;
}
}
visited[start] = 1 ;
dis[start] = 0 ;
parent[start] = -1;
queue < int > q ;
q.push(start);
while(!q.empty()) {
int u = q.front();
q.pop();
for ( int i = 0 ; i < adj[u].size() ; i++) {
if ( visited[adj[u][i]] == 0) {
visited[adj[u][i]] = 1 ;
parent[adj[u][i]] = u ;
dis[adj[u][i]] = dis[u]+1;
q.push(adj[u][i]);
}
}
}
vector <int> path ;
int crawl = goal;
path.push_back(crawl);
while (parent[crawl] != -1) {
path.push_back(parent[crawl]);
crawl = parent[crawl];
}
int fact = 0 ;
for ( int i = 0 ; i < path.size() ; i++) {
if ( path[i] == -1 ) fact = 1 ;
}
if ( path.size() <= 1) fact = 1 ;
if ( fact == 1) printf("No route\n");
else {
reverse(path.begin(),path.end());
for ( int i = 0 ; i < path.size()-1 ; i++) {
cout << mymap2[path[i]] << " " << mymap2[path[i+1]] << endl;
}
}
return 0 ;
}
int main() {
int caseno = 0 ;
int n ;
while(scanf("%d ",&n)!= EOF) {
if ( caseno != 0) printf("\n");
string a,b ;
int index = 0 ;
for ( int i = 0 ; i < n ; i++) {
cin >> a >> b ;
mark[a] = 1;
mark[b] = 1;
if ( mymap.find(a) == mymap.end()) {
mymap[a] = index ;
mymap2[index] = a;
index++ ;
}
if ( mymap.find(b) == mymap.end()) {
mymap[b] = index ;
mymap2[index] = b ;
index++ ;
}
adj[mymap[a]].push_back(mymap[b]);
adj[mymap[b]].push_back(mymap[a]);
}
string source,destination;
cin >> source >> destination;
if ( caseno != 0) printf("\n");
if ( mark[source] != 1 || mark[destination] != 1) printf("No route\n");
else printpath(mymap[source],mymap[destination],index);
caseno++;
mymap.clear();
mymap2.clear();
mark.clear();
for ( int i = 0 ; i < sz ; i++) adj[i].clear();
memset(dis,0,sizeof(dis));
memset(visited,0,sizeof(visited));
memset(parent,0,sizeof(parent));
}
return 0 ;
}