HDU 3853 LOOPS 概率dp(水

来源:互联网 发布:方维众筹系统源码 编辑:程序博客网 时间:2024/05/21 06:39

水水过~



#include <stdio.h>#include <cstring>#include <iostream>#include <map>#include <cmath>template <class T>inline bool rd(T &ret) {    char c; int sgn;    if(c=getchar(),c==EOF) return 0;    while(c!='-'&&(c<'0'||c>'9')) c=getchar();    sgn=(c=='-')?-1:1;    ret=(c=='-')?0:(c-'0');    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');    ret*=sgn;    return 1;}template <class T>inline void pt(T x) {    if (x <0) {        putchar('-');        x = -x;    }    if(x>9) pt(x/10);    putchar(x%10+'0');}using namespace std;typedef unsigned long long ull;const double eps = 1e-10;const int N = 1005;int n, m;double mp[N][N][3], dp[N][N];double solve(){dp[n][m] = 0;for(int i = n; i ; i--)for(int j = m; j; j--){if(i == n && j == m)continue;if(fabs(mp[i][j][0] - 1.0) < eps){dp[i][j] = 0;}else{dp[i][j] = mp[i][j][1] * dp[i][j+1] + mp[i][j][2] * dp[i+1][j];dp[i][j] += 2.0;dp[i][j] /= (1 - mp[i][j][0]);}}return dp[1][1];}void input(){for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++)scanf("%lf %lf %lf", &mp[i][j][0], &mp[i][j][1], &mp[i][j][2]);}int main(){    while(cin>>n>>m){        input();        printf("%.3f\n", solve());    }    return 0;}


0 0
原创粉丝点击