hdu 4418 高斯消元+概率dp

Time travel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2141    Accepted Submission(s): 489


Problem Description

Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to finish a mission by traveling through time with the Time machine. The Time machine can take agent K to some point (0 to n-1) on the timeline and when he gets to the end of the time line he will come back (For example, there are 4 time points, agent K will go in this way 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, ...). But when agent K gets into the Time machine he finds it has broken, which make the Time machine can't stop (Damn it!). Fortunately, the time machine may get recovery and stop for a few minutes when agent K arrives at a time point, if the time point he just arrive is his destination, he'll go and finish his mission, or the Time machine will break again. The Time machine has probability Pk% to recover after passing k time points and k can be no more than M. We guarantee the sum of Pk is 100 (Sum(Pk) (1 <= k <= M)==100). Now we know agent K will appear at the point X(D is the direction of the Time machine: 0 represents going from the start of the timeline to the end, on the contrary 1 represents going from the end. If x is the start or the end point of the time line D will be -1. Agent K want to know the expectation of the amount of the time point he need to pass before he arrive at the point Y to finish his mission.
If finishing his mission is impossible output "Impossible !" (no quotes )instead.
 

Input
There is an integer T (T <= 20) indicating the cases you have to solve. The first line of each test case are five integers N, M, Y, X .D (0< N,M <= 100, 0 <=X ,Y < 100 ). The following M non-negative integers represent Pk in percentile.
 

Output
For each possible scenario, output a floating number with 2 digits after decimal point
If finishing his mission is impossible output one line "Impossible !" 
(no quotes )instead.
 

Sample Input
  
  
2 4 2 0 1 0 50 50 4 1 0 2 1 100
 

Sample Output
  
  
8.14 2.00
 

Source
 

这题是典型的可以无限递归下去的概率模型,每种状态设为F[i],列出转移方程求解即可。

走的路径是0->n-1->0,由于有方向要考虑每个点(除了第一个和最后一个)拆成两个点处理,共2*n-2个点(那么终点可能变成两个,起始点仍然是一个因为初始方向确定了),那么相当于0-2n-2不断循环走。

比较坑的是由于pk是非负整数,也就是可能等于0,意味着有些点可能根本无法到达,所以事先bfs搜索一遍,标记出哪些点可以到达,然后再建立方程做高斯消元。


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

typedef pair<int, int > P;
#define fir first
#define sec second

const int maxn=210;
const double eps=1e-9;
double a[maxn][maxn], pk[maxn];
int n,m,x,y,d;
int N,yy;



int gauss(int equ, int var)
{
    int k,col;
    for(k=0, col=0; k<equ && col<var; k++,col++){
        int maxr=k;
        for(int i=k+1; i<equ; i++)
            if(fabs(a[i][col])>fabs(a[maxr][col]))
                maxr=k;

        if(fabs(a[maxr][col])<eps) return 0;

        if(k!=maxr){
            for(int j=col; j<var+1; j++)
                swap(a[k][j], a[maxr][j]);

        }

        for(int j=col+1; j<var+1; j++)
            a[k][j]/=a[k][col];
        a[k][col]=1.0;
        for(int i=0; i<equ; i++){
                if(i!=k){
                    for(int j=col+1; j<var+1; j++) a[i][j]-=a[k][j]*a[i][col];
                    a[i][col]=0.0;
                }

        }
    }
    return 1;
}

int vis[maxn];
void bfs(int s)
{
    memset(vis, 0, sizeof(vis));
    queue<int> que;
    que.push(s);
    vis[s]=1;

    while(!que.empty()){
        int u=que.front(); que.pop();

        for(int i=1; i<=m; i++){
            if(fabs(pk[i])<eps) continue;
            int v=(u+i)%N;
            if(vis[v]) continue;
            vis[v]=1;
            if(v!=y && v!=yy)
            que.push(v);
        }
    }

}

int main()
{
    int T;
    cin>>T;
    while(T--){
        scanf("%d%d%d%d%d", &n, &m, &y, &x, &d);
        for(int i=1; i<=m; i++) scanf("%lf", pk+i);
        if(x==y){
            puts("0.00");
            continue;
        }

        N=2*n-2;
        int var=N;
        yy=(n-y+n-2)%N;
        if(d==1){
            x=(n-x+n-2)%N;
        }

        bfs(x);

        memset(a, 0, sizeof(a));
        for(int i=0; i<var; i++){
            a[i][i]=100.0;
            if(!vis[i] || i==y || i==yy) continue;
            for(int k=1; k<=m; k++){
                int v=(i+k)%N;
                if(!vis[v]) continue;

                a[i][var]+=pk[k]*k;
                if(v!=y && v!=yy)
                  a[i][v]-=pk[k];
            }
        }

        int res=gauss(var,var);
        if(res==0 || (!vis[y] && !vis[yy]))
            puts("Impossible !");
        else
            printf("%.2lf\n", a[x][var]);

    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值