Advertisement
cmiN

retea

Mar 9th, 2011
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. const int n = 101, inf = 0x3fFF;
  4. int noduri, mat[n][n], cost[n], viz[n];
  5.  
  6. void init()
  7. {
  8.     for (int i = 1; i <= noduri; i++) {
  9.         cost[i] = inf;
  10.     }
  11. }
  12.  
  13. void process()
  14. {
  15.     int coada[n], le, ri, i, nod;
  16.     cost[1] = 0;
  17.     le = ri = 1;
  18.     coada[le] = 1;
  19.     viz[1] = 1;
  20.     while (le <= ri) {
  21.         nod = coada[le++];
  22.         for (i = 1; i <= noduri; i++) {
  23.             if (mat[nod][i]) {
  24.                 if (cost[nod] + 1 < cost[i]) {
  25.                     cost[i] = cost[nod] + 1;
  26.                 }
  27.                 if (!viz[i]) {
  28.                     viz[i] = 1;
  29.                     coada[++ri] = i;
  30.                 }
  31.             }
  32.         }
  33.     }
  34. }
  35.  
  36. void write()
  37. {
  38.     for (int i = 2; i <= noduri; i++) {
  39.         printf("%d ", cost[i]);
  40.     }
  41. }
  42.  
  43. int main()
  44. {
  45.     int muchii, x, y;
  46.     freopen("retea.in", "rt", stdin);
  47.     freopen("retea.out", "wt", stdout);
  48.     scanf("%d %d", &noduri, &muchii);
  49.     while (muchii-- > 0) {
  50.         scanf("%d %d", &x, &y);
  51.         mat[x][y] = mat[y][x] = 1;
  52.     }
  53.     init();
  54.     process();
  55.     write();
  56.     fclose(stdin);
  57.     fclose(stdout);
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement