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
hdu 4418 高斯消元+概率dp_第1张图片

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
 

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

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

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


#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef pair 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; kfabs(a[maxr][col]))
                maxr=k;

        if(fabs(a[maxr][col]) 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])>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




你可能感兴趣的:(数学)