Fair CodeForces - 987D (bfs,设置超级源点,对每个物品进行暴力求解)

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 ss different 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 uu to 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 nntowns.

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
5 5 4 3
1 2 4 3 2
1 2
2 3
3 4
4 1
4 5
Output
2 2 2 2 3 
Input
7 6 3 2
1 2 3 3 2 2 1
1 2
2 3
3 4
2 5
5 6
6 7
Output
1 1 1 2 2 1 1 

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

思路:刚看到这道题时,总不能对每个节点进行bfs一次吧,找到s个物品;看看n个范围,时间复杂度会是n*n,当你观察k的范围为(1~100),看看能不能让物品去找节点,看看到每个节点的花费,对每个物品进行暴力,最后取 到这个节点花费小的s个物品;这中间就应该设置超级源点了;

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 100010
#include<vector>
#include<queue>
int a[Max];
int n,m,k,s;

vector<int> v[110];       // 超级源点i,v[i]中存当前生产的物品为i的所有结点; 
vector<int> G[Max];       // 存图; 
vector<int> d[Max];      // d[i]中存的是 每一个物品到结点i的花费,最后取前k个1; 
int book[Max];

struct node
{
	int x;
	int step;
};

void init()
{
	int i,j;
	for(i = 0;i<=100;i++)
		v[i].clear();
	for(i = 0;i<=n;i++)
	{
		G[i].clear();
		d[i].clear();
	}
		
}
void bfs(int s)
{
	int i,j;
	memset(book,0,sizeof(book));
	queue<node >q;
	for(i = 0;i<v[s].size();i++)
	{
		int k = v[s][i];
		book[k] = 1;
		node star;
		star.x = k;
		star.step = 0;
		d[star.x].push_back(0);
		q.push(star);
	}
	while(!q.empty())
	{
		node star = q.front();
		q.pop();
		int k = star.x;
		for(i = 0;i<G[k].size();i++)
		{
			int tt = G[k][i];
			if(!book[tt])
			{
				book[tt] = 1;
				d[tt].push_back(star.step + 1);
				q.push(node{tt,star.step+1});
			}
		}
	}
}

int main()
{
	int i,j;
	while(~scanf("%d%d%d%d",&n,&m,&k,&s))
	{
		init();
		for(i = 1;i <= n; i ++)
		{
			scanf("%d",&a[i]);
			v[a[i]].push_back(i);
		}
		int x,y;
		for(i = 1;i <= m;i ++)
		{
			scanf("%d%d",&x,&y);
			G[x].push_back(y);
			G[y].push_back(x);
		}
		for(i = 1;i<=k;i++)
			bfs(i);
		for(i = 1;i<=n;i++)
		{
			sort(d[i].begin(),d[i].begin()+k);
			int sum = 0;
			for(j = 0;j<d[i].size();j++)
			{
				if(j==s) break;
				sum += d[i][j];
			}
			if(i!=1) printf(" "); 
			printf("%d",sum);
		}
		printf("\n");
	}
	return 0;
} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值