2019南京ICPC网络赛 D

Robots

**
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 1) whose in-degree is equal to 0,
and there is only one node (node n) whose out-degree is equal to 0. And there are no multiple edges in the graph.
Input
The first line contains one integer T (1≤T≤10)
For each case,the first line contains two integers n (2≤n≤10^5) and m (1≤m≤2×10^5), the number of nodes and the number of edges, respectively.
Each of the next mm lines contains two integers u and v (1≤u,v≤n) denoting a directed edge from u to v.
It is guarenteed that ∑n≤4×10^5, and ∑m≤5×10^5.
Output
Output T lines.Each line have a number denoting the expected durability consumption when the robot arrives at node n.
Please keep two decimal places.
样例输入
1
5 6
1 2
2 5
1 5
1 3
3 4
4 5
样例输出
9.78
**

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
vector<int>v[101000],w[101000];
int sum[101000];
double dp[101000],g[101000];
int n,m;
void dfs(){
 double s;
 int i;
 queue<int>q,qq;
 q.push(n);
 while(!q.empty()){
  int t=q.front();
  qq.push(t);
  q.pop();
  s=(double)w[t].size();
  for(i=0;i<v[t].size();i++){
   int to=v[t][i];
   sum[to]--;
   if(sum[to]==0)
   q.push(to);
  }
  if(t==n)
  dp[t]=0;
  else{
   dp[t]=1.0+1.0/s;
   for(i=0;i<w[t].size();i++){
    int to=w[t][i];
    dp[t]+=1.0/s*dp[to];
   }
  }
  //cout<<dp[t]<<" "<<t<<"\n";
 }
 while(!qq.empty()){
  int t=qq.front();
  qq.pop();
  s=(double)w[t].size();
  if(t==n)
  g[t]=0;
  else{
   g[t]=(1.0+1.0/s)*dp[t];
   for(i=0;i<w[t].size();i++){
    int to=w[t][i];
    g[t]+=g[to]*1.0/s;
   }
  }
  //cout<<g[t]<<" "<<t<<"\n";
 }
}
int main(void){
 int T,i,a,b;
 cin>>T;
 while(T--){
  scanf("%d%d",&n,&m);
  for(i=1;i<=n;i++){
   v[i].clear();
   w[i].clear();
   sum[i]=0;
   dp[i]=0;
   g[i]=0;
  }
  for(i=0;i<m;i++){
   scanf("%d%d",&a,&b);
   v[b].push_back(a);
   sum[a]++;
   w[a].push_back(b);
  }
  dfs();
  printf("%.2f\n",g[1]);
 }
 return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值