Codeforces Round #485 (Div. 1) 题解

A. Fair
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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.

Input

There are 44 integers nnmmkkss in the first line of input (1n1051≤n≤1050m1050≤m≤1051skmin(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 (1aik1≤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 (1u,vn1≤u,v≤nuvu≠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
Copy
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
output
Copy
2 2 2 2 3 
input
Copy
7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
output
Copy
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


【分析】由于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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值