您的位置:首页 > 其它

hdoj 1437 天气情况 【概率dp】

2016-04-04 16:31 375 查看
题目链接:hdoj 1437 天气情况

天气情况

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 714 Accepted Submission(s): 291

Problem Description

如果我们把天气分为雨天,阴天和晴天3种,在给定各种天气之间转换的概率,例如雨天转换成雨天,阴天和晴天的概率分别为0.4,0.3,0.3.那么在雨天后的第二天出现雨天,阴天和晴天的概率分别为0.4,0.3,0.3.现在给你今天的天气情况,问你n天后的某种天气出现的概率.

Input

我们这里假设1,2,3分别代表3种天气情况,Pij表示从i天气转换到j天气的概率.

首先是一个数字T表示数据的组数.

每组数据以9个数开始分别是P11,P12,P13,……,P32,P33,接着下一行是一个数字m,表示提问的次数。每次提问有3个数据,i,j,n,表示过了n天从i天气情况到j天气情况(1<=i,j<=3 1<=n<=1000)。

Output

根据每次提问输出相应的概率(保留3位小数)。

Sample Input

1

0.4 0.3 0.3 0.2 0.5 0.3 0.1 0.3 0.6

3

1 1 1

2 3 1

1 1 2

Sample Output

0.400

0.300

0.250

Hint:如果GC提交不成功,可以换VC试试

思路:dp[i][j][k]表示第i天j天气到k天气的概率。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
double P[4][4];
double dp[4][4][1010];
int main()
{
int t; scanf("%d", &t);
while(t--) {
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
scanf("%lf", &P[i][j]);
dp[i][j][1] = P[i][j];
}
}
for(int i = 2; i <= 1000; i++) {
for(int s = 1; s <= 3; s++) {
for(int t = 1; t <= 3; t++) {
dp[s][t][i] = 0;
for(int j = 1; j <= 3; j++) {
dp[s][t][i] += dp[s][j][i-1] * P[j][t];
}
}
}
}
int m; scanf("%d", &m);
while(m--) {
int x, y, n; scanf("%d%d%d", &x, &y, &n);
printf("%.3lf\n", dp[x][y]
);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: