「CF605E」Intergalaxy Trips - 期望 + 贪心

nn个点的完全图,每条边有pi,jp_{i,j}的概率出现,求最优策略下11号点到达nn号点的期望距离

n2000n\le 2000

CF605E

Solution

贪心考虑不难发现,最优策略一定是每次尽量尝试走向期望时间小的点,如果不能走再考虑第二小的,以此类推,直到某个点的期望时间比它大,那么就不往那边走,直接留在原地

dp[x]dp[x]表示xx号点走到nn号点最优的期望时间,aia_i表示dp[x]dp[x]ii小的xx,那么有

dp[ai]=1+j=1idp[aj]pai,ajk=1j1(1pai,ak)dp[a_i] = 1 + \sum_{j = 1}^{i}dp[a_j]\cdot p_{a_i, a_j} \cdot \prod_{k=1}^{j-1}(1-p_{a_i,a_k})

显然a1=na_1 = n,后面的aia_i都可以通过不断找出当前局面最小的dpdp值求出

至于计算dpdp值的话,稍微化简一下变成:

dp[ai]=1+j=1i1dp[aj]pai,ajk=1j1(1pai,ak)1j=1i1(1pai,aj)dp[a_i] = \frac{1 + \sum_{j=1}^{i-1}dp[a_j]\cdot p_{a_i, a_j}\cdot \prod_{k=1}^{j-1}(1-p_{a_i, a_k})}{1 - \prod_{j=1}^{i-1}(1-p_{a_i, a_j})}

分别维护一下分子部分和\prod的部分即可

时间复杂度O(n2)O(n^2)

Summary

这道题思路真的非常简单,但是自己想的时候就是想不清楚。

这种dpdp和贪心的思想以及转化问题的能力还是太薄弱了

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }

inline void proc_status()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) <<endl;
}

template <typename T> T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

const int Maxn = 1000 + 100;

int N, A[Maxn], Vis[Maxn];
double P[Maxn][Maxn], Dp[Maxn], Prod[Maxn], Sum[Maxn];

inline void Solve ()
{
for (int i = 1; i <= N; ++i) Prod[i] = Sum[i] = 1;
A[1] = N, Dp[N] = 0, Vis[N] = 1;
Dp[0] = 1e9;
for (int i = 2; i <= N; ++i)
{
for (int j = 1; j <= N; ++j)
{
if (Vis[j]) continue;
Sum[j] += Dp[A[i - 1]] * P[j][A[i - 1]] * Prod[j];
Prod[j] *= (1.0 - P[j][A[i - 1]]);
Dp[j] = Sum[j] / (1.0 - Prod[j]);
}
int pos = 0;
for (int j = 1; j <= N; ++j) if (!Vis[j] && Dp[j] < Dp[pos]) pos = j;
Vis[A[i] = pos] = 1;
}
printf("%.10lf\n", Dp[1]);
}

inline void Input ()
{
N = read<int>();
for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) P[i][j] = 1.0 * read<int>() / 100;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("E.in", "r", stdin);
freopen("E.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}
感谢你的赞赏!