寒假特训——I - Fair

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

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 31 2 4 3 21 22 33 44 14 5

Output

2 2 2 2 3 

Input

7 6 3 21 2 3 3 2 2 11 22 33 42 55 66 7

Output

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 .

Fair
思路:
大体思路就是先储存路线和生产地,再就是用bfs找到每一个产品生产地到其他城市最短距离,最后sort进行排序,
求出前s个得和即可。

bfs
第一次进入while,目的是找到生产x产品所有的生产地,第二次,是对每一个生产地进行往后延生,就是找到与这个地方相邻得
地方,距离再加1.

这个最短路bfs的思路要好好整理,以后还可以用到的

就是用bfs,对它附近的城市距离加+1,这个vector的处理要注意学习!!!!!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e5+1000;
vector<int>exa[maxn];
int mov[maxn][110],n;

void bfs(int x)
{
    queue<int>que;
    int i,u,v;
    que.push(x+n);
    while(!que.empty())
    {
        u=que.front();que.pop();
        for(i=0;i<exa[u].size();i++)
        {
            v=exa[u][i];
            if(mov[v][x]==0)
            {
                mov[v][x]=mov[u][x]+1;
                que.push(v);
            }
        }
    }
}

int main()
{
    int m,k,s,a,u,v;
    scanf("%d %d %d %d",&n,&m,&k,&s);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        exa[a+n].push_back(i);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        exa[u].push_back(v);
        exa[v].push_back(u);
    }
    for(int i=1;i<=k;i++) bfs(i);
    for(int i=1;i<=n;i++) sort(mov[i]+1,mov[i]+k+1);
    for(int i=1;i<=n;i++)
    {
        int cnt=0;
        for(int j=1;j<=s;j++) cnt+=mov[i][j]-1;
        i==n?printf("%d\n",cnt):printf("%d ",cnt);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10351643.html

时间: 2024-07-30 14:00:46

寒假特训——I - Fair的相关文章

Android寒假实训云笔记总结——欢迎页

欢迎页使用的是viewpager,需要适配器. 注意点: 1.判断是否是第一次进入这个app. 2.欢迎页小圆点的逻辑. 实现原理: 首先在activity_welcome放入viewpager和固定四个小圆点的图片在下方. viewpager用于存放多张欢迎页的图. welcome_selectoer用于当在哪一页对应的小圆点为灰色状态.即设置成不可点时状态为灰色. viewpager的内容为静态加载进去. 如图: 直接贴代码,代码都有注释:  WelcomeActivity.java pac

Gym.101908 Brazil Subregional Programming Contest(寒假自训第六场)

这几天失眠时间都不太够,室友太会折腾了,感觉有点累,所以昨天的题解也没写,看晚上能不能补起来. B . Marbles 题意:给定N组数(xi,yi),玩家轮流操作,每次玩家可以选择其中一组对其操作,可以把它减去一个数,或同时减去一个数,当玩家操作后出现了(0,0)则胜利. 思路:注意这里是出现(0,0)胜,而不是全都是(0,0)胜,所以我们不能简单的球sg,最后异或得到答案. 但是我们转化一下,如果玩家面对的全是(1,2) 或(2,1),则他胜利,那么我们可以以这两个状态为起点得到sg函数,就

Gym .101879 USP Try-outs (寒假自训第七场)

B .Aesthetics in poetry 题意:给定N个数,(N<2000 ,a[i] <=1e9),让你找一个最大的K,使得N个数膜K的余数个数全都等于N/K个. 思路:我们找到N的因子,然后验证即可,复杂度O(N^2) #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int maxn=1000010; int a[maxn],N,a

Gym -102007 :Benelux Algorithm Programming Contest (BAPC 18) (寒假自训第5场)

A .A Prize No One Can Win 题意:给定N,S,你要从N个数中选最多是数,使得任意两个之和不大于S. 思路:排序,然后贪心的选即可. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int maxn=2000010; ll a[maxn]; int main() { int N,ans; l

寒假特训——搜索——H - Nephren gives a riddle

What are you doing at the end of the world? Are you busy? Will you save us? Nephren is playing a game with little leprechauns. She gives them an infinite array of strings, f0... ∞. f0 is "What are you doing at the end of the world? Are you busy? Will

Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)

A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; const double inf=1e200; const double eps=1e-12; const double pi=4*atan(1.0); int dcmp(double x){ return fabs(x)<eps?0:(x<0?-1:1);} struct

Gym102040 .Asia Dhaka Regional Contest(寒假自训第9场)

B .Counting Inversion 题意:给定L,R,求这个区间的逆序对数之和.(L,R<1e15) 思路:一看这个范围就知道是数位DP. 只是维护的东西稍微多一点,需要记录后面的各种数字的个数cnt,以及逆序对和sum,以及出现了多少种后缀num. 那么枚举到当前位时,假设为i ,那么sum+=cnt[i+1]+cnt[i+2]+....cnt[9];  cnt[i]+=num; 可以参考CF1073E. #include<bits/stdc++.h> #define rep(

整数划分问题(递归法 或 母函数法 )

样题:sdut2015寒假结训赛 开始我还以为是用背包来做,但是写完了代码,怎么写就是不对,并且在实现的时候确实有点地方我用背包的算法描述不了! 后来查到可以用:递归 或者 母函数算法! 比赛时曾考虑过用递归来实现,但没有推导出来,后来发现别人的博客里面写着“整数划分问题”应该在讲解递归的时候就该学会了. 我的心里顿时感到一股抱怨和悔恨,唉!当然自己的责任最大! 整数划分问题是算法中的一个经典命题之一,有关这个问题的讲述在讲解到递归时基本都将涉及.所谓整数划分,是指把一个正整数n写成如下形式:

Gym.102059: 2018-2019 XIX Open Cup, Grand Prix of Korea(寒假gym自训第一场)

整体来说,这一场的质量比较高,但是题意也有些难懂. E.Electronic Circuit 题意:  给你N个点,M根线,问它是否是一个合法的电路. 思路:  一个合法的电路,经过一些串联并联关系,最后有一个点是正极,一个是负极,我们就来模拟这个串联的过程即可,并联的关系会因为我们使用了set而抵消,所以可以不去管它.  模拟串联: 我们选择一个度数为2的点,删去它与两边的点u,v的连线,然后连接u-v,知道不存在度数为2的点. 最后当有两个点度数为1,而且不存在度数为大于大于2的点,则合法.