Fair CodeForces - 987D (一个巧妙的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个城市,m条路,保证任意城市都相通,保证任意两个城市之间都只有1条路径。现在,要在某一个城市举办一场盛会,每个城市都会生产1种商品(不同城市之间生产的商品可能相同)共有k种不同的商品,现在,举办盛会需要s种不同的商品。每种商品都需要走到相应的城市去取。分别输出在n个城市举办盛会需要走的路(路径以单位路径来算例如,如果1----2----3,那么,1到3要走的路为2)

思路:bfs,但是如果枚举所有城市的话,那么一定会超时。所以,我们选择一次性将所有生产i型商品的城市加入队列,一次bfs,将所有到其他城市的最短路算出来!(之前我在写的时候,一次性加入所有i型商品的城市用for循环处理,t了,最后参考Hipo_3412dalao的代码才想到,借用vector即可在很小的时间复杂的以内加入所有的城市,拜谢大佬,这里就直接copy   dalao的代码了,如果侵权,请大佬马上联系我删除博客!!!)

//我们直接将所有生产i型商品的城市一次性全加入队列,然后bfs

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include "algorithm"
using namespace std;
const int MAX=1e6+5;
int n,m,k,s;
int good[MAX],mov[MAX][105];//mov[i][j]记录以生产j商品的城市为举办地,到i城市的最小花费
vector<int> E[MAX];
void bfs(int x)
{
    queue<int> Q;
    int i,u,v;
    Q.push(x+n);
    while(!Q.empty()){
        u=Q.front();
        Q.pop();
        for(i=0;i<E[u].size();i++){
            v=E[u][i];
            if(mov[v][x]==0){
                mov[v][x]=mov[u][x]+1;
                Q.push(v);
            }
        }
    }
}
int main()
{
    int i,j;
    int u,v,cnt;
    scanf("%d%d%d%d",&n,&m,&k,&s);
    for(i=1;i<=n;i++){
        scanf("%d",&good[i]);
        E[good[i]+n].push_back(i);//如果我们直接用一个for循环将生产good[i]型商品的城市加入队列的话,就会超时,所以,我们借用这个vector来处理!
    }
    for(i=0;i<m;i++){
        scanf("%d%d",&u,&v);
        E[u].push_back(v);
        E[v].push_back(u);
    }
    for(i=1;i<=k;i++) bfs(i);
    for(i=1;i<=n;i++){
        cnt=0;
        sort(mov[i]+1,mov[i]+k+1);
        for(j=1;j<=s;j++)
            cnt+=mov[i][j]-1;
        printf("%d ",cnt);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80517147
今日推荐