A. Fair+bfs

把各种颜色到每个点的距离算出来
d[j]记录所有颜色到这一个点的距离
然后取前s个


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 n

towns in Byteland and m

two-way roads between towns. Of course, you can reach any town from any other town using roads.

There are k

types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least s different types of goods. It costs d(u,v) coins to bring goods from town u to town v where d(u,v) is the length of the shortest path from u to v

. 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 n

towns.

Input

There are 4

integers n, m, k, s in the first line of input ( 1n105, 0m105, 1skmin(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 n

integers a1,a2,,an ( 1aik), where ai is the type of goods produced in the i-th town. It is guaranteed that all integers between 1 and k occur at least once among integers ai

.

扫描二维码关注公众号,回复: 1388971 查看本文章

In the next m

lines roads are described. Each road is described by two integers u v ( 1u,vn, uv

) — 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 n

numbers, the i-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town i

. 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 1

you can bring goods from towns 1 ( 0 coins), 2 ( 1 coin) and 4 ( 1 coin). Total numbers of coins is 2

.

Town 2

: Goods from towns 2 ( 0), 1 ( 1), 3 ( 1). Sum equals 2

.

Town 3

: Goods from towns 3 ( 0), 2 ( 1), 4 ( 1). Sum equals 2

.

Town 4

: Goods from towns 4 ( 0), 1 ( 1), 5 ( 1). Sum equals 2

.

Town 5

: Goods from towns 5 ( 0), 4 ( 1), 3 ( 2). Sum equals 3

.

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;


#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

#define all(a) (a).begin(),(a).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second



const ll INF=1e18;
const int MOD=1e9+7;
const int N=1e4+10;

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    int n=rd(),m=rd(),k=rd(),s=rd();

    vi a(n);
    rep(i,0,n-1)a[i]=rd()-1;

    vector<vi> g(n);

    rep(i,0,m-1){
        int x=rd()-1,y=rd()-1;
        g[x].pb(y);
        g[y].pb(x);
    }
    vector<vi> dist(k,vector<int>(n,-1));

    rep(type,0,k-1){
        vector<int>que;
        rep(i,0,n-1){
            if(a[i]==type){
                dist[type][i]=0;
                que.pb(i);
            }
        }
        rep(b,0,que.size()-1){
            for(int u:g[que[b]]){
                if(dist[type][u]==-1){
                    dist[type][u]=dist[type][que[b]]+1;
                    que.pb(u);
                }
            }
        }
    }
    vi d(k);

    rep(i,0,n-1){
        rep(j,0,k-1)
        d[j]=dist[j][i];
        sort(all(d));
        ll sum=0;
        rep(j,0,s-1)
        sum+=d[j];
        printf("%d ",sum);
    }
    puts("");

}


猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80504863
今日推荐