Chieh's Blog

Because Of Coding

ZOJ Problem Set - 2974 Just Pour the Water

Chieh posted @ 2015年11月08日 22:07 in NO Answer No Speak , 298 阅读
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
using namespace std;
const int maxn=23;
double ai[maxn];
struct matrix
{
    double e[maxn][maxn];
};
matrix Cal(matrix a,matrix b,int len)
{
    matrix c;
    for(int i=1; i<=len; i++)
    {
        for(int j=1; j<=len; j++)
        {
            c.e[i][j]=0;
            for(int k=1; k<=len; k++)
            {
                c.e[i][j]+=a.e[i][k]*b.e[k][j];
            }
        }
    }
    return c;

}
matrix a,s;
int n;
void exp_pow(int b)
{
    while(b)
    {
        if(b&1)s=Cal(s,a,n);
        a=Cal(a,a,n);
        b>>=1;
    }
}
void init()
{   scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            s.e[i][j]=0;
        }
    }
    for(int i=1; i<=n; i++)
    {
        scanf("%lf",&s.e[1][i]);
    }
    int k,t;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            a.e[i][j]=0;
        }
    }
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&k);
        if(k==0)
        {
            a.e[i][i]=1;
        }
        else
        {
            double fm=1.0/k;
            for(int j=1; j<=k; j++)
            {
                scanf("%d",&t);
                a.e[i][t]=fm;
            }
        }
    }
}
void play()
{
    int k;
    scanf("%d",&k);
    exp_pow(k);
    bool first=1;
    for(int i=1; i<=n; i++)
    {
        if(!first)printf(" ");
        first=0;
        printf("%.2f",s.e[1][i]);
    }
    printf("\n");
}
int T;
int main()
{

    scanf("%d",&T);
    while(T--)
    {
        init();
        play();
    }
//    cout << "Hello world!" << endl;
    return 0;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter