Fair
Some company is going to hold a fair in Byteland. There are nn towns in Byteland and mm two-way roads between towns. Of course, you can reach any town from any other town using roads.
There are kk types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least ss different types of goods. It costs d(u,v)d(u,v) coins to bring goods from town uu to town vv where d(u,v)d(u,v) is the length of the shortest path from uu to vv . Length of a path is the number of roads in this path.
The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of nn towns.
There are 44 integers nn , mm , kk , ss in the first line of input (1≤n≤1051≤n≤105 , 0≤m≤1050≤m≤105 , 1≤s≤k≤min(n,100)1≤s≤k≤min(n,100) ) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.
In the next line there are nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤ai≤k ), where aiai is the type of goods produced in the ii -th town. It is guaranteed that all integers between 11 and kk occur at least once among integers aiai .
In the next mm lines roads are described. Each road is described by two integers uu vv (1≤u,v≤n1≤u,v≤n , u≠vu≠v ) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.
Print nn numbers, the ii -th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town ii . Separate numbers with spaces.
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
2 2 2 2 3
#pragma warning(disable:4996) #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define mem(arr, in) memset(arr, in, sizeof(arr)) using namespace std; const int maxn = 100205; const int INF = 1e9 + 7; int n, m, k, s; int use[maxn], d[maxn][105], co[maxn]; vector<int> v[maxn]; void minroad(int st) { queue<int> q; q.push(st + n); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < v[u].size(); i++) { int to = v[u][i]; if (!d[to][st]) { d[to][st] = d[u][st] + 1; q.push(to); } } } } int main() { while (scanf("%d %d %d %d", &n, &m, &k, &s) != EOF) { for (int i = 1; i <= n; i++) scanf("%d", co + i), v[co[i] + n].push_back(i); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= k; i++) minroad(i); for (int i = 1; i <= n; i++) { sort(d[i] + 1, d[i] + k + 1); int ans = 0; for (int j = 1; j <= s; j++) ans += d[i][j]; printf("%d ", ans - s); } printf("\n"); } return 0; }