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.
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.
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.
5 5 4 3 1 2 4 3 2 1 2 2 3 3 4 4 1 4 5
2 2 2 2 3
7 6 3 2 1 2 3 3 2 2 1 1 2 2 3 3 4 2 5 5 6 6 7
1 1 1 2 2 1 1
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
分析:这道题比赛的时候测评机卡导致睡觉了结果还没有出来,第二天起来一开超时了,然后分析了一下,原本对一个节点进行bfs,对于每一个节点只要得到满足条件的物品个数然后输出硬币值就可以,但是10万的数据实在太大了,直接爆了,然后分析了一下,发现可以改成对每一个物品进行bfs,这样就可以得到对每一个节点到每一个物品的最短值了,然后只要取前s个小的即可。
AC代码
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 100000+100;
vector<int> vt[maxn];
int d[maxn][105];
int n, m, k, s1;
int a[maxn];
int vis[maxn];
int num[maxn];
struct node{
int v;
int cnt;
};
int bfs(int cur){
memset(vis, -1, sizeof(vis));
queue<int> q;
for(int i=1; i<=n; i++){
if(a[i] == cur){
q.push(i);
vis[i] = 0;
}
}
while(!q.empty()){
int p = q.front();
q.pop();
for(int i=0; i<vt[p].size(); i++){
int temp = vt[p][i];
if(vis[temp] == -1){
vis[temp] = vis[p]+1;
q.push(temp);
}
}
}
for(int i=1; i<=n; i++){
d[i][cur] = vis[i];
}
}
int main(){
memset(d, INF, sizeof(d));
scanf("%d%d%d%d", &n, &m, &k, &s1);
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
d[i][a[i]] = 0;
}
int v, u;
for(int i=1; i<=m; i++){
scanf("%d%d", &u, &v);
vt[u].push_back(v);
vt[v].push_back(u);
}
for(int i=1; i<=k; i++){
bfs(i);
}
for(int i=1; i<=n; i++){
sort(d[i]+1, d[i]+k+1);
int ans = 0;
for(int j=1; j<=s1; j++){
ans += d[i][j];
}
cout<<ans<<" ";
}
}