Codeforces Round #485 (Div. 2) D. Fair BFS

链接:戳这里


D. 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 uuto 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.


题意:

一家公司准备在n个城镇举办展览,每个城镇仅仅生产一种产品,n个城镇加起来有且仅有k个,公司在每个城镇举办展览,且每个城镇至少需要s个产品。u城镇为了收集v城镇的产品,需要走d(u,v)的最短路程,问每个城镇都举办展览的话,每个城镇至少需要走多少路程。

这里需要注意一点,就是假设城镇1收集城镇3的产品,会途径城镇2,那么最短路程的计算需要计算d(1->2) + d(1->3)的,而不是计算d(1->2->3)。


给定n,m,k,s分别表示n个城镇,m条马路,k表示n个城镇生产的产品总工k种,s表示每个城镇举办展览至少需要s个产品。

接下来n个数分别表示每个城镇生成的产品类型

接下来m条边,u,v表示城镇u到城镇v之间有一条路。

思路:

一开始想着dis[i][k],表示城镇i到产品k的最短路,考虑枚举i,然后跑图去找最近的产品k在哪个城市,这样的复杂度是n*n,GG。

中途吃个饭考虑了许久,解法是枚举产品k,BFS去跑图,算出每个城市距离产品k的最短路径,这样跑图的时候因为从城镇产品类型k开始,bfs下去维护dis[i][k]的值就好了。复杂度就是k*m*log(m),貌似这个bfs的复杂度取决于边数,有错的话留言批评我一下谢谢。

那么拿到了每个城镇到产品k的最近距离,排序每个城镇的前s小就好了。

代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
typedef long long LL;
typedef long double ld;
#define INF (1ll<<60)-1
#define Maxn 1e9 + 100
using namespace std;
int n,m,K,S;
int a[100010];
int head[400010],tot = 0;
struct edge{
    int v, next;
}e[400010];
void AddPath(int u, int v){
    e[tot].v = v;
    e[tot].next = head[u];
    head[u] = tot++;
}
LL dis[100010][110];
int vis[100010];
void BFS(int val){
    mst(vis, 0);
    queue< pair<int, int> > qu;
    for(int i = 1; i <= n; i++) {
        if(a[i] == val) {
            qu.push({i, 0});
            vis[i] = 1;
        }
    }
    while(!qu.empty()){
        pair<int, int> pii = qu.front();
        qu.pop();
        int u = pii.first;
        for(int i = head[u]; i != -1; i = e[i].next){
            dis[u][val] = pii.second;
            int v = e[i].v;
            if(v == u || vis[v]) continue;
            vis[v] = 1;
            qu.push({v, pii.second+1});
        }
    }
}
int main(){
    scanf("%d%d%d%d", &n, &m, &K, &S);
    mst(head, -1);
    mst(dis, 0LL);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= m; i++){
        int u,v;
        scanf("%d%d", &u, &v);
        AddPath(u, v);
        AddPath(v, u);
    }
    for(int i = 1; i <= K; i++) BFS(i);
    for(int i = 1; i <= n; i++){
        sort(dis[i]+1,dis[i]+K+1);
        LL ans = 0;
        for(int j = 1; j <= S; j++){
            ans += dis[i][j];
        }
        printf("%lld ", ans);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值