CCCPaste

Untitled

#include 
#include 
#include 
#include 
#include 


using namespace std;
 
typedef long long ll;
#define pb push_back
#define mp make_pair
#define f first
#define s second
int mat[100005][101];

int main()
{
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
 
    int n, m, k, s;
    cin >> n >> m >> k >> s;
    vector type;
    for(int i = 0; i < n; i++){
        int a;
        cin >> a;
        type.pb(a-1);
    }
    vector graph[n+5];
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        graph[a-1].pb(b-1);
        graph[b-1].pb(a-1);
    }
    
    memset(mat, -1, sizeof mat);
    for(int i = 0; i < k; i++){
        queue q;
        vector visited(n, false);

        for(int j = 0; j < n; j++){
            if(type[j] == i) {
            
            q.push(j);
            q.push(0);
            visited[j] = true;
            
        }
            
    }
        while(!q.empty()){
            int c = q.front(); q.pop();
            int dist = q.front(); q.pop();
            mat[c][i] = dist;
            for(int l = 0; l < (int) graph[c].size(); l++){
                int z = graph[c][l];
                if(visited[z]) continue;
                visited[z] = true;
                q.push(z);
                q.push(dist+1);
            }
        }
    
    }
    
    for(int i = 0; i < n; i++){
        sort(mat[i], mat[i] + k);
    }
    for(int i = 0; i < n; i++){
        int dist = 0;
        int z = 0;
        for(int j = 0; j < k; j++){
            dist += mat[i][j];
            z++;
            if(z == s) break;
        }
        cout << dist << " ";
    }
    
    return 0;
 
}