0
点赞
收藏
分享

微信扫一扫

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

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

Input


There are 44 integers nn, mm, kk, ss in the first line of input (1≤n≤1051≤n≤105, 0≤m≤1050≤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 nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤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 (1≤u,v≤n1≤u,v≤n, u≠vu≠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


Copy


5 5 4 3 1 2 4 3 2 1 2 2 3 3 4 4 1 4 5


Copy


2 2 2 2 3


Copy


7 6 3 2 1 2 3 3 2 2 1 1 2 2 3 3 4 2 5 5 6 6 7


Copy


1 1 1 2 2 1 1


题目大概:

给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产)。有k种不同特产。

要求每个城镇需要其他城镇运输特产到自己的城镇,每个城镇必须拥有s种特产,那么在城镇满足s种特产后,需要的最短路径是多长,最短路指的是特产运输过来走过的边的数量。

思路:

一读完题,最暴力的思想是,对每个点进行一次bfs,算出每个点的最短路,但是数据是1e5,n2,就是1e10了。明显超时。

那么发现特产最多100种,可以计算每种特产到每个城镇的最短路,lis【i】【j】计算出所有j特产到所有i的最短距离,这样的话是1e7,还可以勉强接受。

然后把所有到i城镇的特产的最短路 排序,取前s个就是i点的最短路径了。

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;
int e[maxn];
int vis[maxn];
int lis[maxn][120];
vector<int>F[120];
vector<int>G[maxn];
struct poin
{
int x,d;
};
int ans=0;
int s;
void bfs(int x)
{
queue<poin>Q;
for(int i=0;i<F[x].size();i++)
{
int v=F[x][i];
poin q;
q.x=v;q.d=0;
Q.push(q);
}
while(!Q.empty())
{
poin u=Q.front();Q.pop();
lis[u.x][x]=min(u.d,lis[u.x][x]);
for(int i=0;i<G[u.x].size();i++)
{
int v=G[u.x][i];
if(vis[v])continue;
vis[v]=1;
poin q;
q.x=v;
q.d=u.d+1;
Q.push(q);
}
}
}
int main()
{
int n,m,k;
scanf("%d%d%d%d",&n,&m,&k,&s);
for(int i=1;i<=n;i++)
{
scanf("%d",&e[i]);
F[e[i]].push_back(i);
}
int u,v;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
memset(lis,0x3f3f3f3f,sizeof(lis));
for(int i=1;i<=k;i++)
{
memset(vis,0,sizeof(vis));
bfs(i);
}

for(int i=1;i<=n;i++)
{
sort(lis[i]+1,lis[i]+k+1);
long long sum=0;
for(int j=1;j<=s;j++)
{
sum+=lis[i][j];
}
printf("%I64d ",sum);
}

return 0;
}


举报

相关推荐

0 条评论