Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <cstring>
- #include <queue>
- using namespace std;
- typedef long long ll;
- vector<int> graph[100005];
- int dist[100001][101];
- int main() {
- ios_base::sync_with_stdio(false);
- int n, m, k, s;
- cin >> n >> m >> k >> s;
- vector<int> type(n + 1);
- for(int i = 1; i <= n; i++) {
- cin >> type[i];
- }
- for(int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- for(int i = 1; i <= k; i++) {
- queue<int> Q;
- vector<bool> visited(n + 1, false);
- for(int j = 1; j <= n; j++) {
- if(type[j] == i) {
- Q.push(j);
- Q.push(0);
- dist[j][i] = 0;
- visited[j] = true;
- }
- }
- while(!Q.empty()) {
- int node = Q.front();
- Q.pop();
- int cekor = Q.front();
- Q.pop();
- dist[node][i] = cekor;
- for(int j = 0; j < graph[node].size(); j++) {
- int sosed = graph[node][j];
- if(!visited[sosed]) {
- visited[sosed] = true;
- Q.push(sosed);
- Q.push(cekor + 1);
- }
- }
- }
- }
- for(int i = 1; i <= n; i++) {
- vector<int> v;
- for(int j = 1; j <= k; j++) {
- v.push_back(dist[i][j]);
- }
- sort(v.begin(), v.end());
- int sum = 0;
- for(int j = 0; j < s; j++) {
- sum += v[j];
- }
- cout << sum << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement