hdu 4418 概率dp + gauss消元

来源:互联网 发布:台湾网络枪店 编辑:程序博客网 时间:2024/03/29 14:22

Time travel

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


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
24 2 0 1 050 504 1 0 2 1100
 

Sample Output
8.142.00
 

Source
2012 ACM/ICPC Asia Regional Hangzhou Online
 
题目:给出一个数轴,有一个起点和一个终点,某个人可以走1,2,3……m步,每一种情况有一个概率,初始有一个方向,走到头则返回,问到达终点的期望步数为多少。

令dp[i]为当前点到目标点的期望距离,则

dp[i] = (dp[i+j] + j) * p[j],j = 1..m;

dp[ed] = 0

列出方程然后gauss消元

代码:

#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <functional>#include <sstream>#include <iomanip>#include <cmath>#include <cstdlib>#include <ctime>typedef long long ll;//#pragma comment(linker, "/STACK:1024000000,1024000000")  //手动扩栈#define INF 1e9#define maxn 220#define maxm 100086+10#define mod 1000000007#define eps 1e-7#define PI acos(-1.0)#define rep(i,n) for(int i=0;i<n;i++)#define rep1(i,n) for(int i=1;i<=n;i++)#define scan(n) scanf("%d",&n)#define scanll(n) scanf("%I64d",&n)#define scan2(n,m) scanf("%d%d",&n,&m)#define scans(s) scanf("%s",s);#define ini(a) memset(a,0,sizeof(a))#define out(n) printf("%d\n",n)using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1double a[maxn][maxn],x[maxn],b[maxn];bool gauss(int n){int index;rep(i,n){double m = fabs(a[i][i]);index = i;for(int j = i+1;j < n; j++){if(fabs(a[j][i]) > m){m = fabs(a[j][i]);index = j;}}if(m < eps) return false;if(index != i){swap(b[i],b[index]);rep(k,n) swap(a[i][k],a[index][k]);}for(int k = i+1;k < n; k++){double d = a[k][i]/a[i][i];b[k] -= b[i] * d;for(int p = i;p < n; p++) a[k][p] -= a[i][p] * d;}}for(int i = n-1; i >= 0; i--){for(int j = i + 1;j < n; j++) b[i] -= x[j] * a[i][j];x[i] = b[i] / a[i][i];}return true;}int n,m;int D;double p[maxn];int id[maxn];int cnt;int st,ed;void bfs(int st){queue<int> q;q.push(st);cnt = 0;id[st] = cnt++;while(!q.empty()){int u = q.front();q.pop();rep1(i,m){int v = (u+i) % n;if(id[v] != -1 || fabs(p[i]) < eps) continue;id[v] = cnt++;q.push(v);}}}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);//  freopen("out.txt","w",stdout);#endifint T;cin>>T;while(T--){scanf("%d%d%d%d%d",&n,&m,&ed,&st,&D);rep1(i,m){scanf("%lf",&p[i]);p[i] /= 100;}if(st == ed) {puts("0.00");continue;}n = 2 * n - 2;if(D == 1) st = n - st;memset(id,-1,sizeof(id));bfs(st);if(id[ed] == -1 && id[n-ed] == -1){ puts("Impossible !"); continue;}ini(a);ini(b);ini(x);rep(i,n){int u = id[i];if(u == -1) continue;if(i == ed || i == n - ed){a[u][u] = 1;b[u] = 0;continue;}a[u][u] = 1;rep1(j,m){int v = id[(i+j) % n];if(v == -1) continue;a[u][v] -= p[j];b[u] += p[j] * j;}}if(gauss(cnt))printf("%.2lf\n",x[id[st]]);elseputs("Impossible !");}return 0;}

0 0