[动态规划] 放置街灯 Uva 10859

英文题目:

As a part of the mission Beautification of Dhaka City, thegovernment has decided to replace all the old lampposts with new expensive ones.Since the new ones are quite expensive and the budget is not up to therequirement, the government has decided to buy the minimum number of lamppostsrequired to light the whole city.Dhaka city can be modeled as an undirected graph with no cycles, multi-edges orloops. There are several roads and junctions. A lamppost can only be placed onjunctions. These lampposts can emit light in all the directions, and that means alamppost that is placed in a junction will light all the roads leading away fromit.The Dhaka City Corporation has given you the road map of Dhaka city. You arehired to find the minimum number of lampposts that will be required to light thewhole city. These lampposts can then be placed on the required junctions toprovide the service. There could be many combinations of placing these lamppoststhat will cover all the roads. In that case, you have to place them insuch a way that the number of roads receiving light from two lampposts ismaximized.

Input

There will be several cases in the input file. The first lineof input will contain an integerT(T<=30) that will determine the numberof test cases. Each case will start with two integersN(N<=1000) andM( M<N) that will indicate the number of junctions and roads respectively.The junctions are numbered from0 toN-1. Each of the nextMlines will contain two integersa andb, which implies there is aroad from junctiona tob,( 0<= a,b < N ) and a != b. There is a blank line separating twoconsecutive input sets.

Output

For each line of input, there will be one line of output. Eachoutput line will contain 3 integers, with one space separating two consecutive numbers.The first of these integers will indicate the minimum number of lampposts required to light the whole city. The second integer will be the number of roads that are receivinglights from two lampposts and the third integer will be the number of roads thatare receiving light from only one lamppost.

Sample Input

24 30 11 22 35 40 10 20 30 4

Sample Output

2 1 21 0 4

Problem Setter: Sohel Hafiz.Special thanks to Per Austrin.

我的思路是:

先将每个灯周围的路存储 为 1 其他 路为 0 . 再算出任意几个灯点亮之后的补集, 被重复点亮的路记为2, 只点亮一次的记为1, 没有点亮的是0.

优化公式x=M*a+b, a为 未点亮的灯的个数, b为 标记为2 的路的个数 M为略微大于路总数的一个常数.

之后遍历所有子集即可.

代码与我上一篇文章类似.

,有了你,我不再作孤飞于蓝天的雄鹰,

[动态规划] 放置街灯 Uva 10859

相关文章:

你感兴趣的文章:

标签云: