2019年 ACM/ICPC 亞洲區域賽(南京)網絡賽D. Robots(期望dp)

Given a directed graph with no loops which starts at node 11 and ends at node nn.

There is a robot who starts at 11, and will go to one of adjacent nodes or stand still with equal probability every day. Every day the robot will have durability consumption which equals to the number of passed days.

Please calculate the expected durability consumption when the robot arrives at node nn.

It is guaranteed that there is only one node (node 11) whose in-degree is equal to 00, and there is only one node (node nn) whose out-degree is equal to 00. And there are no multiple edges in the graph.

Input

The first line contains one integer T (1 \le T \le 10)T(1≤T≤10)

For each case,the first line contains two integers n (2 \le n \le 10^5)n(2≤n≤105) and m (1 \le m \le 2 \times 10^5)m(1≤m≤2×105), the number of nodes and the number of edges, respectively.

Each of the next mm lines contains two integers uu and vv (1 \le u, v \le n)(1≤u,v≤n) denoting a directed edge from uu to vv.

It is guarenteed that \sum n \le 4\times 10^5∑n≤4×105, and \sum m \le 5 \times 10^5∑m≤5×105.

Output

Output TT lines.Each line have a number denoting the expected durability consumption when the robot arrives at node nn.

Please keep two decimal places.

樣例輸入複製

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

樣例輸出複製

9.78

題意:

給你一個有向無權圖,保證入度、出度爲0的點分別爲1號,n號節點。如果當前在節點u,已經走了i分鐘,那麼下一分鐘可以等概率的留在原地,或者走向它指向的一個點(走向每個點的概率都是相等的,也就是說假設u點的出度爲chu[u],那麼它留在原地和走向它指向的一個點的概率均爲1/(chu[u]+1)),花費爲i+1。

求1號點走到n號點的花費的期望。

也就是假設從1號點走到n號點的期望時間爲E分鐘,則答案爲E(E+1)/2。這個式子可以分成兩部分,即 E*E/2 + E/2。 

因此我們設dp[u]爲u節點走到n節點的期望時間,dp2[u]爲u節點走到n節點的平方時間的期望。

根據題意很容易得到

dp[u]=\frac{1}{chu[u]+1}*(dp[u]+1+\sum(dp[v]+1)),\left ( u,v \right )\epsilon E 

其中E爲邊集。

dp數組即E(x)。根據期望公式,dp2數組即E(x*x)

而E[(x+1)*(x+1)]=E(x*x+2*x+1)=E(x*x)+2*E(x)+1,因此有

dp_{2}[u]=\frac{1}{chu[u]+1}*(dp_{2}[u]+2*dp[u]+1+\sum(dp_{2}[v]+2*dp[v]+1)), \left ( u,v \right )\epsilon E

其中E爲邊集。

將兩個公式化簡,可以得到

dp[u]=\frac{1}{chu[u]}*(1+\sum(dp[v]+1)),\left ( u,v \right )\epsilon E

dp_{2}[u]=\frac{1}{chu[u]}*(2*dp[u]+1+\sum(dp_{2}[v]+2*dp[v]+1)), \left ( u,v \right )\epsilon E

於是將圖反建,按照拓撲排序的順序進行狀態轉移即可。

PS:(賽場上dp2數組公式寫錯了,一直沒過樣例,菜cry...)

代碼:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<iomanip>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 100000+5;
using namespace std;

int S,T;
int in[N],out[N];
double dp[N],dp2[N];
double p[N],pp[N];

vector<int> G[N];
void Topsort(int n) {
    stack<int> St;
    St.push(S);

    while(!St.empty()) {
        int x=St.top();
        St.pop();

        for(int i=0; i<G[x].size(); i++) {
            int y=G[x][i];
            in[y]--;

            dp[y]+=p[y]*(dp[x]+1.0);
            dp2[y]+=p[y]*(dp2[x]+2*dp[x]+1.0);

            if(in[y]==0)
            {
                dp2[y]+=p[y]*(2.0*dp[y]+1);
                St.push(y);
            }
        }
    }
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m;
        scanf("%d%d",&n,&m);

        memset(dp,0,sizeof(dp));
        memset(dp2,0,sizeof(dp2));
        memset(out,0,sizeof(out));
        memset(in,0,sizeof(in));
        for(int i=0;i<=n;i++)
            G[i].clear();
        for(int i=1;i<=m;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            G[y].push_back(x);
            in[x]++;
            out[y]++;
        }
        for(int i=1;i<=n;i++)
        if(in[i]!=0)
        {
            p[i]=1.0/in[i];
            dp[i]+=p[i];
        }

        for(int i=1;i<=n;i++){
            if(out[i]==0)
                T=i;
            if(in[i]==0)
                S=i;
        }

        Topsort(n);
        //for(int i=1;i<=n;i++)
        //cout<<dp[i]<<" "<<dp2[i]<<endl;
        double res=dp[T]/2.0+dp2[T]/2.0;
        cout<<fixed<<setprecision(2)<<res<<endl;
    }
    return 0;
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章