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;
}


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