#include #include #include char names[20][100]; int recv[30]; int active[50][20]; int hosts[50]; int group[20]; int tunnel[20][20]; int attachedto[50]; int packet[50][1000][2]; int lastrouter, lasthost, lastgroup; int findrouter(char *s) { int i; for (i = 0; i < 20; i++) if (!strcmp(names[i], s)) return i; strcpy(names[lastrouter++], s); return lastrouter - 1; } int findhost(int n) { int i; for (i = 0; i < 50; i++) if (n == hosts[i]) return i; hosts[lasthost++] = n; return lasthost - 1; } int findgroup(int n) { int i; for (i = 0; i < 50; i++) if (n == group[i]) return i; group[lastgroup++] = n; return lastgroup - 1; } int findpacket(int n, int m) { int i; for (i = 0; packet[n][i][0]; i++) if (m == packet[n][i][0]) return i; packet[n][i][0] = m; packet[n][i][1] = -1; return i; } int cmp(const void *a, const void *b) { return (((int *)a)[0]) - (((int *)b)[0]); } void sendto(int rout, int grp, int pack, int TTL) { int i, j; if (TTL < 0) return; recv[rout]=1; for (i=0; i= tunnel[rout][i])) { sendto(i, grp, pack, TTL - tunnel[rout][i]); } } recv[rout]=0; } int main() { FILE *fp = fopen("mbone.in", "r"); int i = 0, j, k, n, m, q, cost, r, s, t, u; char buf[256], buf2[256]; for(;;) { i++; lastrouter = lasthost = lastgroup = 0; memset(&names[0][0], 0, 20 * 100 * sizeof(char)); memset(&recv[0], 0, 30 * sizeof(int)); memset(&active[0][0], 0, 50 * 20 * sizeof(int)); memset(&hosts[0], 0, 50 * sizeof(int)); memset(&group[0], 0, 20 * sizeof(int)); memset(&tunnel[0][0], 0, 20 * 20 * sizeof(int)); memset(&attachedto[0], 0, 50 * sizeof(int)); memset(&packet[0][0][0], 0, 50 * 1000 * 2 * sizeof(int)); fscanf(fp, "%d\n", &m); if (!m) break; for (j = 0; j < m; j++) { fscanf(fp, "%s %d\n", buf, &n); q = findrouter(buf); for (k = 0; k < n; k++) { fgets(buf, 256, fp); if (buf[0] == 'T') { sscanf(buf + 2, "%d %s", &cost, buf2); tunnel[q][findrouter(buf2)] = cost; } else { sscanf(buf + 2, "%d", &cost); attachedto[findhost(cost)] = q; } } } fscanf(fp, "%d\n", &n); for (j = 0; j < n; j++) { fgets(buf, 256, fp); switch (buf[0]) { case 'J': sscanf(buf + 2, "%d %d", &r, &s); active[findhost(r)][findgroup(s)] = 1; break; case 'L': sscanf(buf + 2, "%d %d", &r, &s); active[findhost(r)][findgroup(s)] = 0; break; case 'S': sscanf(buf + 2, "%d %d %d %d", &r, &s, &t, &u); sendto(attachedto[findhost(r)],findgroup(s),t,u); break; } } printf("Network #%d\n", i); s = hosts[0]; u = 0; t = hosts[0]; for (k = 0; k < lasthost; k++) { if (hosts[k] < s) { u = k; s = hosts[k]; } if (hosts[k] > t) t = hosts[k]; } for (;;) { qsort(&packet[u][0][0], 1000, 2*sizeof(int), cmp); for (k = 0; (k < 1000) && !packet[u][k][0]; k++); for (; (k < 1000) && packet[u][k][0]; k++) printf("%d %d %d\n", hosts[u], packet[u][k][0], packet[u][k][1]); if (s == t) break; r = s; s = t+1; for (k = 0; k < lasthost; k++) { if (hosts[k] <= r) continue; if (hosts[k] < s) { s = hosts[u = k]; } } } printf("\n"); } return 0; }