codeforces 986A Fair

http://www.elijahqi.win/archives/3546
Some company is going to hold a fair in Byteland. There are n

n
towns in Byteland and m

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

There are k

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

s
different types of goods. It costs d(u,v)

d(u,v)
coins to bring goods from town u

u
to town v

v
where d(u,v)

d(u,v)
is the length of the shortest path from u

u
to v

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

n
towns.

Input
There are 4

4
integers n

n
, m

m
, k

k
, s

s
in the first line of input (1≤n≤105

1≤n≤105
, 0≤m≤105

0≤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 n

n
integers a1,a2,…,an

a1,a2,…,an
(1≤ai≤k

1≤ai≤k
), where ai

ai
is the type of goods produced in the i

i
-th town. It is guaranteed that all integers between 1

1
and k

k
occur at least once among integers ai

ai
.

In the next m

m
lines roads are described. Each road is described by two integers u

u
v

v
(1≤u,v≤n

1≤u,v≤n
, u≠v

u≠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 n

n
numbers, the i

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

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

1
you can bring goods from towns 1

1
(0

0
coins), 2

2
(1

1
coin) and 4

4
(1

1
coin). Total numbers of coins is 2

2
.

Town 2

2
Goods from towns 2

2
(0

0
), 1

1
(1

1
), 3

3
(1

1
). Sum equals 2

2
.

Town 3

3
Goods from towns 3

3
(0

0
), 2

2
(1

1
), 4

4
(1

1
). Sum equals 2

2
.

Town 4

4
Goods from towns 4

4
(0

0
), 1

1
(1

1
), 5

5
(1

1
). Sum equals 2

2
.

Town 5

5
Goods from towns 5

5
(0

0
), 4

4
(1

1
), 3

3
(2

2
). Sum equals 3

3
.

枚举 每种商品 然后跑多源点最短路 更新看到 每个点的最短距离即可 并且在每个点上维护一个优先队列 大小为s即可 最后统计一下即可

#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
#define fi first
#define se second
#define pa pair<int,int>
#define mp(x,y) make_pair(x,y)
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if(T==S){T=(S=now)+fread(now,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int N=1e5+10;
const int inf=0x3f3f3f3f;
struct node{
    int y,next;
}data[N<<1];
vector<int> a[N];
bool mark[N],flag[N];
int n,m,k,s,num,h[N],dis[N];
priority_queue<int> Q[N];
inline void spfa(int id){queue<int>q;
    memset(flag,0,sizeof(flag));memset(dis,0x3f,sizeof(dis));
    for (int i=0;i<a[id].size();++i) flag[a[id][i]]=1,dis[a[id][i]]=0,q.push(a[id][i]);
    while(!q.empty()){
        int x=q.front();q.pop();
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y;
            if (dis[x]+1<dis[y]){
                dis[y]=dis[x]+1;
                if(!flag[y]) flag[y]=1,q.push(y);
            }
        }
    }
    for (int i=1;i<=n;++i) if (dis[i]<Q[i].top()) Q[i].pop(),Q[i].push(dis[i]);
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();m=read();k=read();s=read();
    for (int i=1;i<=n;++i) {
        int x=read();a[x].push_back(i);
        for (int j=1;j<=s;++j) Q[i].push(inf);mark[x]=1;
    }
    for (int i=1;i<=m;++i){
        int x=read(),y=read();
        data[++num].y=y;data[num].next=h[x];h[x]=num;
        data[++num].y=x;data[num].next=h[y];h[y]=num;
    }
    for (int i=1;i<=n;++i){
        if (!mark[i]) continue;spfa(i);
    }
    for (int i=1;i<=n;++i){
        int ans=0;
        while(!Q[i].empty()) ans+=Q[i].top(),Q[i].pop();
        printf("%d\n",ans);
    }
    for (int i=1;i<=5;++i) Q[1].push(i);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值