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