Atcoder Beginner Contest 144 F- Fork the Road(概率DP/期望DP)

Problem Statement

There is a cave consisting of NN rooms and MM one-directional passages. The rooms are numbered 11 through NN .

Takahashi is now in Room 11 , and Room NN has the exit. The ii -th passage connects Room sisi and Room titi (sisi < titi ) and can only be traversed in the direction from Room sisi to Room titi . It is known that, for each room except Room NN , there is at least one passage going from that room.

Takahashi will escape from the cave. Each time he reaches a room (assume that he has reached Room 11 at the beginning), he will choose a passage uniformly at random from the ones going from that room and take that passage.

Aoki, a friend of Takahashi‘s, can block one of the passages (or do nothing) before Takahashi leaves Room 11 . However, it is not allowed to block a passage so that Takahashi is potentially unable to reach Room NN .

Let EE be the expected number of passages Takahashi takes before he reaches Room NN . Find the value of EE when Aoki makes a choice that minimizes EE .

Constraints

  • 2≤N≤6002≤N≤600
  • N−1≤M≤N(N−1)2N−1≤M≤N(N−1)2
  • si<tisi<ti
  • If i!=ji!=j , (si,ti)≠(sj,tj)(si,ti)≠(sj,tj) . (Added 21:23 JST)
  • For every v=1,2,...,N−1v=1,2,...,N−1 , there exists ii such that v=siv=si .

Input

Input is given from Standard Input in the following format:

NN

 MM


s1s1

 t1t1


::


sMsM

 tMtM


Output

Print the value of EE when Aoki makes a choice that minimizes EE . Your output will be judged as correct when the absolute or relative error from the judge‘s output is at most 10−610−6 .

Sample Input 1

4 6
1 4
2 3
1 3
1 2
3 4
2 4

Sample Output 1

1.5000000000

If Aoki blocks the passage from Room 11 to Room 22 , Takahashi will go along the path 134 with probability 1212 and 14 with probability 1212 . E=1.5E=1.5 here, and this is the minimum possible value of EE .

Sample Input 2

3 2
1 2
2 3

Sample Output 2

2.0000000000

Blocking any one passage makes Takahashi unable to reach Room NN , so Aoki cannot block a passage.

Sample Input 3

10 33
3 7
5 10
8 9
1 10
4 6
2 5
1 7
6 10
1 4
1 3
8 10
1 5
2 6
6 9
5 6
5 8
3 6
4 8
2 7
2 9
6 7
1 2
5 9
6 8
9 10
3 9
7 8
4 5
2 10
5 7
3 5
4 7
4 9

Sample Output 3

3.0133333333https://blog.csdn.net/wjl_zyl_1314/article/details/102811697?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task题意在这总算搞明白怎么写了...重点有两个,一个是转移方程。概率DP的一大特点是如果最终状态明确,一般选择从最终状态逆推,所以这里dp[i]表示从i到终点的期望道路数,转移方程为dp[i]=1+(Σdp[k])/len,其中k为i这个点的出边的终点(满足逆推,即由终状态到初始状态),len为i这个点的出边数(结合概率考虑一下,len条道路每条被选择的概率为1/len),这个+1也很好理解,因为i到k必定会走一条路。这是不考虑删边的情况。那么删边该怎么办呢?只需要对于枚举每个点考虑删除它的出边终点里期望最大的那个就可以了(因为逆推,i这个点之后的状态都确定了)。代码写法参考了上面的博客QwQ。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#define N 605
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
vector<int>v[N];
double dp[N]={0};
double process(int p)//对于p这个顶点删除其连着的最大的出边
{
    int i,j;
    for(i=1;i<=n;i++)dp[i]=0.0;
    dp[n]=0.0;//特别强调一下
    for(i=n-1;i>=1;i--)
    {
        if(v[i].size()==1&&p==i)//i出度为1且 要删它的边
        {
            dp[i]=INF;//删了以后必不能到达 所以设置为INF
            continue;
        }
        double len=v[i].size()-(p==i);//如果i是枚举到的p的话要减1 因为删边了
        dp[i]=1.0;//先加上1
        double mmax=0;//。找到最大的,如果p==i的话要在最后减掉
        for(j=0;j<v[i].size();j++)
        {
            double temp=dp[v[i][j]];//获取每个出边终点的dp值
            mmax=max(mmax,temp);
            dp[i]+=temp*1.0/len;//结合转移方程理解
        }
        if(i==p)dp[i]-=mmax*1.0/len;
    }
    //cout<<dp[1]<<endl;
    return dp[1];
}
int main()
{
    cin>>n>>m;
    int i;
    for(i=1;i<=m;i++)
    {
        int s,t;
        scanf("%d%d",&s,&t);
         v[s].push_back(t);//出边
    }
    double ans=INF;
    for(i=n;i>=1;i--)
    {
        ans=min(ans,process(i));
    }
    printf("%.10lf",ans);//注意输出格式
    return 0;
}

原文地址:https://www.cnblogs.com/lipoicyclic/p/12547069.html

时间: 2024-11-08 00:40:27

Atcoder Beginner Contest 144 F- Fork the Road(概率DP/期望DP)的相关文章

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

AtCoder Beginner Contest 144 题解

传送门 $cf$ 自闭了,打 $abc$ 散散心 A - 9x9 ...这个有什么好讲的吗,题目看懂就会做了 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; typedef long long ll; inline int read() { int x=0,f=1; char

AtCoder Beginner Contest 132 F - Small Products

数 sqrt 缩小范围 整除分块 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <iostream> 8 using namespace std; 9 #define ll long long 10 1

AtCoder Beginner Contest 144:E.Gluttony【题解】

题目链接:https://atcoder.jp/contests/abc144/tasks/abc144_e 一道很简单的二分加贪心,但我在比赛时没过.因为我输入错了,它竟然加上样例还有6个点是对的,于是我查了半小时都没发现这件事,到最后只能怀疑是自己想法错了放弃. (我不管我不管,是数据的锅!)至于难度的话应该有T1无T2吧 首先二分答案sum. 现在的问题是你要将A,F数组一一对应,如果A[i]*F[i]>SUM,K-=A[i]-SUM/F[i],看K最后是否小于0. 然后你就会发现其实只要

AtCoder Beginner Contest 133 F - Colorful Tree

题意:给出一棵n个点的树,每条边有颜色和边长两个属性,n个询问,每次询问把颜色x的边的边长变为y问u到v的路径长度是多少,询问之间独立. 解法:这道题蛮有意思的.解法参考https://www.cnblogs.com/Tieechal/p/11185912.html这位大佬的,这里说下我的理解. 对于每组询问(x,y,u,v)答案比较显然就是dist(u,v)+(sumlen[x]-sumcnt[x]*y),但是这道题在线不好做我们考虑离线做.但是答案的式子是设计到两个点的,怎么才能离线做呢?这

AtCoder Beginner Contest 153 F - Silver Fox vs Monster

题目链接 https://atcoder.jp/contests/abc153/tasks/abc153_f 题意 : 在坐标轴上有一些怪兽,每个怪兽有对应的生命值hi,你可以对他们进行炮击,你的每次炮击可以队该点前后D范围内的怪兽造成A的伤害,问最少要炮击多少次. 我的最初的想法是先排序,扫到最左边的怪兽,先进行炮击,把他打死,然后记录炮击了多少次,然后把其后2d距离的怪兽都炮击一下发现超时 代码如下: inline ll read(){ ll s=0,w=1; char ch=getchar

AtCoder Beginner Contest 128 F - Frog Jump

题意 有一只青蛙,有\(0, 1, \cdots, N - 1\)个荷叶.每个荷叶上有权值\(s_i\). 选定\(A\), \(B\),初始分数为\(0\). 当前位置为\(x\): 对于\(y = x + A\): 如果\(y = N - 1\),游戏结束. 如果\(y \neq N - 1\),但是\(y\)这个荷叶存在,那么分数增加\(s_i\),并且这片荷叶消失. 如果\(y \neq N - 1\),但是\(y\)这个荷叶不存在,那么分数减去\(10^{100}\),游戏结束. 对于

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one