[2019 Nanjing D network game title] Robots

Topic Link

Desperate ing !!!

Engage in three hours of D, the end did not before, eat when suddenly thought wrong, altered, pass by Orz.

Unfortunately, the topology seeking !! wrong order, to use a [n] -> a [1], with the result became n-> 1. To be teammates Masi QAQ

Further examples are 1-n exactly topology sequence (


Question is intended that the DAG from 1 to n, each time there is x / (x + 1) is the probability of the point of attachment, 1 / (x + 1) is the probability of standing still, (point X is connected number). Each move to a day, each time moving the current day will consume much energy values, the energy required to reach a desired point n consumption.

Feel quite outset, the desired DP DAG running, start and the expected number of days, pitted desired energy, provided DP [i] n the number of days to the desired point is at point i, the initial dp [n] = 0, to metastasis $ dp [i] = \ tfrac {1} {x + 1} * \ sum dp [j] + \ tfrac {1} {x + 1} * dp [i] + 1 $ j-> i edged.

Then rerun desired energy, DP2 is [i] n i to a desired energy point, initially dp2 [n] = 0, the transfer of $ dp2 [i] = \ tfrac {1} {x + 1} * \ sum dp2 [j] + \ tfrac {1} {x + 1} * dp2 [i] + dp [i] $ j-> edged.

Code extremely ugly look sorry.

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 4e5 + 10;
 4 struct node {
 5     int s, e, next;
 6 }edge[maxn], edge2[maxn];
 7 int head[maxn], head2[maxn], len, len2;
 8 void init() {
 9     memset(head, -1, sizeof(head));
10     memset(head2, -1, sizeof(head2));
11     len = len2 = 0;
12 }
13 void add(int s, int e) {
14     edge[len].s = s;
15     edge[len].e = e;
16     edge[len].next = head[s];
17     head[s] = len++;
18 }
19 void add2(int s, int e) {
20     edge2[len2].s = s;
21     edge2[len2].e = e;
22     edge2[len2].next = head2[s];
23     head2[s] = len2++;
24 }
25 double dp[maxn], dp2[maxn];
26 int in[maxn], a[maxn], num[maxn];
27 void solve(int n) {
28     int lens = 0;
29     queue<int>q;
30     for (int i = 1; i <= n; i++)
31         if (in[i] == 0) q.push(i);
32     while (!q.empty()) {
33         int p = q.front(); q.pop();
34         a[++lens] = p;
35         for (int i = head[p]; i != -1; i = edge[i].next) {
36             int y = edge[i].e;
37             in[y]--;
38             if (in[y] == 0)
39                 q.push(y);
40         }
41     }
42 }
43 int main() {
44     int t;
45     scanf("%d", &t);
46     while (t--) {
47         int n, m, x, y;
48         scanf("%d%d", &n, &m);
49         init();
50         for (int i = 1; i <= n; i++)
51             in[i] = 0, num[i] = 0, dp2[i] = dp[i] = 0;
52         for (int i = 1; i <= m; i++)
53             scanf("%d%d", &x, &y), add(x, y), add2(y, x), in[Y] ++ ; // save each of a forward-reverse FIG run forward topology, a desired reverse seek
 54 is          Solve (n-); // find Sequence Topological
 55          DP [A [n-]] = 0 ;
 56 is          for ( int X = n-; X> = . 1 ; x-- ) {// dp desired daily push down
 57 is              IF dp [A [X]] = NUM + [A [X]] (A [X] = n-!) + . 1 , DP [a [X]] / = NUM [a [X]]; // NUM [i] is the i point of attachment points
 58              NUM [a [X]] = 0 ;
 59              for ( int i = head2 The [A [X]]; I = -! . 1 ; I = Edge2 [I] .next) {
 60                  int Y = Edge2 [I] .E;
 61 is                 dp[y] += dp[a[x]];
62                 num[y]++;
63             }
64         }
65         dp2[n] = 0;
66         for (int x = n; x >= 1; x--) {
67             if (a[x] != n) dp2[a[x]] += (num[a[x]] + 1) * dp[a[x]], dp2[a[x]] /= num[a[x]];
68             for (int i = head2[a[x]]; i != -1; i = edge2[i].next) {
69                 int y = edge2[i].e;
70                 dp2[y] += dp2[a[x]];
71                 num[y]++;
72             }
73         }
74         printf("%.2lf\n", dp2[1]);
75     }
76 }

 

Guess you like

Origin www.cnblogs.com/sainsist/p/11443200.html