acwing-数学-期望与概率

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
vector<int> w[N];
vector<double> t[N];
double f[N];
double s[N];

double dp(int x)
{
    if (f[x]>=0) return f[x];
    f[x]=0;
    for (int i=0;i<w[x].size();i++)
    {
        f[x]+=(t[x][i]+dp(w[x][i]))/s[x];
    }
    return f[x];
}

int main()
{
    int n,m;
    cin>>n>>m;
    for (int i=0;i<m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        w[a].push_back(b);
        t[a].push_back(c);
        s[a]++;
    }
    
    memset(f,-1,sizeof f);
    
    printf("%.2lf",dp(1));
    
    return 0;
}

在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define endl "\n"
typedef long long ll;
typedef pair<int ,int> PII;
const double INF=1e20;
const int mod=1e9+7;
const int N=14;
int A,B,C,D;
double f[N][N][N][N][5][5];
double dp(int a,int b,int c, int d, int x,int y)
{
	double &v=f[a][b][c][d][x][y];
	if (v>=0) return v;
	int as=a+(x==0)+(y==0);
	int bs=b+(x==1)+(y==1);
	int cs=c+(x==2)+(y==2);
	int ds=d+(x==3)+(y==3);
	if (as>=A&&bs>=B&&cs>=C&&ds>=D) return v=0;
	int sum=a+b+c+d+(x!=4)+(y!=4);
	sum=54-sum;
	if (sum<=0) return v=INF;
	v=1;
	if (a<13) v+=(13.0-a)/sum*dp(a+1,b,c,d,x,y);
    if (b<13) v+=(13.0-b)/sum*dp(a,b+1,c,d,x,y);
    if (c<13) v+=(13.0-c)/sum*dp(a,b,c+1,d,x,y);
    if (d<13) v+=(13.0-d)/sum*dp(a,b,c,d+1,x,y);
	if(x==4)
	{
		double t=INF;
		for (int i=0;i<4;i++)t=min(t,1.0/sum*dp(a,b,c,d,i,y));
		v+=t;
	}
	if (y==4)
	{
		double t=INF;
		for (int i=0;i<4;i++) t=min(t,1.0/sum*dp(a,b,c,d,x,i));
		v+=t;
	}
	return v;
}
int main()
{
	IOS;
	cin>>A>>B>>C>>D;
	memset(f,-1,sizeof f);
	double t=dp(0,0,0,0,4,4);
	if (t>INF/2) t=-1;
	printf("%.3lf\n",t);
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值