codeforces 987D. Fair

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 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 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 3

题意:每个城市有且仅有一种特产,城市之间有路连着,花费均为1。n座城市一共有k种特产

如果一座城市要举办庆典,则这个城市需要S个特产。

现在问如果每一个城市都要举办庆典,则每一个城市举办庆典需要花费最小为多少。

思路:一开始是没思路的,不过看到k和s最大只有一百就觉得可以暴力了。

反过来想,我们每一个特产跑全图,记录dp[i][j]表示特产I到城市J的最小花费。

这样我们队所有特产跑完全图之后排序的s小就是我们的答案了。

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<cmath>
#include<stack>
#include<iomanip>
#include<functional>
#include <iomanip>
#include<bitset>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int maxn = (int)1e5 + 100;
const int BN = 30;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1000000007;
const double eps = 1e-8;
const double PI = acos(-1);
int d[105][maxn];
int goods[maxn];
int dis[105];
vector<int>edge[maxn];
int n, m, k, s;
void bfs(int s) {
	queue<int>que;
	for (int i = 1; i <= n; i++) {
		if (s == goods[i]) {
			que.push(i);
			d[s][i] = 0;
		}
	}
	while (!que.empty()) {
		int u = que.front();
		que.pop();
		for (int i = 0; i < edge[u].size(); i++) {
			int &v = edge[u][i];
			if (d[s][v] == -1) {
				que.push(v);
				d[s][v] = d[s][u] + 1;
			}
		}
	}
}
int main() {
	while (~scanf("%d%d%d%d", &n, &m, &k, &s)) {
		for (int i = 1; i <= n; i++) {
			scanf("%d", &goods[i]);
			edge[i].clear();
		}
		memset(d, -1, sizeof(d));
		while (m--) {
			int u, v;
			scanf("%d%d", &u, &v);
			edge[u].push_back(v);
			edge[v].push_back(u);
		}
		for (int i = 1; i <= k; i++)
			bfs(i);
		for (int i = 1; i <= n; i++) {
			memset(dis, 0, sizeof(dis));
			for (int j = 1; j <= k; j++)
				dis[j] = d[j][i];
			sort(dis + 1, dis + 1 + k);
			LL ans = 0;
			for (int j = 1; j <= s; j++)
				ans += dis[j];
			printf("%I64d", ans);
			printf("%s", i < n ? " " : "\n");
		}
	}
	return 0;
}

3.
现在

猜你喜欢

转载自blog.csdn.net/xiuya19/article/details/80528712