Placing Lampposts

As a part of the mission ‘Beautification of Dhaka City’, the government 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 the requirement, the government has decided to buy the minimum number of lampposts required
to light the whole city.
Dhaka city can be modeled as an undirected graph with no cycles, multi-edges or loops. There are
several roads and junctions. A lamppost can only be placed on junctions. These lampposts can emit
light in all the directions, and that means a lamppost that is placed in a junction will light all the roads
leading away from it.
The ‘Dhaka City Corporation’ has given you the road map of Dhaka city. You are hired to find
the minimum number of lampposts that will be required to light the whole city. These lampposts can
then be placed on the required junctions to provide the service. There could be many combinations of
placing these lampposts that will cover all the roads. In that case, you have to place them in such a
way that the number of roads receiving light from two lampposts is maximized.
Input

There will be several cases in the input file. The first line of input will contain an integer T (T ≤ 30)
that will determine the number of test cases. Each case will start with two integers N (N ≤ 1000)
and M (M < N) that will indicate the number of junctions and roads respectively. The junctions are
numbered from 0 to N − 1. Each of the next M lines will contain two integers a and b, which implies
there is a road from junction a to b, (0 ≤ a, b < N) and a ̸= b. There is a blank line separating two
consecutive input sets.
Output

For each line of input, there will be one line of output. Each output 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 receiving lights from two lampposts and the third integer will be the number of roads that are
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

题意:

  一个无向图上,要去放灯,要求每条边都被照亮的最少灯数,并且1边被两盏灯照亮的边数要尽量多,输出灯数,两盏照亮的边数,一盏照亮的边数。

题解:

  首先对于每个节点,我们都有取或不取,这个DP就好了,下面是如何保证取优先值方法

取双优值方法:

      考虑要保证a最小的情况下...b最小...那么就是在状态转移过程中保证a是占主体地位的..只有当a相等时..b才发挥作用...这可以联想到两个数比较.

      首先要比最高位..当最高位不相等时..低位如何变换都不能影响到比较结果...

那么选取一个尽可能大但又不至于爆int,long long的数M..让在题目范围内..b如何多..都达不到M..那么表示状态下值为a*M+b

输出答案时...P/M为第一个最优的...P%M为保证第一个最优下第二个最优

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e3+, M = , mod = 1e9+, inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0
vector<int > G[N];
int n,m,vis[N],T,ans,dp[N][];
void dfs(int u) {
dp[u][] = ;
dp[u][] = M+;
vis[u] = ;
for(int i=;i<G[u].size();i++) {
int to = G[u][i];
if(vis[to]) continue;
dfs(to);
dp[u][]+=dp[to][];
dp[u][] += min(dp[to][]-,dp[to][]+);
}
}
int main() {
scanf("%d",&T);
while(T--) {
for(int i=;i<N;i++) G[i].clear(), vis[i] = ;
scanf("%d%d",&n,&m);
memset(dp,,sizeof(dp));
for(int i=;i<=m;i++) {
int a,b;
scanf("%d%d",&a,&b);
a++,b++;
G[a].push_back(b);
G[b].push_back(a);
}
ans = ;
for(int i=;i<=n;i++) {
if(vis[i]) continue;
dfs(i);
dp[i][]--;
ans += min(dp[i][], dp[i][]);
// cout<<min(dp[i][0], dp[i][1])<<endl;
}
printf("%d %d %d\n",ans/M, m-ans%M, ans%M);
}
return ;
}

UVA 10859 - Placing Lampposts 树形DP、取双优值的更多相关文章

  1. UVa 10859 - Placing Lampposts 树形DP 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  2. UVA 10859 Placing Lamppost 树形DP+二目标最优解的求解方案

    题意:给定一个无向,无环,无多重边,要求找出最少的若干点,使得,每条边之中至少有一个点上有街灯.在满足上述条件的时候将还需要满足让两个点被选择的边的数量尽量多. 题解: 对于如何求解最小的节点数目这点 ...

  3. UVaLive 10859 Placing Lampposts (树形DP)

    题意:给定一个无向无环图,要在一些顶点上放灯使得每条边都能被照亮,问灯的最少数,并且被两盏灯照亮边数尽量多. 析:其实就是一个森林,由于是独立的,所以我们可以单独来看每棵树,dp[i][0] 表示不在 ...

  4. UVA - 10859 Placing Lampposts 放置街灯

    Placing Lampposts 传送门:https://vjudge.net/problem/UVA-10859 题目大意:给你一片森林,要求你在一些节点上放上灯,一个点放灯能照亮与之相连的所有的 ...

  5. uva 10859 - Placing Lampposts dp

    题意: 有n个节点,m条边,无向无环图,求最少点覆盖,并且在同样点数下保证被覆盖两次的变最多 分析: 1.统一化目标,本题需要优化目标有两个,一个最小灯数a,一个最大双覆盖边数b,一大一小,应该归一成 ...

  6. uva10859 Placing Lampposts (树形dp+求两者最小值方法)

    题目链接:点击打开链接 题意:给你一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都被照亮,每盏灯将照亮以它为一个端点的所有边.在灯的总数最小的前提下,被两盏灯同时照亮的边数应尽量大. ...

  7. UVa 10859 Placing Lampposts

    这种深层递归的题还是要多多体会,只看一遍是不够的 题意:有一个森林,在若干个节点处放一盏灯,灯能照亮与节点邻接的边.要求:符合要求的放置的灯最少为多少,在灯数最少的前提下,一条边同时被两盏灯照亮的边数 ...

  8. LightOj 1230 Placing Lampposts(树形DP)

    题意:给定一个森林.每个节点上安装一个灯可以覆盖与该节点相连的所有边.选择最少的节点数num覆盖所有的边.在num最小的前提下,合理放置num个灯使得被两个灯覆盖的边最多? 思路:F[i][0]代表没 ...

  9. UVA - 1218 Perfect Service(树形dp)

    题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...

随机推荐

  1. cloudfoundry service broker 制作

    实验室这边需要制作service broker.从今天开始将精力投入其中.

  2. c++对象关系映射(ORM)框架

    ORM(Object Relational Mapping, 对象关系映射),用来将基于对象的数据结构映射到SQL的数据结构中,即将基于对象的数据映射到关系表中的字段,然后我们可以通过对象提供的接口来 ...

  3. lua类实现

    _Account = {} --创建一张借记卡 function _Account:new( tb ) local _Tb = tb or {} setmetatable(_Tb, self) sel ...

  4. LyX初步

    最近写毕业论文少量入手了LyX. 这个工具是两三年前在CTeX群里听说的.当时感觉太高大上,连Linux下用LaTeX都还没搞定,于是没想这个. 但是最近用了LaTeX模板感觉太麻烦,于是试着装了一下 ...

  5. 【MFC】如何在mfc窗口程序中调用控制台

    1.工程名为Zero,在CZeroDlg.cpp中加入头文件 #include “conio.h” : 2.在CZeroDlg::OnInitDialog() {…}函数中加入AllocConsole ...

  6. hdu 2489 dfs枚举组合情况+最小生成树

    大家都说,搜索是算法的基础.今天最这题就有体会了.在n个顶点里选择m个顶点,求最小生成树.用到了深搜的回溯.所有情况都能枚举. #include<iostream> #include< ...

  7. hdu1811 Rank of Tetris 拓扑排序+并查集

    这道题是拓扑排序和并查集的综合运用. 由于排行榜是一种从高到低的排序.所以在拓扑排序的时候,如果有一次加入的入度为零的点数大于1,就有变得不确定了(UNCERTAIN). 由于只有一棵树,当树的数量大 ...

  8. 微信小程序开发之animation动画实现

    1. 创建动画实例 wx.createAnimation(OBJECT) 创建一个动画实例animation.调用实例的方法来描述动画.最后通过动画实例的export方法导出动画数据传递给组件的ani ...

  9. asp.net URL传递中文参数System.Web.HttpUtility.UrlEncode与Server.UrlEncode的区别

    asp.net URL传递中文参数System.Web.HttpUtility.UrlEncode与Server.UrlEncode的区别(一) HttpUtility.UrlEncode 方法: 对 ...

  10. 整理Py小demo

    from email.mime.text import MIMEText # 第一个参数就是邮件正文,第二个参数是MIME的subtype, # 传入'plain'表示纯文本,最终的MIME就是'te ...