HDU 4035 Maze probability dp + tree dp, hdu4035

Source: Internet
Author: User

HDU 4035 Maze probability dp + tree dp, hdu4035

Question: Click the open link.


#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <algorithm>#include <map>#include <cmath>using namespace std;const double eps = 1e-9;const int N = 10010;vector<int> G[N];int n;double k[N], e[N], dp[N];double A[N], B[N], C[N];bool dfs(int u, int fa){int m = G[u].size();A[u] = k[u];B[u] = (1 - k[u] - e[u])/m;C[u] = 1 - k[u] - e[u];double tmp = (1 - k[u] - e[u]) / (double)m, self = 0;for(int i = G[u].size()-1; i >= 0; i--){int v = G[u][i]; if(v == fa)continue;if(!dfs(v, u)) return false;A[u] +=  tmp * A[v];self += tmp * B[v];C[u] += tmp * C[v];}if(fabs(self - 1.0) < eps) return false;A[u] /= 1-self;B[u] /= 1-self;C[u] /= 1-self;return true;}void input(){cin>>n;for(int i = 1, u, v; i < n; i++){scanf("%d %d", &u, &v);G[u].push_back(v); G[v].push_back(u);}for(int i = 1; i <= n; i++) {scanf("%lf %lf", &k[i], &e[i]);k[i] /= 100.0;e[i] /= 100.0;}}int main() {int T, Cas = 1; cin>>T;    while(T--){    input();    bool ok = dfs(1, -1);    printf("Case %d: ", Cas++);    if(!ok || fabs(1.0-A[1]) < eps)    puts("impossible");    else    printf("%.6f\n", C[1] / (1.0-A[1]));    for(int i = 1; i <= n; i++) G[i].clear();    }    return 0;}



200 dynamic planning details

Well, I haven't learned this for a long time. I am also confused. I guess I just got started two months ago ......
You can see that what he wrote is ineffective and what the optimal sub-structure is. I am also too large ............
Dynamic Planning generally solves two types of problems: optimization, maximum value and minimum value, and total number of solutions.

There are many subcategories,
I have met many other students (I am a sophomore and am currently preparing for NOIP)
(If you have more questions, I will only say the name)
Backpack. I won't say much about it when I put it on even the 9th floor ......
The longest non-rising sub-sequence problem (for example, the flying of the pan PAAs birthday simulation competition is a classic non-falling deformation)
Resource allocation problems (such as window layout, canopy problems, and machine allocation problems)
Interval returns (maximum product, energy necklace, etc)
Longest public subsequence problem (there is a genetic code like );
Solution tree, for example, the stair climb ........................

There are many types of dynamic planning, because they are flexible. Our teacher once found 100 DP equations for us, but they are useless. The key is understanding.

A little deeper, we have the DP optimization, and the dimensionality reduction of time space (that is, using other methods, or for example, a backpack is originally a two-dimensional space that has been optimized to a one-dimensional one ), tree-like DP (I won't ).
(There is a classic question "crossing the river" in optimization.)

I think DP is the kind of one that suddenly got started ...... Don't look at the concept of "Dynamic Planning" or something, it's actually a relatively computation. If you know what he is doing, you have a clue, and then there is an equation and an idea ......

I also want to read more questions. From a simple start, I want to understand his ideas ...... Pay attention to the following issues when writing your own changes:
1. The premise is to make sure that you are doing dynamic questions ...... After reading this, you will know what type of questions you are facing.
2. The premise is that the idea is correct (I first think about the temporal dimension of the question when I do the question, and then think about the equation based on this,
If you really cannot remember it, You can first look at the problem and understand people's thoughts. Then, don't look at the standard process to make the program ......
3. Note that the array should not be opened too small. Generally, both the left and right are opened a little larger. For example, its data range is 1 ~ 100, the array is 0 ~ 101. This is out-of-bounds defense, because F [0], F [0, 0] will be used when many DP values are assigned.
4. The initial value should be correct, because many other aspects of DP are correct, because the initial value is wrong and cannot be passed ...... (For example, the currency system in USACO)
5. The range of the DP loop should be correct. Generally, the number of statements written in the range should be determined based on the question. (For example, if the window display problem occurs, this question cannot be written this afternoon because the loop is wrong)

There are also many DP questions in USACO, which can be done ......
I hope it will be helpful to you.
I am also a learner. Not all of the above things are correct, but they are useful to me and are my experience. I hope I can study and communicate together and make progress in the future.
QQ: 340131980
1. Resource Question 1
----- Machine allocation problems
F [I, j]: = max (f [I-1, k] + w [I, j-k])

2. Resource Question 2
------ 01 backpack Problems
F [I, j]: = max (f [I-1, j-v] + w, f [I-1, j]);

3. Linear Dynamic Programming 1
----- Simple longest non-descending subsequence
F: = max {f [j] + 1}

4. Splitting problem 1
----- Merge stones
F [I, j]: = min (f [I, k] + f [k + 1, j] + sum [I, j]);

5. Splitting Question 2
----- Polygon partitioning
F [I, j]: = min (f [I, k] + f [k, j] + a [k] * a [j] * );

6. Splitting problem 3
------ Maximum Product
F [I, j]: = max (f [k, J-1] * mult [k, I]);

7. Resource question 3
----- System reliability (full backpack)
F [I, j]: = max {f [I-1, j-c * k] * P [I, x]}

8 ...... the remaining full text>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.