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 nntowns.
Input
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.
Output
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.
Examples
Input
5 5 4 3 1 2 4 3 2 1 2 2 3 3 4 4 1 4 5
Output
2 2 2 2 3
Input
7 6 3 2 1 2 3 3 2 2 1 1 2 2 3 3 4 2 5 5 6 6 7
Output
1 1 1 2 2 1 1
Note
Let's look at the first sample.
To hold a fair in town 11 you can bring goods from towns 11 (00 coins), 22 (11 coin) and 44 (11 coin). Total numbers of coins is 22.
Town 22: Goods from towns 22 (00), 11 (11), 33 (11). Sum equals 22.
Town 33: Goods from towns 33 (00), 22 (11), 44 (11). Sum equals 22.
Town 44: Goods from towns 44 (00), 11 (11), 55 (11). Sum equals 22.
Town 55: Goods from towns 55 (00), 44 (11), 33 (22). Sum equals 33.
题意:有n个城市m条边,每个城市都有各自的特产(允许多个城市共有一种特产),总共有k种特产。现在要在每个城市都举办盛会,每次盛会需要 s 种特产,这些特产需要从其他城市运过来(共m条边,每条边长度为1)问每个点要举办一场盛会的最小代价是多少?
思路:n <= 1e5 所以不能多次bfs,再看 k <= 100 。所以我们可以考虑每次以每种特产为起点,搜一下它到各个城市的最短距离。然后对每个城市,我们直接对这个城市拥有各种特产的代价进行排序,只取前s个就好了。ps:由于n过大,不能直接用邻接矩阵,所以可以考虑用邻接表或者vector储存图。
代码如下:
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,k,s,dis[100110][100];
vector<int>v[100110];
void bfs(int x)
{
queue<int>Q;
Q.push(x+n); //第x种特产
while(!Q.empty())
{
int p=Q.front();
Q.pop();
for(int i=0; i<v[p].size(); i++)//遍历与p相连的所有城市
{
int u=v[p][i]; //与p相连的城市
if(dis[u][x]==0) //x还没有到达过这个城市
{
dis[u][x]=dis[p][x]+1; //从这个城市得到x特产的最小代价
Q.push(u);//入队列
}
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&s);
for(int i=1; i<=n; i++)
{
int x;
scanf("%d",&x);
v[x+n].push_back(i); //第x种特产在i城市,可以理解为:x+n与i相连
}
for(int i=1; i<=m; i++)
{
int x,y;
scanf("%d%d",&x,&y); //无向图
v[x].push_back(y);
v[y].push_back(x);
}
for(int i=1; i<=k; i++) //遍历每种特产到达所有城市的最短距离
bfs(i);
for(int i=1; i<=n; i++)
{
sort(dis[i]+1,dis[i]+k+1);//从小到大排序,切记是从1开始计算的
int sum=0;
for(int j=1; j<=s; j++)
sum+=dis[i][j]-1;
if(i<n)printf("%d ",sum);
else printf("%d\n",sum);
}
}