Description
The Net
The Net |
Taking into account the present interest in the Internet, a smart information routing becomes a must. This job is done by routers situated in the nodes of the network. Each router has its own list of routers which are visible for him (so called ``routing table"). It is obvious that the information should be directed in the way which minimizes number of routers it has to pass (so called ``hop count").
Your task is to find an optimal route (minimal hop count) for the given network form the source of the information to its destination.
Input
First line contains number of routers in the network ( n). Next n lines contain description of the network. Each line contains router ID, followed by a hyphen and comma separated list of IDs of visible routers. The list is sorted in ascending order. Next line contains a number of routes ( m) you should determine. The consecutive m lines contain starting and ending routers for the route separated by a single space.Input data may contain descriptions of many networks.
Output
For each network you should output a line with 5 hyphens and then, for each route, a list of routers passed by information sent from starting to destination routers.In case passing of information is impossible (no connection exists) you should output a string ``connection impossible". In case of multiple routes with the same `hop count' the one with lower IDs should be outputted (in case of route form router 1 to 2 as 1 3 2 and 1 4 2 the 1 3 2 should be outputted).
Assumptions: A number of routers is not greater than 300 and there are at least 2 routers in the network. Each routers ``sees" no more than 50 routers.
Sample Input
6 1-2,3,4 2-1,3 3-1,2,5,6 4-1,5 5-3,4,6 6-3,5 6 1 6 1 5 2 4 2 5 3 6 2 1 10 1-2 2- 3-4 4-8 5-1 6-2 7-3,9 8-10 9-5,6,7 10-8 3 9 10 5 9 9 2
Sample Output
----- 1 3 6 1 3 5 2 1 4 2 3 5 3 6 2 1 ----- 9 7 3 4 8 10 connection impossible 9 6 2
#define DeBUG #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <string> #include <set> #include <sstream> #include <map> #include <list> #include <bitset> using namespace std ; #define zero {0} #define INF 0x3f3f3f3f #define EPS 1e-6 typedef long long LL; const double PI = acos(-1.0); //#pragma comment(linker, "/STACK:102400000,102400000") inline int sgn(double x) { return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1); } #define N 1005 int mp[N][N]; int pre[N][N]; int main() { #ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin); #endif int n, m; while (scanf("%d", &n) + 1) { memset(mp, INF, sizeof(mp)); memset(pre, -1, sizeof(pre)); for (int i = 1; i <= n; i++)//初始化记录路径数组 { for (int j = 1; j <= n; j++) { pre[i][j] = j; } } int a, b; char s[1000]; char c; int num; for (int i = 1; i <= n; i++) { stringstream in; scanf("%s", s); in << s; in >> a; in >> c; while (in >> num) { mp[a][num] = 1; in >> c; } } int cost; for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cost = mp[i][k] + mp[k][j]; if (cost < mp[i][j]) { mp[i][j] = cost; pre[i][j] = pre[i][k]; } else if (cost == mp[i][j])//注意字典序最小 { if (pre[i][j] > pre[i][k]) pre[i][j] = pre[i][k]; } } } } scanf("%d", &m); printf("-----\n"); while (m--) { scanf("%d%d", &a, &b); int k = a; if (mp[a][b] == INF) { printf("connection impossible\n"); continue; } printf("%d", a); while (k != b) { printf(" %d", pre[k][b]); k = pre[k][b]; } printf("\n"); } } return 0; }