CodeForces - 986A Fair (BFS)

本文介绍了一种算法,用于解决在一个包含多个小镇的区域中,如何找到举办爬梯的最优地点,以确保所需多种特产的最低运输成本。通过运行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 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 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 towns.

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
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 11 you can bring goods from towns 11 (00 coins), 22 (11 coin) and 44 (11 coin). Total numbers of coins is 22.

Town 22: Goods from towns 22 (00), 11 (11), 33 (11). Sum equals 22.

Town 33: Goods from towns 33 (00), 22 (11), 44 (11). Sum equals 22.

Town 44: Goods from towns 44 (00), 11 (11), 55 (11). Sum equals 22.

Town 55: Goods from towns 55 (00), 44 (11), 33 (22). Sum equals 33.


一、原题地址

    点击打开链接


二、大致题意

    现在有n个小镇,m条双向边。在这些小镇里一共有k种不同的特产,现在人们想在其中的一个小镇上举行趴体,开爬梯需要有s种特产才行,人们需要从别的小镇带来不一样的特产才能保证爬梯顺利举行。已知每次走到相邻的小镇所需的代价为1,每个小镇有自己特有的特产编号。现在想要知道在哪个小镇举办爬梯的代价最小,所以你需要输出在每个小镇举行爬梯的最小代价,方便人们来选择。


三、大致思路

    

    如果选择从每一个小镇跑一遍最短路,想法是没错的,但是对于这道题目的数据范围来讲,n是1e5的范围导致必定是超时的。

所以从特产入手,我们可以设置一个dis[ i ][ type ]表示第 i 个小镇购买到type这种特产的最小代价。这样跑 k 个BFS,我们是可以得到所有小镇买对应特产的最小情况,此时,只需要取这些情况中前 s 个比较小的就是答案了。


四、代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf = 0x3f3f3f3f;
#define LL long long int 
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }

const int maxn = 100005;
int n, m, k, s;
int dis[maxn][105];
int type[maxn];
bool vis[maxn];
vector<int>e[maxn];
void read()
{
	scanf("%d %d %d %d", &n, &m, &k, &s);
	for (int i = 1; i <= n; i++)
		scanf("%d", &type[i]);
	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);
		e[u].push_back(v);
		e[v].push_back(u);
	}
}
void BFS(int typ) 
{
	queue<int>q;
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; i++)
	{
		if (type[i] == typ)
		{
			q.push(i);
			vis[i] = true;
			dis[i][typ] = 0;
		}
	}
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		int size = e[t].size();
		for (int i = 0; i < size; i++)
		{
			int to = e[t][i];
			if (!vis[to]) 
			{
				dis[to][typ] = dis[t][typ] + 1;
				q.push(to);
				vis[to] = true;
			}
		}
	}
}
void solve()
{
	read();
	memset(dis, 0, sizeof(dis));
	for (int i = 1; i <= k; i++)
	{
		BFS(i);
	}
	for (int i = 1; i <= n; i++)
	{
		sort(dis[i] + 1, dis[i] + 1 + k);
		int ans = 0;
		for (int j = 1; j <= s; j++)
			ans += dis[i][j];
		printf("%d ", ans);
	}
}
int main()
{
	solve();
	getchar();
	getchar();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值