CodeForces 986A Fair(BFS)

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 m two-way roads between towns. Of course, you can reach any town from any other town using roads.

There are 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 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 ai.

In the next 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 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 2.

Town 2.

Town 2.

Town 2.

Town 3.

题意:有n个城镇,和m条路,每个城镇生产一种物品(总共有k种),问你将s种不同的物品运到x城镇的最短距离。(1<=x<=n)。

思路: 因为K只有100,所以我们计算对于第k种物品运到所有城镇的最短距离,最后将到x城镇的k种物品从小到大排序,求最小的s种。

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#define ll long long
using namespace std;
const int maxn=401000;
queue<int> q;
int l=0,n,m,s,k,x,y;
int link[maxn],first[maxn],dis[maxn],ne[maxn],f[maxn/4][110],a[maxn],b[110],vis[maxn];
void add(int x,int y)
{
	link[++l]=y;ne[l]=first[x];first[x]=l;
}
void bfs()
{
	while(!q.empty())
	  {
	  	int u=q.front();
	  	q.pop();
	  	for(int i=first[u];i;i=ne[i])
	  	   {
	  	   	int v=link[i];
	  	   	if(dis[v]>dis[u]+1&&vis[v])
	  	   	  {
	  	   	  	dis[v]=dis[u]+1;
	  	   	  	q.push(v);
			  }
		   }
	  }
}
using namespace std;
int main()
{
	scanf("%d%d%d%d",&n,&m,&k,&s);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=m;i++) 
	    {
	    scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
		}
	for(int i=1;i<=k;i++)
	   {
	   	while(!q.empty()) q.pop();
	   	for(int j=1;j<=n;j++)
	   	   if(a[j]==i) dis[j]=0,vis[j]=0,q.push(j);
	   	   else dis[j]=12345678,vis[j]=1;
	   	bfs();
	   	for(int j=1;j<=n;j++) f[j][i]=dis[j]; 
	   } 
	for(int i=1;i<=n;i++)
	   {
	   	for(int j=1;j<=k;j++) b[j]=f[i][j];
	   	sort(b+1,b+1+k);
	   	int ans=0;
	   	for(int j=1;j<=s;j++) ans+=b[j];
	   	if(i!=n)printf("%d ",ans);else printf("%d
",ans);
	   }
}