hdu 4418 Time travel [gause+dp]

Time travel

Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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

题意:一个人在数轴上来回走,以pi的概率走i步i∈[1, m],给定n(数轴长度),m,e(终点),s(起点),d(方向),求从s走到e经过的点数期望

 

解析:设E[x]是人从x走到e经过点数的期望值,显然对于终点有:E[e] = 0

一般的:E[x] = sum((E[x+i]+i) * p[i])(i∈[1, m]) 

(走i步经过i个点,所以是E[x+i]+i)

 

建立模型:高斯消元每个变量都是一个互不相同的独立的状态,由于人站在一个点,还有一个状态是方向!例如人站在x点,有两种状态向前、向后,不能都当成一种状态建立方程,所以要把两个方向化为一个方向从而使状态不受方向的影响

实现:

n个点翻过去(除了头尾两个点~~~)变为2*(n-1)个点,例如:

6个点:012345  --->  0123454321

那么显然,从5开始向右走其实就是相当于往回走

然后方向就由两个状态转化成一个状态的,然后每个点就是只有一种状态了,对每个点建立方程高斯消元即可

 

bfs判断是否可以到达终点;


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 0x7fffffff
#define SUP 0x80000000
#define eps 1e-9
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=100007;

double p[110],mat[210][210];
int xi[210],n,cnt;

void bfs(int s,int M)
{
    queue<int> que;
    mem(xi,-1);
    cnt=0;
    que.push(s);
    xi[s]=cnt++;
    while(!que.empty())
    {

        int cur=que.front();//cout<<cur<<"-"<<xi[cur]<<endl;
        que.pop();
        for(int i=1;i<=M;i++)
        {
            if(fabs(p[i])<eps) continue;
            int tmp=(cur+i)%n;
            if(xi[tmp]==-1)
            {
                xi[tmp]=cnt++;
                que.push(tmp);
            }
        }
    }
}

int gause()
{
    int maxx;
    for(int i=0;i<cnt;i++)
    {
        maxx=i;
        for(int j=i+1;j<cnt;j++)
        {
            if(fabs(mat[maxx][i])<fabs(mat[j][i]))
                maxx=j;
        }
        if(mat[maxx][i]<eps) return 0;
        if(maxx!=i)
        {
            for(int j=0;j<=cnt;j++)
                swap(mat[i][j],mat[maxx][j]);
        }

        for(int j=i+1;j<=cnt;j++) mat[i][j]/=mat[i][i];
        mat[i][i]=1.0;

        for(int j=0;j<cnt;j++)
        {
            if(j==i) continue;
            for(int k=i+1;k<=cnt;k++)
            {
                mat[j][k]-=mat[i][k]*mat[j][i];
            }
            mat[j][i]=0.0;
        }
    }
    return 1;
}

int main()
{
    int T;scanf("%d",&T);
    int N,M,Y,X,D;
    while(T--)
    {
        scanf("%d%d%d%d%d",&N,&M,&Y,&X,&D);
        double pp;
        for(int i=1;i<=M;i++)
        {
            scanf("%lf",&pp);
            p[i]=pp/100;
        }

        if(X==Y)
        {
            printf("0.00\n");
            continue;
        }
        n=2*N-2;
        if(D==1) X=n-X;

        bfs(X,M);
        if(xi[Y]==-1&&xi[n-Y]==-1)
        {
            printf("Impossible !\n");
            continue;
        }

        mem(mat,0);

        for(int i=0;i<=n;i++)
        {
            if(xi[i]==-1) continue;
            if(i==Y||i==n-Y)   //终点状态,即没有后续状态来推,直接标值。
            {
                mat[xi[i]][xi[i]]=1;
                mat[xi[i]][cnt]=0;
                continue;
            }
            mat[xi[i]][xi[i]]=1;
            for(int j=1;j<=M;j++)
            {
                int tmp=(i+j)%n;
                if(xi[tmp]!=-1)
                {
                    mat[xi[i]][xi[tmp]]-=p[j];
                    mat[xi[i]][cnt]+=j*p[j];
                }
            }
        }

        if(!gause())
        {
            printf("Impossible !\n");
        }
        else
        {
            printf("%.2f\n",mat[xi[X]][cnt]);
        }


    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章