点击打开链接
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 n

towns in Byteland and  m

two-way roads between towns. Of course, you can reach any town from any other town using roads.

There are k

types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least  sdifferent types of goods. It costs  d(u,v) coins to bring goods from town  u to town  v where  d(u,v) is the length of the shortest path from  uto  v

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

towns.

Input

There are 4

integers  n,  m,  k,  s in the first line of input ( 1≤n≤105,  0≤m≤105,  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 n

integers  a1,a2,…,an ( 1≤ai≤k), where  ai is the type of goods produced in the  i-th town. It is guaranteed that all integers between  1 and  k occur at least once among integers  ai

.

In the next m

lines roads are described. Each road is described by two integers  u  v ( 1≤u,v≤n,  u≠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 n

numbers, the  i-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town  i

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

you can bring goods from towns  1 ( 0 coins),  2 ( 1 coin) and  4 ( 1 coin). Total numbers of coins is  2

.

Town 2

: Goods from towns  2 ( 0),  1 ( 1),  3 ( 1). Sum equals  2

.

Town 3

: Goods from towns  3 ( 0),  2 ( 1),  4 ( 1). Sum equals  2

.

Town 4

: Goods from towns  4 ( 0),  1 ( 1),  5 ( 1). Sum equals  2

.

Town 5

: Goods from towns 5 (0), 4 (1), 3 (2). Sum equals 3.

题意:

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

思路:

特产最多100种,可以计算每种特产到每个城镇的最短路,mov【i】【j】计算出所有j特产到所有i的最短距离,

这样的话是1e7,还可以勉强接受。

然后把所有到i城镇的特产的最短路 排序,取前s个就是i点的最短路径了。

直接将所有生产i型商品的城市一次性全加入队列,然后bfs

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include "algorithm"
using namespace std;
const int MAX=1e6+5;
int n,m,k,s;
int good[MAX],mov[MAX][105];
//mov[i][j]记录以生产j商品的城市为举办地,到i城市的最小花费
vector<int> E[MAX];
void bfs(int x)
{queue<int> Q;int i,u,v;Q.push(x+n);while(!Q.empty()){u=Q.front();Q.pop();for(i=0;i<E[u].size();i++){v=E[u][i];if(mov[v][x]==0){mov[v][x]=mov[u][x]+1;Q.push(v);}}}
}
int main()
{int i,j;int u,v,cnt;scanf("%d%d%d%d",&n,&m,&k,&s);for(i=1;i<=n;i++){scanf("%d",&good[i]);E[good[i]+n].push_back(i);//如果我们直接用一个for循环将生产good[i]型商品的城市加入队列的话,就会超时,所以,我们借用这个vector来处理!}for(i=0;i<m;i++){scanf("%d%d",&u,&v);E[u].push_back(v);E[v].push_back(u);}for(i=1;i<=k;i++) bfs(i);for(i=1;i<=n;i++){cnt=0;sort(mov[i]+1,mov[i]+k+1);for(j=1;j<=s;j++)cnt+=mov[i][j]-1;printf("%d ",cnt);}return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10,inf=0x3f3f3f3f3f;
int n,m,s,k,ans[maxn];
vector<int> edge[maxn],color[maxn];
int dis[107][maxn];
void bfs(vector<int>color,int *dis){fill(dis+1,dis+1+n,-1);queue<int>q;for (auto u:color){dis[u]=0;q.push(u);}while(!q.empty()){int u=q.front();q.pop();for (int v:edge[u])if(dis[v]==-1) dis[v]=dis[u]+1,q.push(v);}
}
int main(){scanf("%d%d%d%d",&n,&m,&k,&s);for (int i=1,tmp;i<=n;i++) scanf("%d",&tmp),color[tmp].push_back(i);for (int i=1,u,v;i<=m;i++){scanf("%d%d",&u,&v);edge[u].push_back(v);edge[v].push_back(u);}for (int i=1;i<=k;i++) bfs(color[i],dis[i]);for (int i=1;i<=n;i++){for (int j=1;j<=k;j++)ans[j]=dis[j][i];sort(ans+1,ans+1+k);printf("%d",accumulate(ans+1,ans+1+s,0));printf("%c",i==n?'\n':' ');}return 0;
}

Codeforces D. Fair 多源BFS求最短路相关推荐

  1. *【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)

    题干: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...

  2. UVa 816 (BFS求最短路)

    /*816 - Abbott's Revenge ---代码完全参考刘汝佳算法入门经典 ---strchr() 用来查找某字符在字符串中首次出现的位置,其原型为:char * strchr (cons ...

  3. 算法提高课-图论-单源最短路的建图方式-AcWing 920. 最优乘车:bfs求最短路、建图

    题目分析 来源:acwing 分析: 本题难在抽象建图上,这里采用的建图方式是:同一条公交线路上,前面的站点都可以连一条有向边到其后面的站点,且边权都为1. 由于边权都是1,可以用bfs来求最短路. ...

  4. CSP认证201409-4 最优配餐[C++题解]:bfs、多源bfs、最短路、图论

    文章目录 题目解答 题目链接 题目解答 来源:acwing 分析: 有很多起点S,同时有很多终点T,求每个终点到起点中最短的路,只要是到达其中一个起点就行.所以这是一个多源bfs的题目. ac代码 # ...

  5. AcWing 845. 八数码(3阶数字华容道):bfs求最短路,状态表示困难

    文章目录 题目 题目分析 题目 题目链接:AcWing 845. 八数码(数字华容道) 在一个3×3的网格中,1~8这8个数字和一个"x"恰好不重不漏地分布在这3×3的网格中. 例 ...

  6. UVa1600 Patrol Robot (BFS求最短路进阶)

    题目链接 A robot has to patrol around a rectangular area which is in a form of m × n grid (m rows and n ...

  7. 2018ACM-ICPC 焦作站现场赛 F. Honeycomb(BFS求最短路,卡memset)

    F. Honeycomb 从前不信命,从这道题开始,我信了. 我就是没有拿牌子的命.这道题或者说这个memset,击碎了我所有对ACM的美好记忆. #include<bits/stdc++.h& ...

  8. 魔戒-BFS求最短路

    魔戒 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 蓝色空间号和万有引力号进入了四维水洼, ...

  9. Wappo BFS求最短路+路径记录

    点击打开链接 描述 Einstein最近迷上了一款手机益智游戏Wappo,但是由于IQ和RP等诸多原因,他一直无法通关,他希望你编一个程序来玩这个游戏. Wappo的游戏规则是这样的:在一张m*n的地 ...

最新文章

  1. dial tcp 10.96.0.1:443: getsockopt: no route to host --- kubernetes(k8s)DNS 服务反复重启
  2. mopso算法代码程序_JAVA程序员的必杀技,面试中常考的8个经典算法题,过程精妙,代码精炼...
  3. 数据库不推荐使用外键的9个理由!
  4. javascript-模板方法模式-提示框归一化插件
  5. 奇异值分解 VS 特征值分解
  6. ES6 对正则表达式的扩展
  7. Python:我可以使用类变量作为线程锁吗?
  8. 2.JAVA-基础语法以及String的介绍
  9. Flutter基础—你好,Flutter!
  10. 【文末福利】聊天机器人的几种主要架构实现
  11. 排序算法之——三路快排分析
  12. cscd论坛_高压电器第九届电工技术前沿问题学术论坛“先进电磁技术”分论坛及专题征稿...
  13. 美国TOP100大学优势专业位置分布!长篇吐血整理!
  14. 5.2 差模信号、共模信号、共模抑制比
  15. 第4章第6节-水管工游戏
  16. Activiti6.0流程引擎学习——(22)activiti的任务管理服务(TaskService)
  17. 那些靠互联网年赚百万的大佬们是如何赚钱的?
  18. emmc和SPI共舞
  19. 上海市证券、保险公司一览
  20. latex作者不省略,加作者传记,latex图片取消自动编号,控制图片与上下文距离,段首取消空行。图的上下距离,去掉行间距

热门文章

  1. 应用负载均衡之LVS(三):ipvsadm命令
  2. Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)...
  3. 104.全排列(深搜)搜索与回溯
  4. 作品第四课----agruments应用一求出函数参数的总合
  5. Google Chrome v48.0.2564.
  6. php坐标轴取整,PHP取整函数:ceil,floor,round,intval的区别详细解析
  7. php实例类,php实例-对象与类
  8. 验证手机号码的正则表达式
  9. php分页类代码,php 分页类 扩展代码
  10. mysql map 键值对获取_mysql map_get function,用于解析map结构数据,根据key返回相对应value...