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对象,这时候系统恢复保 ...
随机推荐
- Js、jquery学习笔记
end() 方法 重新定位到上次操作的元素,一般与siblings()一起使用,操作其兄弟元素.如:$(this).addClass("highlight").children(& ...
- AT命令控制上网 PDP
1. AT+CGATT=1 (Attach or detach from GPRS service, GPRS 附着状态) 说的简单点,这一步就是让SGSN (服务GPRS节点,你可以把它理解成与基站 ...
- Java安全 – JCE Blowfish算法报错
代码里用Blowfish算法加解密,结果jdk升到1.7后算法初始化失败 java.lang.RuntimeException: java.lang.RuntimeException: PANIC: ...
- HDU-3661-Assignments
/* Assignments Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- .NET自带IOC容器MEF之初体验(转)
本文主要把MEF作为一种IOC容器进行讲解,.net中可用的IOC容器非常多,如 CastleWindsor,Unity,Autofac,ObjectBuilder,StructureMap,Spri ...
- redis存session问题测试内容
转至元数据起始 官网,现网由于是双节点,session是存储在redis作为共享的. +1是单节点.目前是存储成文件的 本次的问题根源是 由于 session是存储在redis,所造成的. 所以需 ...
- Flask之模板之宏、继承、包含
3.5 宏.继承.包含 类似于python中的函数,宏的作用就是在模板中重复利用代码,避免代码冗余. Jinja2支持宏,还可以导入宏,需要在多处重复使用的模板代码片段可以写入单独的文件,再包含在所有 ...
- linux tcp调优
Linux TCP Performance Tuning News Linux Performance Tuning Recommended Books Recommended Links Linux ...
- Python网络编程与并发编程
网络编程基础 黏包 , 并发 计算机网络的发展及基础网络概念 Python 中的进程与 锁 Python IO 多路复用 \协程
- 如何清除svn的账号缓存信息(solaris)
如果我们不小心输入svn账号错误的话,后面就一直提示认证失败,不能checkout代码. 这个是因为svn把你输入的账号进行了缓存. 如果我们想重新输入新的账号,必须要清除缓存 svn存储账号的目录在 ...