UVa10859 放置街灯
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
题目大意:给定一个无向无环图,要求在点上放灯,如果某一点上放了等,则可以照亮与它相通的边,现在要求放尽量少得等,使得所有边都被照亮,并且输出灯数,被照亮两次的边数(即边的两个端点均放置灯),被照亮一次的边。 如果等数一样的话,按照被照亮一次边越大的方案。
解题思路:
优化目标有两个,使得灯数a尽量少,使得被两个灯的边数b尽量多
在这里可以转化一下,就是恰被一盏灯照亮的边数c尽量少
<IMPORTANT>当我们同时需要优化两个变量a,c,要求首先满足a最小的情况下,使得c尽量小
我们可以引入一个值x=Ma+c,在这里的M是一个很大的值,是一个比c的理论最大值和a的最小理论值之差还要大的值
为什么?因为如果两个a不同的,无论c相差多少,仍然是a起决定性的作用
则我们的目标就是使x的值尽量小,那么x/M的整数部分就是灯数,x%M就是只被一个灯照亮的,m-x%M就是被两个灯照亮的
我们令dp[i][j]表示在i节点的x的最小值(j=1表示父亲节点放灯,0则反之)
那么对i来说,有两个决策
如果在i放灯
那么dp[i][j] = sum(dp[k][1])+M,k是i的子节点。如果j=0且i不是根,还要+1,因为i和他的父亲节点这条边只被一个灯照亮
如果在i不放灯,必须j=1(不然边就无法照亮)
那么dp[i][j] = sum(dp[k][0]),如果i不是根,还要加1,因为i和他的父亲节点这条边只被一个灯照亮
通过边DFS边DP的思路就可以把结果求出来
另外图可能有多个连通分量
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define Size 1005
#define M 2000
using namespace std; int d[Size][];
bool vis[Size][];
vector<int> edge[Size];
int n,m; int dp(int i,int j,int f){
if(vis[i][j]==true)return d[i][j];
vis[i][j]=true; int k=edge[i].size(); //yes
int& ans=d[i][j];
ans=M;
for(int ch=;ch<k;ch++){
if(edge[i][ch]!=f){
ans+=dp(edge[i][ch],,i);
}
}
if(j==&&f!=-)ans++; //no
if(j== || f==-){
int ans2=;
for(int ch=;ch<k;ch++){
if(edge[i][ch]!=f){
ans2+=dp(edge[i][ch],,i);
}
}
if(f!=-)ans2++; ans=min(ans,ans2);
} return ans;
} int main(){
freopen("30.in","r",stdin); int T; cin>>T;
while(T--){
for(int i=;i<=n;i++)edge[i].clear();
memset(vis,false,sizeof(vis)); cin>>n>>m;
int a,b;
for(int i=;i<m;i++){
cin>>a>>b;
edge[a].push_back(b);
edge[b].push_back(a);
} int ans=;
for(int i=;i<n;i++){
if(vis[i][]==false){
ans+=dp(i,,-);//-1表示无父节点 即 i为根节点
}
} cout<<ans/M<<' ' <<m-ans%M<<' '
<<ans%M<<endl;
} fclose(stdin);
return ;
}
UVa10859 放置街灯的更多相关文章
- 10_放置街灯(Placing Lampposts,UVa 10859)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P70 例题30: 问题描述:有给你一个n个点m条边(m<n<=1000)的无向无环图,在尽量少的节点上放灯,使得所有边都被照 ...
- UVA - 10859 Placing Lampposts 放置街灯
Placing Lampposts 传送门:https://vjudge.net/problem/UVA-10859 题目大意:给你一片森林,要求你在一些节点上放上灯,一个点放灯能照亮与之相连的所有的 ...
- 「算法笔记」树形 DP
一.树形 DP 基础 又是一篇鸽了好久的文章--以下面这道题为例,介绍一下树形 DP 的一般过程. POJ 2342 Anniversary party 题目大意:有一家公司要举行一个聚会,一共有 \ ...
- UVa10895 Placing Lampposts
UVa10895 Placing Lampposts 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34290 [思路] ...
- Spring环境搭建之:导入jar包、配置文件名称及放置位置
Spring环境搭建之:导入jar包.配置文件名称及放置位置 现在项目开发中spring框架应用的还是比较多的,自己用的还不太熟练,每次用的时候总配置半天,总有些配置弄错,就找个时间总结以下,方便以后 ...
- ArcGIS ElementLayer上放置Windows控件
ElementLayer是ArcGIS API for Silverlight/WPF中的一种图层类型,主要用来承载Silverlight/WPF中的UIElement对象(UIElement),使用 ...
- JavaScript放置位置区别
JavaScript放置位置区别 页面中的脚本会在页面载入浏览器后立即执行.我们并不总希望这样.有时,我们希望当页面载入时执行脚本,而另外的时候,我们则希望当用户触发事件时才执行脚本. 位于 head ...
- cadence学习之——原理图库的添加及器件的放置
画原理图,库是必不可少的,库有cadence自带的,也可以自己建自己的库,然后在画原理图工程时, 这些库都需要被添加进原理图工程才能使用. 1.库的添加 打开Place Part属性框,操作如下: ( ...
- 使用Fragment应用放置后台很久,被系统回收,出现crash
使用Fragment应用放置后台很久,被系统回收,出现crash:原因:系统做了源码FragmentActivity调用onSaveInstanceState保存Fragment对象,这时候系统恢复保 ...
随机推荐
- mysql分区表之二:MySQL的表的四种分区类型介绍
一.什么是表分区 通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysql5.1开始支持数据表分区了.如:某用户表的记录超过了600万条,那么就可以根据入库日期将表分区,也可以根据所在地将表分区 ...
- 查询mysql数据库启动时间抛异常
mysql 5.7.10使用dbforget Studio 连接异常 提示:The'INFORMATION_SCHEMA.SESSION_VARIABLES' feature is dis 查看mys ...
- 第三章 k8s cluster环境创建
1 用如下方法安装指定版本的docker,但是我的环境会报错 # 安装rpm apt install rpm # 下载 RPM 包, docker 版本 wget https://download. ...
- 从一个开发的角度看负载均衡和LVS--FullNat
从一个开发的角度看负载均衡和LVS 在大规模互联网应用中,负载均衡设备是必不可少的一个节点,源于互联网应用的高并发和大流量的冲击压力,我们通常会在服务端部署多个无状态的应用服务器和若干有状态的存储服务 ...
- 华为交换机S5700 vty 0 4
最佳答案 vty 0 4 代表有5条VTY线路 由0到4 这个要看设备的版本有的设备会有更多条VTY线路比如一些企业版的设备 显示用户界面 进入TELNET 设置模式 user-interface ...
- c++获取lua嵌套table某属性的值
开发环境:vs2012 lua版本:LuaJIT-2.0.2 lua文件作为配置文件,c++读取这个配置. lua配置结构如下 SceneConfig = { [] = { name =}, [] = ...
- 深入浅出 Java Concurrency (13): 锁机制 part 8 读写锁 (ReentrantReadWriteLock) (1)
从这一节开始介绍锁里面的最后一个工具:读写锁(ReadWriteLock). ReentrantLock 实现了标准的互斥操作,也就是一次只能有一个线程持有锁,也即所谓独占锁的概念.前面的章节中一 ...
- 【Consul】Consul实践指导-配置文件
Agent有各种各样的配置选项,这些配置选项可以通过命令行参数的方式设定,也可用通过配置文件的方式设定--所有的配置选项都是可选的,当然也是有默认值的. 当加载配置选项时,consul是按照词典顺序从 ...
- SpringData JPA 接口和方法
1.1 简单查询--接口方法 1.2 五个接口详解 1.2.1 CrudRepository接口 其中T是要操作的实体类,ID是实体类主键的类型.该接口提供了11个常用操作方法. @NoRepo ...
- malloc()与calloc区别 (转)
另外说明: 1.分配内存空间函数malloc 调用形式: (类型说明符*) malloc (size) 功能:在内存的动态存储区中分配一块长度为"size" 字节的连续区域.函数的 ...