- #include <bits/stdc++.h>
- #define mem(n) memset(n, 0, sizeof(n))
- #define memm(n) memset(n, -1, sizeof(n))
- using namespace std;
- vector <int> net[303];
- int vis[303], path[303];
- void print_path(int v, int u){
- int p = path[v];
- if(p != u)print_path(p, u);
- cout << p << " ";
- }
- void BFS(int src, int dest){
- queue <int> Q;
- Q.push(src);
- vis[src] = 1;
- while(!Q.empty()){
- int u = Q.front();
- Q.pop();
- int sz = net[u].size();
- for(int i = 0; i < sz; i++){
- int v = net[u][i];
- if(!vis[v]){
- vis[v] = 1;
- Q.push(v);
- path[v] = u;
- }
- }
- }
- if(path[dest] == -1)puts("connection impossible");
- else print_path(dest, src), cout << dest << endl;
- }
- int main(){
- int test, num, ind, x, y;
- int n, m, k, start, endd;
- string routes;
- while(cin >> n){
- for(int i = 1; i <= n; i++){
- cin >> routes; num = 0;
- int sz = routes.size();
- for(int j = 0; j < sz; j++){
- if(routes[j] == '-')ind = num, num = 0;
- else if(routes[j] == ',')net[ind].push_back(num), num = 0;
- else num = (num*10) + (routes[j]-'0');
- }
- if(num)net[ind].push_back(num);
- }
- cin >> m;
- puts("-----");
- for(int i = 1; i <= m; i++){
- cin >> start >> endd;
- memm(path), mem(vis);
- BFS(start, endd);
- }
- for(int i = 1; i <= n; i++)net[i].clear();
- }
- return 0;
- }
Friday, April 24, 2015
UVa - 627 - The Net
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment