UVA 10859 有向無環圖的動態規劃
http://vjudge.net/vjudge/contest/view.action?cid=53516#problem/E
Description
|
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 |
給你一個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-11504 - Dominos(有向圖的強連通分量)
- UVA 10498 Happiness!(線性規劃)APP
- 拓撲排序 (BFS )DAG (有向無環圖)排序
- 動態規劃動態規劃
- 圖解 | 原來這就是動態規劃圖解動態規劃
- 演算法(七):圖解動態規劃演算法圖解動態規劃
- 有關動態規劃的相關優化思想動態規劃優化
- 動態規劃之數的劃分動態規劃
- 無向圖的最小環問題
- 動態規劃法動態規劃
- 模板 - 動態規劃動態規劃
- 動態規劃初步動態規劃
- [leetcode] 動態規劃(Ⅰ)LeetCode動態規劃
- 動態規劃(DP)動態規劃
- 動態規劃分析動態規劃
- 禮物的最大價值(一維動態規劃&二維動態規劃)動態規劃
- 【圖解動態規劃】打家劫舍 II(四種解法)圖解動態規劃
- 演算法系列-動態規劃(1):初識動態規劃演算法動態規劃
- 無向圖的最小環問題[TODO]
- 3.動態規劃動態規劃
- 動態規劃題單動態規劃
- 動態規劃方法論動態規劃
- 動態規劃 總結動態規劃
- 雙序列動態規劃動態規劃
- 動態規劃(Dynamic programming)動態規劃
- 區間動態規劃動態規劃
- 好題——動態規劃動態規劃
- [leetcode 1235] [動態規劃]LeetCode動態規劃
- [atcoder 358] 【動態規劃】動態規劃
- 動態規劃專題動態規劃
- 動態規劃-----線性動態規劃
- 動態規劃初級動態規劃
- 淺談動態規劃動態規劃
- 動態規劃小結動態規劃
- 我的動態規劃題單動態規劃
- [簡易版]有向無環圖(DAG)前端視覺化前端視覺化
- 乾貨:圖解演算法——動態規劃系列圖解演算法動態規劃
- 如何在 Java 中實現無向環和有向環的檢測Java