博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
10_放置街灯(Placing Lampposts,UVa 10859)
阅读量:5268 次
发布时间:2019-06-14

本文共 4012 字,大约阅读时间需要 13 分钟。

问题来源:刘汝佳《算法竞赛入门经典--训练指南》 P70 例题30:

问题描述:有给你一个n个点m条边(m<n<=1000)的无向无环图,在尽量少的节点上放灯,使得所有边都被照亮,每盏灯将照亮以它为一个端点的所有边。在灯的总数最小的前提下,被两盏灯同时照亮的边数尽量大。

问题分析:1.题中的图,是由多颗树构成的森林,对每颗树用相同的方法即可。

       2.本题优化目标:放置的街灯数a应尽量少,在a尽量少的情况下,被两盏灯同时照亮的边数b尽量大(即只被一盏灯照亮的边数c尽量小(b+c=m)),两个要求可合并为一个要求,即x(x=Ma+c),(要求无论c怎么取值都不会影响a的取值,M是一个比c的最大理论值还要大的数即可)

       3.对于树上的每一个节点i,只有两种状态,放灯和不放灯,i的状态将影响其子节点的决策,则可以将父节点(fa)的状态加入状态表示中,设i点的状态为dp[i][j],其中若j=0表示节点i的父亲节点(fa)没有放灯,若j=1表示fa放灯了:

        决策1:节点i不放灯,必须保证j==1(fa放灯) || i为根节点,此时dp[i][j] = Sum{dp[k][0] | k为取遍i的所有子节点}

             如果i不是根dp[i][j]++(i与fa之间只有一盏灯);

        决策2:节点i放灯,此时dp[i][j] = Sum{dp[k][1] | k为取遍i的所有子节点}+M

             如果j==0 && i不为根节点dp[i][j]++(i与fa之间只有一盏灯);

例题链接:

例题:UVa 10891

10859 - Placing Lampposts

Time limit: 3.000 seconds

  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 #include "cstdio" 2 #include "cstring" 3 #include "vector" 4 using namespace std; 5  6 #define N 1005 7 #define M 2000 8 int n,m; 9 int dp[N][2]; //对于每个节点,只有两种状态(1:选,0:不选)10 int vis[N][2];  //标记11 vector
adj[N];12 13 int inline Min(int a,int b)14 {15 return a
=0) ans++; //父节点没有放灯(j==0) 并且i不为根节点(fa!=-1),则点i和点fa相连的这条边只有一盏灯照亮,ans++;37 if(j==1 || fa<0) //i为根节点(fa==-1),或者父亲节点放灯了(j==1),就可以考虑i点不放灯.38 {39 int sum = 0;40 for(int k=0; k
=0) sum++; //如果i不是根,则点i和点fa相连的这条边只有一盏灯照亮,sum++;44 ans = Min(ans,sum);45 }46 return ans;47 }48 49 int main()50 {51 int T;52 int i;53 int x,y;54 int ans;55 scanf("%d",&T);56 while(T--)57 {58 scanf("%d %d",&n,&m);59 Init();60 ans = 0;61 for(i=1; i<=m; i++)62 {63 scanf("%d %d",&x,&y);64 adj[x].push_back(y);65 adj[y].push_back(x);66 }67 for(i=0; i

 

转载于:https://www.cnblogs.com/ruo-yu/p/4391774.html

你可能感兴趣的文章
修复IE不能执行JS的方法
查看>>
程序员究竟该如何提高效率zt
查看>>
希尔排序法(缩小增量法)
查看>>
PHP编程基础学习(一)——数据类型
查看>>
MongoDB-JAVA-Driver 3.2版本常用代码全整理(2) - 查询
查看>>
NPOI处理Word文本中上下角标
查看>>
Android笔记 Handler
查看>>
如何阅读大型前端开源项目的源码(转)
查看>>
java.util.Arrays类详解
查看>>
idea搭建tocmat
查看>>
NYOJ-626-intersection set(二分查找)
查看>>
项目管理之路(1):初步踏入项目管理
查看>>
Java 中 静态方法与非静态方法的区别
查看>>
echarts饼图显示百分比
查看>>
JMS消息
查看>>
Jenkins+ProGet+Windows Batch搭建全自动的内部包(NuGet)打包和推送及管理平台
查看>>
php上传文件及头像预览
查看>>
大四java实习生的一些经历
查看>>
线程池的概念
查看>>
Oracle_Statspack性能诊断工具
查看>>