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. Android之Action Bar

    Action Bar在实际应用中,很好地为用户提供了导航,窗口位置标识,操作点击等功能.它出现于Android3.0(API 11)之后的版本中,在2.1之后的版本中也可以使用. 添加与隐藏Actio ...

  2. python课程设计笔记(二)破冰基本语法

    python两种编程方式:交互式与文件式 交互式:语法练习,输一条运行一条 文件式:通用,执行一组语句 注释 #单行注释  ...XXXXX...多行注释 逻辑 没有大括号,按缩进确定逻辑——缩进格数 ...

  3. Ajax内容签名技术(减少无谓流量损耗)

    UI界面Ajax获取数据内容的时候,一般是直接加载内容填充,不管内容有无变化.自己也是一直这么干,包括定时刷新公告等.今天在浏览器控制台调试的时候,发现动态刷新内容,其实挺耗费流量的,特别是内容无变化 ...

  4. WAMP安装之坑

    Apache安装目录不能有空格 Apache根目录修改后不能直接localhost打开,可以通过改变端口,然后输入 localhost:端口号 打开

  5. Java入门第一季——从此投身Java??

    找工作告一段落. 最后的工作呢,和java紧密相关,也是阴差阳错,不过都是软件开发,都好了,不过以后侧重点肯定是在java这边,php有机会还是一直学下去的,那么美的说~ Java开发第一季  一.简 ...

  6. 工欲善其事必先利其器之windows篇

    Windows是我们最常用的系统,下面就让我们重新认识一下Windows有哪些可以让我们提高工作效率的快捷键以及部分技巧,,以及在外行看来可以看起来逼格高的技巧! 1.Windows最实用,最常用的快 ...

  7. JS 1000以内的水仙花数 (三位数 各个数字的立方和等于本身 例如 1*1*1 + 5*5*5 + 7*7*7 = 157)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. BZOJ 4827: [Hnoi2017]礼物 FFT_多项式_卷积

    题解稍后在笔记本中更新 Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r&q ...

  9. springboot使用aop做日志

    一.引入jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  10. [SDOI2016]生成魔咒(后缀自动机)

    看一眼题.本质不同的字串数. 嘴角微微上扬. 每一次加一个数输出一个答案. 笑容渐渐消失. 等等,\(SAM\)好像也可以求本质不同的字串. 设当前字符串用\(x\)表示,每次插入完成后\(ans\) ...