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 ssdifferent 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
7 6 3 2 1 2 3 3 2 2 1 1 2 2 3 3 4 2 5 5 6 6 7
1 1 1 2 2 1 1
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
【分析】由于1<=s<=k<=min(100, n),且每条路径长度为1,不难想到以每个点作为起点做一次bfs,当访问种类达到s时退出bfs,
写的时候是想着因为k和s很小,每次bfs进队数量不会很多,至于初始化,由于进队数量不多,用一个数组记录修改的元
素,结束后再改回来,如果直接memset由于数组较大,会超时。 写完后想了下,发现当图是一条链的时候,前面元素全
都一样,最后几个不一样,是可以卡掉的,但是都A了,就懒得改了。
【代码】 写得丑,随便看
#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
int n, m, k, s;
int head[110000], ne[220000], y[220000], tot, ans;
int a[110000], dis[110000], f[11000], fb;
bool b[110000], flag[200];
int h[110000], he, ta;
void add(int u,int v)
{
ne[++tot] = head[u];
head[u] = tot;
y[tot] = v;
}
void bfs(int x)
{
he = ta = 1;
int cnt = 1, u, p;
h[1] = x;
b[x] = 1;
flag[a[x]] = 1;
f[++fb] = x;
while (ta >= he)
{
u = h[he++];
p = head[u];
while (p)
{
if (cnt >= s) return;
if (dis[y[p]] == 0) dis[y[p]] = dis[u] + 1;
if (!b[y[p]]) h[++ta] = y[p], b[y[p]] = 1, f[++fb] = y[p];
if (!flag[a[y[p]]]) ++cnt, flag[a[y[p]]] = 1, ans += dis[y[p]];
p = ne[p];
}
}
}
int main()
{
int i, j, u, v;
scanf("%d%d%d%d", &n, &m, &k, &s);
for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (i = 1; i <= m; ++i)
{
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
for (i = 1; i <= n; ++i)
{
ans = 0;
fb = 0;
bfs(i);
if (i != n) printf("%d ", ans);
else printf("%d", ans);
for (j = 1; j <= fb; ++j) dis[f[j]] = 0, flag[a[f[j]]] = 0, b[f[j]] = 0;
}
return 0;
}