4eyes4u

Untitled

Mar 30th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N=1010;
  5.  
  6. vector<int> g[11][N];
  7. int n, m, t, source, dist[N];
  8.  
  9. void bfs (int start, int x)
  10. {
  11.     queue<int> q;
  12.     q.push(start);
  13.     while (!q.empty())
  14.     {
  15.         int xt=q.front();
  16.         q.pop();
  17.         for (int i=0;i<g[x][xt].size();i++)
  18.         {
  19.             int u=g[x][xt][i];
  20.             if (u!=start && !dist[u])
  21.             {
  22.                 dist[u]=dist[xt]+1;
  23.                 q.push(u);
  24.             }
  25.         }
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31.     cin >> t;
  32.     while (t--)
  33.     {
  34.         scanf ("%d%d", &n, &m);
  35.         for (int i=0;i<m;i++)
  36.         {
  37.             int a, b;
  38.             scanf ("%d%d", &a, &b);
  39.             g[t][a].push_back(b);
  40.             g[t][b].push_back(a);
  41.         }
  42.         scanf ("%d", &source);
  43.         bfs (source, t);
  44.         for (int i=1;i<=n;i++)
  45.         {
  46.             if (i==source) continue;
  47.             if (dist[i]) printf ("%d ", dist[i]*6);
  48.             else printf ("-1 ");
  49.         }
  50.         printf ("\n");
  51.         memset (dist, 0, sizeof dist);
  52.     }
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment