UVA 10859 有向无环图的动态规划
来源:互联网 发布:淘宝卖的猛犸象牙猫腻 编辑:程序博客网 时间:2024/05/21 21:38
http://vjudge.net/vjudge/contest/view.action?cid=53516#problem/E
Description
Input: Standard In
Output: Standard Out
Time Limit: 2 seconds
Problem DPlacing LamppostsAs 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
24 3
0 1
1 2
2 3
5 4
0 1
0 2
0 3
0 4
Sample Output
2 1 21 0 4
给你一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有的边都被照亮,每盏灯将照亮以他为一个端点的所有边。在等的总数量最小的前提下,被两盏灯同时照亮的边数音尽量大。
解题思路:
见大白书p71。(太多解释,我就不照着敲了)
#include <stdio.h>#include <string.h>#include <vector>using namespace std;vector<int>adj[1010];int vis[1010][2],d[1010][2],n,m;int dp(int i,int j,int f)//f是i的父亲节点{ if(vis[i][j]) return d[i][j]; vis[i][j]=1; int& ans=d[i][j];//引用的意思就是d[i][j]和ans完全等价,改变一个就相当于改变另一个 //放灯总是合法的决策 ans=2000;//灯的数量加1,x+2000 for(int k=0; k<adj[i].size(); k++) if(adj[i][k]!=f)//这个判断非常重要!除了父节点之外的相邻节点才是子节点 ans+=dp(adj[i][k],1,i);//注意这些节点的父节点是i; if(!j&&f>=0)ans++;//如果i不是根,且父节点没放灯,则x+1 if(j||f<0)//i是根或者其父节点已放灯,i才可以不放灯 { int sum=0; for(int k=0; k<adj[i].size(); k++) if(adj[i][k]!=f) sum+=dp(adj[i][k],0,i); if(f>=0)sum++;//如果i不是根,则x+1 ans=min(ans,sum); } return ans;}int main(){ int T,a,b; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0; i<n; i++) //adj里面保存着上一组数据的值,因此清空是必要的 adj[i].clear(); for(int i=0; i<m; i++) { scanf("%d%d",&a,&b); adj[a].push_back(b); adj[b].push_back(a);//无向图双向建边 } memset(vis,0,sizeof(vis)); int ans=0; for(int i=0; i<n; i++) { if(!vis[i][0])//新的一棵树 ans+=dp(i,0,-1);//i是树根,因此父亲结点不存在为-1 } printf("%d %d %d\n",ans/2000,m-ans%2000,ans%2000);//从x计算3个整数 } return 0;}
- UVA 10859 有向无环图的动态规划
- 有向无环图的动态规划实现
- 有向无环图DAG上的动态规划
- 动态规划求有向无环图的最短路径
- C++之求有向无环图的最长路径(拓扑排序+动态规划)
- 矩形嵌套 ————DAG(有向无环图)上的动态规划
- C++之求有向无环图的最长路径(拓扑排序+动态规划)
- 【笔试/面试】—— 有向无环图(DAG)的最短路径问题(动态规划)
- UVa 1025 (DAG 上的动态规划,有固定终点的最短时间,逆推法)
- 有向无环图中最长路(无权有向无环无回路图)动态规划初步!
- 有向边的拓扑排序 UVA
- uva 437 DAG上的动态规划
- UVA 10859 Placing Lampposts (动态规划)
- 动态规划-uva-674
- UVA 1619 动态规划
- 动态规划 uva 1025
- uva 11584 动态规划
- uva 10003 动态规划
- 【SCOI2005】扫雷
- C/C++函数与算法速查手册正在改版,欢迎多提宝贵意见
- 【机房收费系统】之MSHFlexGrid控件清空
- UVA - 140 Bandwidth(全排列枚举)
- 【HDU2888】BurningSide定理+因子分解+求逆元+矩阵快速幂
- UVA 10859 有向无环图的动态规划
- 如何使用PHP缓存?
- Android大图片裁剪终极解决方案
- Simplify Path
- 《老罗的Android之旅》导读PPT
- 家训
- 2014年8月4日 金砖银行来了
- Excel公式与函数案例速查手册/电脑技巧从入门到精通丛书
- UVa-10098-Generating Fast