Time travel HDU - 4418(高斯消元)
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.
InputThere 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.
OutputFor 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
一个黑衣人在0到n-1的n个时间点里无限穿梭,他需要从 x 位置到 y 位置,时间穿梭器每传送 k 次,最多M次,就有 Pk 的概率停下一次,如果停下的位置正好是 y, 那么黑衣人到达目的地。
然后给出的 N 个点,M次传送次数,目的地y,出发地x,方向D, D为 0 时从左到右走,D为 1 时,从右往左走。
对于方向,可以把 N 个点翻倍,比如 N = 5,可以看成0 1 2 3 4 5 4 3 2 1, 假设我x = 1, D = 0,那么就是从下标 1 开始走,如果x = 1, D = 1,那么我就是从下标 2*n-2-1 开始走,并且这么走,都只要看成往右走。
然后对开始位置进行 bfs ,把可以从开始位置走的点找出来,并且对这些点标号。
然后对于列方程。令dp[i] 表示从 i 位置走出的期望。
1. i == y, dp[i] = 0.
2. i != y, dp[i] = Σ((dp[i+j] + j) * p[j])
= Σ(dp[i+j] * p[j]) + p[j] * j
即 dp[i] - Σ(dp[i+j] * p[j]) = p[j] * j
通过这个式子对可以走到的点列方程
列方程时 a[has[i]][has[v]] -= p[j]; 和 a[has[i]][has[v]] = -p[j]; 是不一样的,因为我需要对 n 取模,那么我的 v 位置可能多次走到。
然后用高斯消元求出 x[has[x]] 就可以了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 . 3 ';;;;;. 4 '!;;;;;;!;` 5 '!;|&#@|;;;;!: 6 `;;!&####@|;;;;!: 7 .;;;!&@$$%|!;;;;;;!'.`:::::'. 8 '!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;. 9 :!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:. 10 ;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$; 11 ;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%` 12 `!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&; 13 :@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@| 14 ;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%. 15 `!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|. 16 %&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##! 17 !%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&: 18 ':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%. 19 !@|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&' 20 .:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@: 21 .%$;;;;;;;;;;;;;;;;;;|$$$$@&|;;;;;;;;;;;;;;;;;;;;%@%. 22 !&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#; 23 `%$!;;;;;;;;;;;$@|;;;;;;;;;;;;;;;;;;;;;;;;!%$@#@|. 24 .|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%$$$$$@#|. 25 ;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|. 26 |##$|!;;;;;;::'':;;;;;;;;;;;;;!%$$$@#@; 27 ;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|` 28 .%##@|;;;;:::''''''''''::;!%&##$' 29 `$##@$$@@&|!!;;;:'''''::::;;;;;|&#%. 30 ;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@: 31 .%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%. 32 :@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;` 33 `%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&' 34 |##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!' 35 :;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$: !@#$' 36 |##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%: '%%!%&; 37 |&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&; 38 |@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%` 39 !@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&: 40 '$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&: 41 |#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$' 42 |&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@! 43 `%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@! 44 !&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%` 45 '%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|' 46 .!%%&%'`|$; :|$#%|@#&;%#%. 47 */ 48 #include <map> 49 #include <set> 50 #include <list> 51 #include <ctime> 52 #include <cmath> 53 #include <stack> 54 #include <queue> 55 #include <string> 56 #include <vector> 57 #include <cstdio> 58 #include <bitset> 59 #include <cstdlib> 60 #include <cstring> 61 #include <iostream> 62 #include <algorithm> 63 #define lowbit(x) x & (-x) 64 #define mes(a, b) memset(a, b, sizeof a) 65 #define fi first 66 #define se second 67 #define pii pair<int, int> 68 #define INOPEN freopen("in.txt", "r", stdin) 69 #define OUTOPEN freopen("out.txt", "w", stdout) 70 71 typedef unsigned long long int ull; 72 typedef long long int ll; 73 const int maxn = 2e2 + 10; 74 const int maxm = 1e5 + 10; 75 const int mod = 1e9 + 7; 76 const ll INF = 1e18 + 100; 77 const int inf = 0x3f3f3f3f; 78 const double pi = acos(-1.0); 79 const double eps = 1e-9; 80 using namespace std; 81 82 int n, m; 83 int cas, tol, T; 84 int src, des; 85 86 int has[maxn]; 87 double x[maxn]; 88 double p[maxn]; 89 double a[maxn][maxn]; 90 91 void init() { 92 tol = 0; 93 mes(a, 0); 94 mes(x, 0); 95 mes(p, 0); 96 mes(has, -1); 97 } 98 99 void bfs(int src) { 100 queue<int> q; 101 while(!q.empty()) 102 q.pop(); 103 q.push(src); 104 has[src] = tol++; 105 while(!q.empty()) { 106 int u = q.front(); 107 q.pop(); 108 for(int i=1; i<=m; i++) { 109 if(fabs(p[i]) < eps) continue; 110 int v = (u + i) % n; 111 if(has[v] == -1) { 112 has[v] = tol++; 113 q.push(v); 114 } 115 } 116 } 117 } 118 119 void build() { 120 for(int i=0; i<n; i++) { 121 if(has[i] == -1) continue; 122 double ans = 0.0; 123 a[has[i]][has[i]] = 1.0; 124 if(i == des || i == n-des) { 125 x[has[i]] = 0; 126 continue; 127 } 128 for(int j=1; j<=m; j++) { 129 int v = (i + j) % n; 130 a[has[i]][has[v]] -= p[j]; 131 ans += j * p[j]; 132 } 133 x[has[i]] = ans; 134 } 135 } 136 137 int gauss(int equ, int var) { 138 int i, j, k, col, max_r; 139 for(k=0, col=0; k<equ && col<var; k++, col++) { 140 max_r = k; 141 for(i=k+1; i<equ; i++) { 142 if(fabs(a[i][col]) > fabs(a[max_r][col])) { 143 max_r = i; 144 } 145 } 146 if(fabs(a[max_r][col] < eps)) return 0; 147 if(k != max_r) { 148 swap(a[k], a[max_r]); 149 swap(x[k], x[max_r]); 150 } 151 x[k] /= a[k][col]; 152 for(j=col+1; j<var; j++) { 153 a[k][j] /= a[k][col]; 154 } 155 a[k][col] = 1; 156 for(i=0; i<equ; i++) { 157 if(i != k) { 158 x[i] -= x[k] * a[i][k]; 159 for(j=col+1; j<var; j++) { 160 a[i][j] -= a[k][j] * a[i][col]; 161 } 162 a[i][col] = 0; 163 } 164 } 165 } 166 return 1; 167 } 168 169 int main() { 170 scanf("%d", &T); 171 while(T--) { 172 init(); 173 int D; 174 scanf("%d%d%d%d%d", &n, &m, &des, &src, &D); 175 for(int i=1; i<=m; i++) { 176 scanf("%lf", &p[i]); 177 p[i] /= 100; 178 } 179 if(src == des) { 180 printf("0.00\n"); 181 continue; 182 } 183 n = 2 * n - 2; 184 if(D == 1) 185 src = n - src; 186 bfs(src); 187 if(has[des] == -1 && has[n - des] == -1) { 188 printf("Impossible !\n"); 189 continue; 190 } 191 build(); 192 if(gauss(tol, tol)) { 193 printf("%.2f\n", x[has[src]]); 194 } else { 195 printf("Impossible !\n"); 196 } 197 } 198 return 0; 199 }