《TZOJ1546》

挺好的一个题,就是数据拉胯~,一开始暴力都能水过去..

暴力:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,int> pii;
const int N = 1005;
const int M = 1e6+5;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e9
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;

double a[25],b[25];
vector<int> vec[25];
int main()
{
    int ca;ca = read();
    while(ca--)
    {
        int n;n = read();
        for(rg int i = 1;i <= n;++i) cin >> a[i];
        for(rg int i = 1;i <= n;++i) vec[i].clear();
        for(rg int i = 1;i <= n;++i)
        {
            int k;k = read();
            if(k == 0) vec[i].push_back(i);
            while(k--)
            {
                int x;x = read();
                vec[i].push_back(x);
            }
        }
        int m;m = read();
        while(m--)
        {
            memset(b,0,sizeof(b));
            for(rg int i = 1;i <= n;++i)
            {
                int len = vec[i].size();
                double ma = a[i] / len;
                for(rg int j = 0;j < len;++j) b[vec[i][j]] += ma;
            }
            for(rg int i = 1;i <= n;++i) a[i] = b[i];
        }
        for(rg int i = 1;i <= n;++i) printf("%.2f%c",a[i],i == n ? '
' : ' ');
    }
   // system("pause");
    return 0;
}
View Code

题意就是需要注意一点:每个水杯倒水都是同时开始的,也就是每个水杯的水都是由上一分钟的所有状态转移来的。

正解:矩阵快速幂。

因为每个水杯都是由上一分钟状态转移来的,所以可以矩阵快速幂。

可以发现,每个水杯都可以看成是由上一个状态的各个水杯*1/k转移来。

这里的1/k是一个定值,那么我们就可以将它放到转移矩阵中,处理出最后的转移矩阵,然后让原始矩阵乘上即可得。

这里有一个坑点:如果k = 0,那么这个水杯应该看成是自己倒给自己,这样才能保证最后有水。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,int> pii;
const int N = 1005;
const int M = 1e6+5;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e9
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;

double a[25];
struct Mat{
    int len;
    double m[25][25];
    Mat operator * (const Mat a)const{
        Mat c;memset(c.m,0,sizeof(c.m));
        c.len = a.len;
        for(rg int i = 1;i <= c.len;++i)
            for(rg int j = 1;j <= c.len;++j)
                for(rg int k = 1;k <= c.len;++k) c.m[i][j] += m[i][k]*a.m[k][j];
        return c;
    }
};
Mat Mat_Mul(Mat a,int b)
{
    Mat res;memset(res.m,0,sizeof(res.m));
    res.len = a.len;
    for(rg int i = 1;i <= res.len;++i) res.m[i][i] = 1;
    while(b)
    {
        if(b&1) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
int main()
{
    int ca;ca = read();
    while(ca--)
    {
        int n;n = read();
        for(rg int i = 1;i <= n;++i) cin >> a[i];
        Mat ans;memset(ans.m,0,sizeof(ans.m));
        ans.len = n;
        for(rg int i = 1;i <= n;++i)
        {
            int k;k = read();
            if(k == 0) ans.m[i][i] = 1.0;
            double ff = 1.0 / double(k);//k在while中--会变.
            while(k--)
            {
                int x;x = read();
                ans.m[x][i] = ff;
            }
        }
        int m;m = read();
        ans = Mat_Mul(ans,m);
        Mat tmp;memset(tmp.m,0,sizeof(tmp.m));
        tmp.len = n;
        for(rg int i = 1;i <= n;++i) tmp.m[i][1] = a[i];
        tmp = ans * tmp;
        for(rg int i = 1;i <= n;++i) printf("%.2f%c",tmp.m[i][1],i == n ? '
' : ' ');
    }
    system("pause");
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zwjzwj/p/13776496.html