程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu4418 Time travel 高斯+期望

hdu4418 Time travel 高斯+期望

編輯:C++入門知識

Time travel
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 519    Accepted Submission(s): 82


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
2012 ACM/ICPC Asia Regional Hangzhou Online

Recommend
liuyiding
 E[x] = sigma((E[x+i]+i) * p)(i∈[1, m])
(走i步經過i個點,所以是E[x+i]+i)
展開就是這樣E=E[i+1]*p1+E[i+2]*p2+……E[i+m]*pm+p[1]*1+p[2]*2……p[m]*m
 
[cpp] 
/*
題意:一個人在數軸上來回走,以pi的概率走i步i∈[1, m],給定n(數軸長度),m,
e(終點),s(起點),d(方向),求從s走到e經過的點數期望.
題意真是晦澀難懂,誤導了我好久,糾結,痛苦。
設E[x]是人從x走到e經過點數的期望值,顯然對於終點有:E[e] = 0
n個點翻過去(除了頭尾兩個點)變為2*(n-1)個點
例如:6個點:012345  --->  0123454321
那麼顯然,從5開始向右走其實就是相當於往回走
然後方向就由兩個狀態轉化成一個狀態的,然後每個點就是只有一種狀態了,對每個點建立方程高斯消元即可
bfs判斷是否可以到達終點
建立方程:
E[i]=E[i+1]*p1+E[i+2]*p2+……E[i+m]*pm+1*p[1]+2*p[2]……m*p[m]
*/ 
#include<iostream> 
#include<cstdlib> 
#include<stdio.h> 
#include<math.h> 
#include<queue> 
#include<memory.h> 
using namespace std; 
int n,m,y,xx,d; 
double p[105]; 
bool vis[210]; 
double a[210][210]; 
const double eps = 1e-12; 
double x[230]; 
bool free_x[230]; 
void debug() 

    for(int i=0;i<=n;i++) 
    for(int j=0;j<=n;j++) 
    { 
        printf("%lf ",a[i][j]); 
        if(j==n) puts(""); 
    } 

int sgn(double x) 

    return (x>eps)-(x<-eps); 

int gauss() 

    int i, j, k; 
    int max_r; 
    int col; 
    double temp; 
    int free_x_num; 
    int free_index; 
    int equ = n,var = n; 
    col = 0; 
    memset(free_x,true,sizeof(free_x)); 
    for (k = 0; k < equ && col < var; k++, col++) 
    { 
        max_r = k; 
        for (i = k + 1; i < equ; i++) 
        { 
            if (sgn(fabs(a[i][col]) - fabs(a[max_r][col]))>0) max_r = i; 
        } 
        if (max_r != k) 
        { 
            for (j = k; j < var + 1; j++) swap(a[k][j], a[max_r][j]); 
        } 
        if (sgn(a[k][col]) == 0 ) 
        { 
            k--; continue; 
        } 
        for (i = k + 1; i < equ; i++) 
        { 
            if (sgn(a[i][col])!=0) 
            { 
                double t = a[i][col] / a[k][col]; 
                for (j = col; j < var + 1; j++) 
                { 
                    a[i][j] = a[i][j] - a[k][j] * t; 
                } 
            } 
        } 
    } 
    for(i=k;i<equ;i++) 
    if(sgn(a[i][col])!=0) {return 0;} 
    if (k < var) 
    { 
        for (i = k - 1; i >= 0; i--) 
        { 
            free_x_num = 0; 
            for (j = 0; j < var; j++) 
            { 
                if ( sgn(a[i][j])!=0 && free_x[j]){ 
                    free_x_num++, free_index = j; 
                } 
            } 
            if(free_x_num>1)    continue; 
            temp = a[i][var]; 
            for (j = 0; j < var; j++) 
            { 
                if (sgn(a[i][j])!=0 && j != free_index) temp -= a[i][j] * x[j]; 
            } 
            x[free_index] = temp / a[i][free_index]; 
            free_x[free_index] = 0; 
        } 
        return var - k; 
    } 
 
    for (i = var - 1; i >= 0; i--) 
    { 
        temp = a[i][var]; 
        for (j = i + 1; j < var; j++) 
        { 
            if (sgn(a[i][j])!=0) temp -= a[i][j] * x[j]; 
        } 
        x[i] = temp / a[i][i]; 
    } 
    return 1; 

int bfs() 

    queue<int>qiqi; 
    qiqi.push(xx); 
    vis[xx]=true; 
    while(!qiqi.empty()) 
    { 
        int u=qiqi.front(); 
        qiqi.pop(); 
        for(int i=1;i<=m;i++) 
        { 
            int v=(u+i)%n; 
            if(sgn(p[i])&&vis[v]==false) 
            { 
                vis[v]=true; 
                qiqi.push(v); 
            } 
        } 
    } 
    if(vis[y]||vis[(n-y)%n]) return 1; 
    else return 0; 

void build() 

        int i,j; 
        double sum = 0; 
        for (i = 1; i <=m;i++) 
        sum += p[i]*i; 
        memset(a, 0, sizeof a); 
        for (i = 0; i < n; ++i) 
        { 
            a[i][i] = 1; 
            if (!vis[i]) 
            { 
                a[i][n] = 1e9; 
                continue; 
            } 
            if (i==y||i==(n-y)%n) 
            { 
                a[i][n] = 0; 
                continue; 
            } 
            a[i][n] = sum; 
            int now = i; 
            for (j = 1; j <=m; ++j) 
            { 
                now++; 
                if (now==n) now = 0; 
                a[i][now] -= p[j]; 
            } 
        } 

int main() 

    int t; 
    scanf("%d",&t); 
    while(t--) 
    { 
        scanf("%d%d%d%d%d",&n,&m,&y,&xx,&d); 
        for(int i=1;i<=m;i++) 
        { 
            int g; 
            scanf("%d",&g); 
            p[i]=1.0*g/100; 
        } 
        if(y==xx) {puts("0.00");continue;} 
        n=2*(n-1); 
        if(d>0) xx=(n-xx)%n; 
        memset(vis,false,sizeof(vis)); 
        if(!bfs()) {puts("Impossible !");continue;} 
        build(); 
       // debug(); 
        int uu=gauss(); 
        if(uu==0)  {puts("Impossible !");continue;} 
        printf("%.2lf\n",x[xx]); 
    } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved