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
| #include <stdio.h> struct note { int x; int s; }; int main(){ int i, j, n, m, a,b,start, end,tail,head,flag=0; int e[51][51] = { 0 }; int book[51] = { 0 }; struct note que[2501]; scanf_s("%d %d %d %d", &n, &m, &start, &end); for (i = 1; i <= m; i++) { scanf_s("%d %d", &a, &b); e[a][b] = 1; e[b][a] = 1; } tail = 1; head = 1; que[tail].x = start; que[tail].s = 0; book[start] = 1; tail++; while (head < tail) { int cur; cur = que[head].x; for (i = 1; i <= n; i++) { if (e[cur][i] == 1 && book[i] == 0) { book[i] = 1; que[tail].x = i; que[tail].s = que[head].s + 1; tail++; } if (que[tail].x == end) { flag = 1; break; } } if (flag == 1) break; head++; } printf("%d", que[tail-1].s); getchar(); getchar(); return 0; }
|