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




英文题目:


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 next Mlines 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

2
4 3
0 1
1 2
2 3

5 4
0 1
0 2
0 3
0 4

Sample Output

2 1 2
1 0 4

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


我的思路是:

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

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

之后遍历所有子集即可.

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值