c语言sscanf函数的用法是什么
290
2022-08-28
D. Fair(多源bfs)
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
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
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
Copy
5 5 4 3 1 2 4 3 2 1 2 2 3 3 4 4 1 4 5
Copy
2 2 2 2 3
Copy
7 6 3 2 1 2 3 3 2 2 1 1 2 2 3 3 4 2 5 5 6 6 7
Copy
1 1 1 2 2 1 1
题目大概:
给出一个n个城镇m条边的图,给出每个城镇拥有的特产(可能多个城镇有相同特产)。有k种不同特产。
要求每个城镇需要其他城镇运输特产到自己的城镇,每个城镇必须拥有s种特产,那么在城镇满足s种特产后,需要的最短路径是多长,最短路指的是特产运输过来走过的边的数量。
思路:
一读完题,最暴力的思想是,对每个点进行一次bfs,算出每个点的最短路,但是数据是1e5,n2,就是1e10了。明显超时。
那么发现特产最多100种,可以计算每种特产到每个城镇的最短路,lis【i】【j】计算出所有j特产到所有i的最短距离,这样的话是1e7,还可以勉强接受。
然后把所有到i城镇的特产的最短路 排序,取前s个就是i点的最短路径了。
代码:
#include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~