Advertisement
Josif_tepe

Untitled

Mar 16th, 2022
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5. #include <cstring>
  6. #include <queue>
  7. using namespace std;
  8. typedef long long ll;
  9. vector<int> graph[100005];
  10. int dist[100001][101];
  11. int main() {
  12.     ios_base::sync_with_stdio(false);
  13.     int n, m, k, s;
  14.     cin >> n >> m >> k >> s;
  15.     vector<int> type(n + 1);
  16.     for(int i = 1; i <= n; i++) {
  17.         cin >> type[i];
  18.     }
  19.     for(int i = 0; i < m; i++) {
  20.         int a, b;
  21.         cin >> a >> b;
  22.         graph[a].push_back(b);
  23.         graph[b].push_back(a);
  24.     }
  25.    
  26.     for(int i = 1; i <= k; i++) {
  27.         queue<int> Q;
  28.         vector<bool> visited(n + 1, false);
  29.         for(int j = 1; j <= n; j++) {
  30.             if(type[j] == i) {
  31.                 Q.push(j);
  32.                 Q.push(0);
  33.                 dist[j][i] = 0;
  34.                 visited[j] = true;
  35.             }
  36.         }
  37.         while(!Q.empty()) {
  38.             int node = Q.front();
  39.             Q.pop();
  40.             int cekor = Q.front();
  41.             Q.pop();
  42.             dist[node][i] = cekor;
  43.             for(int j = 0; j < graph[node].size(); j++) {
  44.                 int sosed = graph[node][j];
  45.                 if(!visited[sosed]) {
  46.                     visited[sosed] = true;
  47.                     Q.push(sosed);
  48.                     Q.push(cekor + 1);
  49.                 }
  50.             }
  51.         }
  52.     }
  53.     for(int i = 1; i <= n; i++) {
  54.         vector<int> v;
  55.         for(int j = 1; j <= k; j++) {
  56.             v.push_back(dist[i][j]);
  57.         }
  58.         sort(v.begin(), v.end());
  59.         int sum = 0;
  60.         for(int j = 0; j < s; j++) {
  61.             sum += v[j];
  62.         }
  63.         cout << sum << " ";
  64.     }
  65.     return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement