-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkov Chain.cpp
More file actions
42 lines (38 loc) · 981 Bytes
/
Markov Chain.cpp
File metadata and controls
42 lines (38 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<bits/stdc++.h>
using namespace std;
double x[50][50],ans[50][50];
double p[50][50],pk[50][50],pk1[50][50];
int main()
{
//freopen("in.txt","r",stdin);
int m,n,K;
cin>>m>>n;
for(int i=1;i<=m;i++) cin>>x[1][i];
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
cin>>p[i][j];
for(int i=1;i<=m;++i)
for(int j=1;j<=n;j++)
for(int k=1;k<=m;k++)
pk[i][j] += p[i][k]*p[k][j];
cin>>K;
K-=2;
while(K--)
{
for(int i=1;i<=m;++i)
for(int j=1;j<=n;j++)
for(int k=1;k<=m;k++)
pk1[i][j] += pk[i][k]*p[k][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
pk[i][j] = pk1[i][j];
memset(pk1,0,sizeof(pk1));
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
ans[1][i]+=x[1][j]*pk[j][i];
for(int i=1;i<=n;i++)
cout<<ans[1][i]<<" ";
cout<<endl;
return 0;
}