output
stringlengths 52
181k
| instruction
stringlengths 296
182k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const double inf = 1e20, eps = 1e-9;
const int N = 1005;
double p[N][N], f[N], g[N], P[N];
int n, vis[N];
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j)
scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (register int i = 1; i <= n; ++i) g[i] = inf, f[i] = P[i] = 1;
f[n] = g[n] = 0;
for (register int i = 1; i < n; ++i) {
double tmp = inf;
int bj = 0;
for (register int j = 1; j <= n; ++j)
if (!vis[j] && g[j] < tmp) tmp = g[j], bj = j;
vis[bj] = 1;
for (register int j = 1; j <= n; ++j)
if (!vis[j]) {
f[j] += tmp * P[j] * p[j][bj], P[j] *= 1.0 - p[j][bj];
if (1.0 - P[j] < eps)
g[j] = inf;
else
g[j] = f[j] / (1.0 - P[j]);
}
}
printf("%.10lf\n", g[1]);
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double inf = 1e20, eps = 1e-9;
const int N = 1005;
double p[N][N], f[N], g[N], P[N];
int n, vis[N];
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= n; ++j)
scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (register int i = 1; i <= n; ++i) g[i] = inf, f[i] = P[i] = 1;
f[n] = g[n] = 0;
for (register int i = 1; i < n; ++i) {
double tmp = inf;
int bj = 0;
for (register int j = 1; j <= n; ++j)
if (!vis[j] && g[j] < tmp) tmp = g[j], bj = j;
vis[bj] = 1;
for (register int j = 1; j <= n; ++j)
if (!vis[j]) {
f[j] += tmp * P[j] * p[j][bj], P[j] *= 1.0 - p[j][bj];
if (1.0 - P[j] < eps)
g[j] = inf;
else
g[j] = f[j] / (1.0 - P[j]);
}
}
printf("%.10lf\n", g[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, temp;
double p[1000][1000];
double dis[1000], q[1000];
bool vis[1000];
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", &temp);
p[i][j] = temp / 100.0;
}
for (int i = 0; i < n; i++) {
q[i] = p[i][n - 1];
if (p[i][n - 1] > 0.003)
dis[i] = 1 / p[i][n - 1];
else
dis[i] = 1e12;
}
dis[n - 1] = 0;
vis[n - 1] = 1;
for (int i = 0; i < n; i++) {
temp = -1;
for (int j = 0; j < n; j++)
if (!vis[j] && (temp == -1 || dis[j] < dis[temp])) temp = j;
if (temp == -1) break;
vis[temp] = 1;
for (int j = 0; j < n; j++)
if (!vis[j]) {
if (q[j] > 0) {
dis[j] = dis[j] * q[j] + (1 - q[j]) * p[j][temp] * dis[temp];
q[j] = q[j] + (1 - q[j]) * p[j][temp];
dis[j] /= q[j];
} else {
q[j] = p[j][temp];
if (q[j] > 0.003) dis[j] = dis[temp] + 1 / q[j];
}
}
}
printf("%.12lf\n", dis[0]);
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, temp;
double p[1000][1000];
double dis[1000], q[1000];
bool vis[1000];
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", &temp);
p[i][j] = temp / 100.0;
}
for (int i = 0; i < n; i++) {
q[i] = p[i][n - 1];
if (p[i][n - 1] > 0.003)
dis[i] = 1 / p[i][n - 1];
else
dis[i] = 1e12;
}
dis[n - 1] = 0;
vis[n - 1] = 1;
for (int i = 0; i < n; i++) {
temp = -1;
for (int j = 0; j < n; j++)
if (!vis[j] && (temp == -1 || dis[j] < dis[temp])) temp = j;
if (temp == -1) break;
vis[temp] = 1;
for (int j = 0; j < n; j++)
if (!vis[j]) {
if (q[j] > 0) {
dis[j] = dis[j] * q[j] + (1 - q[j]) * p[j][temp] * dis[temp];
q[j] = q[j] + (1 - q[j]) * p[j][temp];
dis[j] /= q[j];
} else {
q[j] = p[j][temp];
if (q[j] > 0.003) dis[j] = dis[temp] + 1 / q[j];
}
}
}
printf("%.12lf\n", dis[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double x[2010], y[2010];
double a[2010][2010];
int vis[2010];
double dis[2010];
int main() {
int i, j, m, n, s, t, k, min;
scanf("%d", &n);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) {
scanf("%d", &t);
a[i][j] = t / 100.0;
}
for (i = 1; i <= n; i++) x[i] = 1, y[i] = 1;
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++) dis[i] = 2e9;
dis[n] = 0;
for (i = 1; i < n; i++) {
k = 0;
for (j = 1; j <= n; j++)
if (vis[j])
continue;
else if (k == 0 || dis[j] <= dis[k])
k = j, min = dis[j];
vis[k] = 1;
for (j = 1; j <= n; j++)
if (vis[j] == 0) {
y[j] += x[j] * a[j][k] * dis[k];
x[j] = x[j] * (1 - a[j][k]);
dis[j] = y[j] / (1 - x[j]);
}
}
printf("%.17f", dis[1]);
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double x[2010], y[2010];
double a[2010][2010];
int vis[2010];
double dis[2010];
int main() {
int i, j, m, n, s, t, k, min;
scanf("%d", &n);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) {
scanf("%d", &t);
a[i][j] = t / 100.0;
}
for (i = 1; i <= n; i++) x[i] = 1, y[i] = 1;
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++) dis[i] = 2e9;
dis[n] = 0;
for (i = 1; i < n; i++) {
k = 0;
for (j = 1; j <= n; j++)
if (vis[j])
continue;
else if (k == 0 || dis[j] <= dis[k])
k = j, min = dis[j];
vis[k] = 1;
for (j = 1; j <= n; j++)
if (vis[j] == 0) {
y[j] += x[j] * a[j][k] * dis[k];
x[j] = x[j] * (1 - a[j][k]);
dis[j] = y[j] / (1 - x[j]);
}
}
printf("%.17f", dis[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, vst[N];
double p[N][N], g[N], f[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (int i = 1; i < n; i++) g[i] = f[i] = 1;
f[n] = 0;
for (int i = 1; i <= n; i++) {
int pos = -1;
double mn = 1e30;
for (int j = 1; j <= n; j++)
if (!vst[j] && g[j] < 1 && f[j] / (1.0 - g[j]) < mn) {
mn = f[j] / (1.0 - g[j]);
pos = j;
}
if (pos == -1) break;
vst[pos] = 1;
f[pos] = f[pos] / (1.0 - g[pos]);
for (int j = 1; j <= n; j++) {
if (vst[j]) continue;
f[j] += f[pos] * p[j][pos] * g[j];
g[j] *= (1.0 - p[j][pos]);
}
}
printf("%.10f\n", f[1]);
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, vst[N];
double p[N][N], g[N], f[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (int i = 1; i < n; i++) g[i] = f[i] = 1;
f[n] = 0;
for (int i = 1; i <= n; i++) {
int pos = -1;
double mn = 1e30;
for (int j = 1; j <= n; j++)
if (!vst[j] && g[j] < 1 && f[j] / (1.0 - g[j]) < mn) {
mn = f[j] / (1.0 - g[j]);
pos = j;
}
if (pos == -1) break;
vst[pos] = 1;
f[pos] = f[pos] / (1.0 - g[pos]);
for (int j = 1; j <= n; j++) {
if (vst[j]) continue;
f[j] += f[pos] * p[j][pos] * g[j];
g[j] *= (1.0 - p[j][pos]);
}
}
printf("%.10f\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1000 + 5;
const double inf = 1e18;
int n;
double f[Maxn], p[Maxn], g[Maxn][Maxn];
bool vis[Maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
g[i][j] = 1.0 * x / 100;
}
if (n == 1) {
printf("0\n");
return 0;
}
vis[n] = 1;
for (int i = 1; i < n; i++) {
f[i] = 1;
p[i] = 1 - g[i][n];
}
for (int i = 1; i <= n; i++) {
double mn = inf;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && f[j] / (1 - p[j]) < mn) mn = f[j] / (1 - p[j]), pos = j;
if (pos == 1) {
printf("%.10lf\n", f[1] / (1 - p[1]));
return 0;
}
vis[pos] = 1;
for (int j = 1; j <= n; j++) {
f[j] += f[pos] / (1 - p[pos]) * g[j][pos] * p[j];
p[j] *= (1 - g[j][pos]);
}
}
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1000 + 5;
const double inf = 1e18;
int n;
double f[Maxn], p[Maxn], g[Maxn][Maxn];
bool vis[Maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
g[i][j] = 1.0 * x / 100;
}
if (n == 1) {
printf("0\n");
return 0;
}
vis[n] = 1;
for (int i = 1; i < n; i++) {
f[i] = 1;
p[i] = 1 - g[i][n];
}
for (int i = 1; i <= n; i++) {
double mn = inf;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && f[j] / (1 - p[j]) < mn) mn = f[j] / (1 - p[j]), pos = j;
if (pos == 1) {
printf("%.10lf\n", f[1] / (1 - p[1]));
return 0;
}
vis[pos] = 1;
for (int j = 1; j <= n; j++) {
f[j] += f[pos] / (1 - p[pos]) * g[j][pos] * p[j];
p[j] *= (1 - g[j][pos]);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
double dis[N][N], E[N], prod[N];
bool vis[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
double x;
scanf("%lf", &x);
dis[i][j] = x / 100;
}
if (n == 1) {
puts("0");
return 0;
}
vis[n] = true;
for (int i = 1; i < n; i++) E[i] = 1, prod[i] = 1 - dis[i][n];
for (int i = 1; i <= n; i++) {
double low = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && E[j] / (1 - prod[j]) < low)
low = E[j] / (1 - prod[j]), pos = j;
if (pos == 1) {
printf("%.10lf\n", E[1] / (1 - prod[1]));
return 0;
}
vis[pos] = true;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - prod[pos]) * dis[j][pos] * prod[j],
prod[j] *= (1 - dis[j][pos]);
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
double dis[N][N], E[N], prod[N];
bool vis[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
double x;
scanf("%lf", &x);
dis[i][j] = x / 100;
}
if (n == 1) {
puts("0");
return 0;
}
vis[n] = true;
for (int i = 1; i < n; i++) E[i] = 1, prod[i] = 1 - dis[i][n];
for (int i = 1; i <= n; i++) {
double low = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && E[j] / (1 - prod[j]) < low)
low = E[j] / (1 - prod[j]), pos = j;
if (pos == 1) {
printf("%.10lf\n", E[1] / (1 - prod[1]));
return 0;
}
vis[pos] = true;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - prod[pos]) * dis[j][pos] * prod[j],
prod[j] *= (1 - dis[j][pos]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1010][1010];
double f[1010];
double sum[1010];
double rest[1010];
bool chk[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", p[i] + j), p[i][j] /= 100;
for (int i = 1; i < n; i++) f[i] = 1e8, rest[i] = 1;
while (!chk[1]) {
double mn = 1e9;
int x = 0;
for (int i = 1; i <= n; i++)
if (!chk[i] && f[i] < mn) {
mn = f[i];
x = i;
}
chk[x] = 1;
for (int i = 1; i <= n; i++)
if (!chk[i] && f[i] > f[x] + 1e-7) {
sum[i] += rest[i] * p[i][x] * mn;
rest[i] = rest[i] * (1 - p[i][x]);
if (rest[i] > 0.9999999) continue;
f[i] = min(f[i], (1 + sum[i]) / (1 - rest[i]));
}
}
printf("%.15lf\n", f[1]);
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1010][1010];
double f[1010];
double sum[1010];
double rest[1010];
bool chk[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", p[i] + j), p[i][j] /= 100;
for (int i = 1; i < n; i++) f[i] = 1e8, rest[i] = 1;
while (!chk[1]) {
double mn = 1e9;
int x = 0;
for (int i = 1; i <= n; i++)
if (!chk[i] && f[i] < mn) {
mn = f[i];
x = i;
}
chk[x] = 1;
for (int i = 1; i <= n; i++)
if (!chk[i] && f[i] > f[x] + 1e-7) {
sum[i] += rest[i] * p[i][x] * mn;
rest[i] = rest[i] * (1 - p[i][x]);
if (rest[i] > 0.9999999) continue;
f[i] = min(f[i], (1 + sum[i]) / (1 - rest[i]));
}
}
printf("%.15lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
inline int read() {
int x;
char c;
while ((c = getchar()) < '0' || c > '9')
;
for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';) x = x * 10 + c - '0';
return x;
}
int u[1000 + 5];
double p[1000 + 5][1000 + 5], v[1000 + 5], r[1000 + 5], z;
int main() {
int n = read(), i, j, k;
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j) p[i][j] = read(), p[i][j] /= 100;
for (i = 1; i < n; ++i) r[i] = 1;
for (i = 1; i <= n; ++i) {
if (i > 1) {
for (j = 1, z = 1e99; j < n; ++j)
if (!u[j] && r[j] < 1)
if ((v[j] + r[j]) / (1 - r[j]) < z)
z = (v[j] + r[j]) / (1 - r[j]), k = j;
u[k] = 1;
v[k] = (v[k] + r[k]) / (1 - r[k]);
if (k == 1) return 0 * printf("%.10lf", v[1]);
} else
k = n;
for (j = 1; j < n; ++j)
if (!u[j]) v[j] += r[j] * p[j][k] * (1 + v[k]), r[j] -= r[j] * p[j][k];
}
printf("%.10lf", v[1]);
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
inline int read() {
int x;
char c;
while ((c = getchar()) < '0' || c > '9')
;
for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';) x = x * 10 + c - '0';
return x;
}
int u[1000 + 5];
double p[1000 + 5][1000 + 5], v[1000 + 5], r[1000 + 5], z;
int main() {
int n = read(), i, j, k;
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j) p[i][j] = read(), p[i][j] /= 100;
for (i = 1; i < n; ++i) r[i] = 1;
for (i = 1; i <= n; ++i) {
if (i > 1) {
for (j = 1, z = 1e99; j < n; ++j)
if (!u[j] && r[j] < 1)
if ((v[j] + r[j]) / (1 - r[j]) < z)
z = (v[j] + r[j]) / (1 - r[j]), k = j;
u[k] = 1;
v[k] = (v[k] + r[k]) / (1 - r[k]);
if (k == 1) return 0 * printf("%.10lf", v[1]);
} else
k = n;
for (j = 1; j < n; ++j)
if (!u[j]) v[j] += r[j] * p[j][k] * (1 + v[k]), r[j] -= r[j] * p[j][k];
}
printf("%.10lf", v[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
const long double inf = 1e30;
long double A[MAXN], B[MAXN];
priority_queue<pair<long double, int>, vector<pair<long double, int> >,
greater<pair<long double, int> > >
q;
int n;
long double P[MAXN][MAXN];
void load() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
int a;
scanf("%d", &a);
P[i][j] = (long double)a / 100.0;
}
}
inline long double ans(long double a, long double b) {
return (b + 1.0) / (1.0 - a);
}
bool bio[MAXN];
void solve() {
q.push(pair<long double, int>(0.0, n - 1));
for (int i = 0; i < n; i++) {
A[i] = 1;
B[i] = 0;
}
while (!q.empty()) {
int u = q.top().second;
long double ansu = q.top().first;
q.pop();
if (bio[u]) continue;
bio[u] = true;
if (u == 0) {
printf("%.10f\n", (double)ansu);
return;
}
for (int i = 0; i < n; i++) {
if (bio[i]) continue;
long double newA = A[i] * (1.0 - P[i][u]);
long double newB = B[i] + A[i] * P[i][u] * ansu;
A[i] = newA;
B[i] = newB;
if (A[i] < 1 - 1e-6) q.push(pair<long double, int>(ans(newA, newB), i));
}
}
}
int main() {
load();
solve();
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
const long double inf = 1e30;
long double A[MAXN], B[MAXN];
priority_queue<pair<long double, int>, vector<pair<long double, int> >,
greater<pair<long double, int> > >
q;
int n;
long double P[MAXN][MAXN];
void load() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
int a;
scanf("%d", &a);
P[i][j] = (long double)a / 100.0;
}
}
inline long double ans(long double a, long double b) {
return (b + 1.0) / (1.0 - a);
}
bool bio[MAXN];
void solve() {
q.push(pair<long double, int>(0.0, n - 1));
for (int i = 0; i < n; i++) {
A[i] = 1;
B[i] = 0;
}
while (!q.empty()) {
int u = q.top().second;
long double ansu = q.top().first;
q.pop();
if (bio[u]) continue;
bio[u] = true;
if (u == 0) {
printf("%.10f\n", (double)ansu);
return;
}
for (int i = 0; i < n; i++) {
if (bio[i]) continue;
long double newA = A[i] * (1.0 - P[i][u]);
long double newB = B[i] + A[i] * P[i][u] * ansu;
A[i] = newA;
B[i] = newB;
if (A[i] < 1 - 1e-6) q.push(pair<long double, int>(ans(newA, newB), i));
}
}
}
int main() {
load();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
template <class T1, class T2>
inline bool cmin(T1 &a, const T2 &b) {
return b < a ? (a = b, true) : false;
}
template <class T1, class T2>
inline bool cmax(T1 &a, const T2 &b) {
return a < b ? (a = b, true) : false;
}
template <class Type>
Type read() {
Type a;
bool b;
unsigned char c;
while (c = getchar() - 48, (c > 9) & (c != 253))
;
for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c)
;
return b ? -a : a;
}
auto rd = read<int>;
const int N = 1001;
int n;
bool vis[N];
double p[N][N], g[N], h[N];
int main() {
n = rd();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = rd() / 100.0;
vis[n] = true;
for (int i = 1; i < n; ++i) g[i] = 1, h[i] = 1 - p[i][n];
while (!vis[1]) {
int j;
double f = 1e100;
for (int i = 1; i < n; ++i)
if (!vis[i] && cmin(f, g[i] / (1 - h[i]))) j = i;
vis[j] = true;
if (j == 1) {
printf("%.12lf\n", f);
return 0;
}
for (int i = 1; i < n; ++i)
if (!vis[i]) {
g[i] += h[i] * p[i][j] * f;
h[i] *= (1 - p[i][j]);
}
}
puts("0");
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
template <class T1, class T2>
inline bool cmin(T1 &a, const T2 &b) {
return b < a ? (a = b, true) : false;
}
template <class T1, class T2>
inline bool cmax(T1 &a, const T2 &b) {
return a < b ? (a = b, true) : false;
}
template <class Type>
Type read() {
Type a;
bool b;
unsigned char c;
while (c = getchar() - 48, (c > 9) & (c != 253))
;
for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c)
;
return b ? -a : a;
}
auto rd = read<int>;
const int N = 1001;
int n;
bool vis[N];
double p[N][N], g[N], h[N];
int main() {
n = rd();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = rd() / 100.0;
vis[n] = true;
for (int i = 1; i < n; ++i) g[i] = 1, h[i] = 1 - p[i][n];
while (!vis[1]) {
int j;
double f = 1e100;
for (int i = 1; i < n; ++i)
if (!vis[i] && cmin(f, g[i] / (1 - h[i]))) j = i;
vis[j] = true;
if (j == 1) {
printf("%.12lf\n", f);
return 0;
}
for (int i = 1; i < n; ++i)
if (!vis[i]) {
g[i] += h[i] * p[i][j] * f;
h[i] *= (1 - p[i][j]);
}
}
puts("0");
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 1;
double p[MAXN][MAXN];
double E[MAXN];
double A[MAXN], B[MAXN];
bool vis[MAXN];
int n;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%lf", &p[i][j]);
}
}
for (int i = 0; i < MAXN; i++) {
E[i] = 1e18;
A[i] = 0;
B[i] = 1;
}
E[n - 1] = 0;
for (int iter = 0; iter <= n; iter++) {
double minim = 1e18;
int idx = -1;
for (int i = 0; i < n; i++) {
if (minim > E[i] && !vis[i]) {
idx = i, minim = E[i];
}
}
if (idx == 0) {
printf("%.10lf\n", E[0]);
return 0;
}
vis[idx] = true;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
A[i] += B[i] * E[idx] * 0.01 * p[i][idx];
B[i] *= (1.0 - 0.01 * p[i][idx]);
if ((A[i] + 1.0) / (1.0 - B[i]) < E[i]) {
E[i] = (A[i] + 1.0) / (1.0 - B[i]);
}
}
}
}
assert(false);
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 1;
double p[MAXN][MAXN];
double E[MAXN];
double A[MAXN], B[MAXN];
bool vis[MAXN];
int n;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%lf", &p[i][j]);
}
}
for (int i = 0; i < MAXN; i++) {
E[i] = 1e18;
A[i] = 0;
B[i] = 1;
}
E[n - 1] = 0;
for (int iter = 0; iter <= n; iter++) {
double minim = 1e18;
int idx = -1;
for (int i = 0; i < n; i++) {
if (minim > E[i] && !vis[i]) {
idx = i, minim = E[i];
}
}
if (idx == 0) {
printf("%.10lf\n", E[0]);
return 0;
}
vis[idx] = true;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
A[i] += B[i] * E[idx] * 0.01 * p[i][idx];
B[i] *= (1.0 - 0.01 * p[i][idx]);
if ((A[i] + 1.0) / (1.0 - B[i]) < E[i]) {
E[i] = (A[i] + 1.0) / (1.0 - B[i]);
}
}
}
}
assert(false);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
const double eps = 1e-6;
int n;
double p[N][N];
double zaden[N];
double curr[N];
double ans[N];
bitset<N> vis;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
int w;
cin >> w;
p[i][j] = (double)w / 100;
}
zaden[i] = 1;
}
for (int i = 1; i <= n - 1; ++i) {
ans[i] = 1000000007;
}
while (vis.count() < n) {
int v = -1;
for (int i = 1; i <= n; ++i) {
if (!vis[i] && (v == -1 || ans[v] > ans[i])) v = i;
}
vis[v] = 1;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
curr[i] += (ans[v] + 1) * p[i][v] * zaden[i];
zaden[i] *= (1 - p[i][v]);
if (zaden[i] + eps > 1)
ans[i] = 1000000007;
else
ans[i] = 1 / (1 - zaden[i]) * (zaden[i] + curr[i]);
}
}
}
cout << fixed << setprecision(10) << ans[1];
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
const double eps = 1e-6;
int n;
double p[N][N];
double zaden[N];
double curr[N];
double ans[N];
bitset<N> vis;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
int w;
cin >> w;
p[i][j] = (double)w / 100;
}
zaden[i] = 1;
}
for (int i = 1; i <= n - 1; ++i) {
ans[i] = 1000000007;
}
while (vis.count() < n) {
int v = -1;
for (int i = 1; i <= n; ++i) {
if (!vis[i] && (v == -1 || ans[v] > ans[i])) v = i;
}
vis[v] = 1;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
curr[i] += (ans[v] + 1) * p[i][v] * zaden[i];
zaden[i] *= (1 - p[i][v]);
if (zaden[i] + eps > 1)
ans[i] = 1000000007;
else
ans[i] = 1 / (1 - zaden[i]) * (zaden[i] + curr[i]);
}
}
}
cout << fixed << setprecision(10) << ans[1];
}
```
|
#include <bits/stdc++.h>
const int N = 1e3 + 100;
int n;
long double p[N][N];
void init() {
std::cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int t;
scanf("%d", &t);
p[i][j] = (long double)t / 100.0;
}
}
}
void work() {
static long double f[N], g[N];
static bool visit[N];
std::fill(f + 1, f + n + 1, 1 / 0.0);
std::fill(g + 1, g + n + 1, 1.0);
std::fill(visit + 1, visit + n + 1, false);
f[n] = 0.0;
for (int i = 1; i <= n; i++) {
int u = -1;
for (int j = 1; j <= n; j++) {
if (visit[j] == false && (u == -1 || f[j] < f[u])) {
u = j;
}
}
visit[u] = true;
for (int j = 1; j <= n; j++) {
if (visit[j] == false) {
if (std::abs(g[j] - 1) > 1e-8) {
f[j] *= (1.0 - g[j]);
f[j] -= g[j];
} else {
f[j] = 0.0;
}
f[j] += g[j] * p[j][u] * (f[u] + 1);
g[j] *= (1 - p[j][u]);
f[j] = (f[j] + g[j]) / (1.0 - g[j]);
}
}
}
printf("%.15f\n", (double)f[1]);
}
int main() {
init();
work();
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 1e3 + 100;
int n;
long double p[N][N];
void init() {
std::cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int t;
scanf("%d", &t);
p[i][j] = (long double)t / 100.0;
}
}
}
void work() {
static long double f[N], g[N];
static bool visit[N];
std::fill(f + 1, f + n + 1, 1 / 0.0);
std::fill(g + 1, g + n + 1, 1.0);
std::fill(visit + 1, visit + n + 1, false);
f[n] = 0.0;
for (int i = 1; i <= n; i++) {
int u = -1;
for (int j = 1; j <= n; j++) {
if (visit[j] == false && (u == -1 || f[j] < f[u])) {
u = j;
}
}
visit[u] = true;
for (int j = 1; j <= n; j++) {
if (visit[j] == false) {
if (std::abs(g[j] - 1) > 1e-8) {
f[j] *= (1.0 - g[j]);
f[j] -= g[j];
} else {
f[j] = 0.0;
}
f[j] += g[j] * p[j][u] * (f[u] + 1);
g[j] *= (1 - p[j][u]);
f[j] = (f[j] + g[j]) / (1.0 - g[j]);
}
}
}
printf("%.15f\n", (double)f[1]);
}
int main() {
init();
work();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(0.0) * 2.0;
const long double eps = 1e-10;
const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1},
{-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
template <class T>
inline T abs1(T a) {
return a < 0 ? -a : a;
}
template <typename t, typename t1>
t min1(t a, t1 b) {
return a < b ? a : b;
}
template <typename t, typename... arg>
t min1(t a, arg... arr) {
return min1(a, min1(arr...));
}
template <typename t, typename t1>
t max1(t a, t1 b) {
return a > b ? a : b;
}
template <typename t, typename... arg>
t max1(t a, arg... arr) {
return max1(a, max1(arr...));
}
inline int jud(double a, double b) {
if (abs(a) < eps && abs(b) < eps)
return 0;
else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
return 0;
if (a < b) return -1;
return 1;
}
template <typename t>
inline int jud(t a, t b) {
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
if (na == 0) return 0;
int be = 0, en = na - 1;
if (*a <= *(a + na - 1)) {
if (f_lb == 0)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != 1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != -1)
en = mid;
else
be = mid + 1;
}
if (f_small && jud(*(a + be), val) == 1) be--;
if (!f_small && jud(*(a + be), val) == -1) be++;
} else {
if (f_lb)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != -1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != 1)
en = mid;
else
be = mid + 1;
}
if (!f_small && jud(*(a + be), val) == -1) be--;
if (f_small && jud(*(a + be), val) == 1) be++;
}
return be;
}
template <class T>
inline T lowb(T num) {
return num & (-num);
}
inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(unsigned long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(unsigned int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(unsigned long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
inline int bitmaxl(long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
long long pow(long long n, long long m, long long mod = 0) {
if (m < 0) return 0;
long long ans = 1;
long long k = n;
while (m) {
if (m & 1) {
ans *= k;
if (mod) ans %= mod;
}
k *= k;
if (mod) k %= mod;
m >>= 1;
}
return ans;
}
template <class t1, class t2>
inline void add(t1 &a, t2 b, int mod = -1) {
if (mod == -1) mod = 1000000007;
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
}
template <class t>
void output1(t arr) {
for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
cerr << endl;
}
template <class t>
void output2(t arr) {
for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
}
const int maxn = 1010;
int arr[maxn][maxn];
int n;
double ans[maxn], lef[maxn];
int have[maxn];
int main() {
ios_base::sync_with_stdio(0);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) scanf("%d", arr[i] + j);
}
ans[n - 1] = 0;
have[n - 1] = 1;
for (int i = 0; i < n - 1; i++) {
ans[i] = 1.0 * arr[i][n - 1] / 100;
lef[i] = 1 - 1.0 * arr[i][n - 1] / 100;
}
for (int i = 0; i < n - 1; i++) {
int no = 0;
for (int j = 0; j < n; j++)
if (have[no] || (have[j] == 0 && (ans[j] + lef[j]) / (1 - lef[j]) <
(ans[no] + lef[no]) / (1 - lef[no])))
no = j;
ans[no] = (ans[no] + lef[no]) / (1 - lef[no]);
have[no] = 1;
for (int j = 0; j < n; j++)
if (!have[j]) {
ans[j] += lef[j] * 1.0 * arr[j][no] * (1 + ans[no]) / 100;
lef[j] *= (1 - 1.0 * arr[j][no] / 100);
}
}
printf("%.10f\n", ans[0]);
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(0.0) * 2.0;
const long double eps = 1e-10;
const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1},
{-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
template <class T>
inline T abs1(T a) {
return a < 0 ? -a : a;
}
template <typename t, typename t1>
t min1(t a, t1 b) {
return a < b ? a : b;
}
template <typename t, typename... arg>
t min1(t a, arg... arr) {
return min1(a, min1(arr...));
}
template <typename t, typename t1>
t max1(t a, t1 b) {
return a > b ? a : b;
}
template <typename t, typename... arg>
t max1(t a, arg... arr) {
return max1(a, max1(arr...));
}
inline int jud(double a, double b) {
if (abs(a) < eps && abs(b) < eps)
return 0;
else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
return 0;
if (a < b) return -1;
return 1;
}
template <typename t>
inline int jud(t a, t b) {
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
if (na == 0) return 0;
int be = 0, en = na - 1;
if (*a <= *(a + na - 1)) {
if (f_lb == 0)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != 1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != -1)
en = mid;
else
be = mid + 1;
}
if (f_small && jud(*(a + be), val) == 1) be--;
if (!f_small && jud(*(a + be), val) == -1) be++;
} else {
if (f_lb)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != -1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != 1)
en = mid;
else
be = mid + 1;
}
if (!f_small && jud(*(a + be), val) == -1) be--;
if (f_small && jud(*(a + be), val) == 1) be++;
}
return be;
}
template <class T>
inline T lowb(T num) {
return num & (-num);
}
inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(unsigned long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(unsigned int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(unsigned long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
inline int bitmaxl(long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
long long pow(long long n, long long m, long long mod = 0) {
if (m < 0) return 0;
long long ans = 1;
long long k = n;
while (m) {
if (m & 1) {
ans *= k;
if (mod) ans %= mod;
}
k *= k;
if (mod) k %= mod;
m >>= 1;
}
return ans;
}
template <class t1, class t2>
inline void add(t1 &a, t2 b, int mod = -1) {
if (mod == -1) mod = 1000000007;
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
}
template <class t>
void output1(t arr) {
for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
cerr << endl;
}
template <class t>
void output2(t arr) {
for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
}
const int maxn = 1010;
int arr[maxn][maxn];
int n;
double ans[maxn], lef[maxn];
int have[maxn];
int main() {
ios_base::sync_with_stdio(0);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) scanf("%d", arr[i] + j);
}
ans[n - 1] = 0;
have[n - 1] = 1;
for (int i = 0; i < n - 1; i++) {
ans[i] = 1.0 * arr[i][n - 1] / 100;
lef[i] = 1 - 1.0 * arr[i][n - 1] / 100;
}
for (int i = 0; i < n - 1; i++) {
int no = 0;
for (int j = 0; j < n; j++)
if (have[no] || (have[j] == 0 && (ans[j] + lef[j]) / (1 - lef[j]) <
(ans[no] + lef[no]) / (1 - lef[no])))
no = j;
ans[no] = (ans[no] + lef[no]) / (1 - lef[no]);
have[no] = 1;
for (int j = 0; j < n; j++)
if (!have[j]) {
ans[j] += lef[j] * 1.0 * arr[j][no] * (1 + ans[no]) / 100;
lef[j] *= (1 - 1.0 * arr[j][no] / 100);
}
}
printf("%.10f\n", ans[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
int n;
double P[1050][1050];
double F[1050], R[1050], D[1050];
bool fix[1050];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &P[j][i]), P[j][i] /= 100;
for (int i = 1; i < n; i++)
F[i] = std::numeric_limits<double>::max(), R[i] = 1;
for (int i = 1; i <= n; i++) {
double PP = std::numeric_limits<double>::max();
int Q;
for (int j = 1; j <= n; j++)
if (!fix[j] && F[j] < PP) PP = F[j], Q = j;
fix[Q] = true;
for (int j = 1; j <= n; j++)
if (!fix[j] && P[Q][j]) {
D[j] += R[j] * P[Q][j] * F[Q], R[j] *= (1 - P[Q][j]);
F[j] = (1 + D[j]) / (1 - R[j]);
}
}
printf("%.10lf\n", F[1]);
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
int n;
double P[1050][1050];
double F[1050], R[1050], D[1050];
bool fix[1050];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &P[j][i]), P[j][i] /= 100;
for (int i = 1; i < n; i++)
F[i] = std::numeric_limits<double>::max(), R[i] = 1;
for (int i = 1; i <= n; i++) {
double PP = std::numeric_limits<double>::max();
int Q;
for (int j = 1; j <= n; j++)
if (!fix[j] && F[j] < PP) PP = F[j], Q = j;
fix[Q] = true;
for (int j = 1; j <= n; j++)
if (!fix[j] && P[Q][j]) {
D[j] += R[j] * P[Q][j] * F[Q], R[j] *= (1 - P[Q][j]);
F[j] = (1 + D[j]) / (1 - R[j]);
}
}
printf("%.10lf\n", F[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
return x * x;
}
const int maxn = 1003;
int p[maxn][maxn];
double sum[maxn], pr[maxn];
bool used[maxn];
double getans(double x, double y) { return (x + y) / (1 - y); }
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> p[i][j];
for (int i = 1; i <= n; i++) {
sum[i] = 0;
pr[i] = 1;
}
pr[n] = 0;
for (int k = 1; k <= n; k++) {
int num = -1;
for (int i = 1; i <= n; i++)
if (!used[i]) {
if (pr[i] != 1) {
if (num == -1 || getans(sum[num], pr[num]) > getans(sum[i], pr[i])) {
num = i;
}
}
}
used[num] = true;
for (int i = 1; i <= n; i++)
if (!used[i] && p[i][num] != 0) {
sum[i] += pr[i] * (p[i][num] / 100.) * (1 + getans(sum[num], pr[num]));
pr[i] *= 1 - p[i][num] / 100.;
}
}
cout.precision(10);
cout << fixed << getans(sum[1], pr[1]) << "\n";
return 0;
}
|
### Prompt
Construct a Cpp code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T sqr(T x) {
return x * x;
}
const int maxn = 1003;
int p[maxn][maxn];
double sum[maxn], pr[maxn];
bool used[maxn];
double getans(double x, double y) { return (x + y) / (1 - y); }
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> p[i][j];
for (int i = 1; i <= n; i++) {
sum[i] = 0;
pr[i] = 1;
}
pr[n] = 0;
for (int k = 1; k <= n; k++) {
int num = -1;
for (int i = 1; i <= n; i++)
if (!used[i]) {
if (pr[i] != 1) {
if (num == -1 || getans(sum[num], pr[num]) > getans(sum[i], pr[i])) {
num = i;
}
}
}
used[num] = true;
for (int i = 1; i <= n; i++)
if (!used[i] && p[i][num] != 0) {
sum[i] += pr[i] * (p[i][num] / 100.) * (1 + getans(sum[num], pr[num]));
pr[i] *= 1 - p[i][num] / 100.;
}
}
cout.precision(10);
cout << fixed << getans(sum[1], pr[1]) << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, v[1010];
long double p[1010][1010], a[1010], d[1010];
int work() {
int k;
long double x = 1e100L;
for (int i = 1; i <= n; i++)
if (!v[i] && (d[i] + a[i]) / (1 - a[i]) <= x)
x = (d[i] + a[i]) / (1 - a[i]), k = i;
v[k] = 1, d[k] = (d[k] + a[k]) / (1 - a[k]);
if (k == 1) return 1;
for (int i = 1; i <= n; i++)
if (!v[i]) d[i] += a[i] * p[i][k] * (d[k] + 1), a[i] *= 1 - p[i][k];
return 0;
}
int main() {
cin >> n;
for (int i = 1, x; i <= n; i++) {
for (int j = 1; j <= n; j++) cin >> x, p[i][j] = 0.01L * x;
a[i] = 1;
}
a[n] = 0;
for (int i = 1; i <= n; i++)
if (work()) {
cout << fixed << setprecision(15) << d[1];
return 0;
}
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, v[1010];
long double p[1010][1010], a[1010], d[1010];
int work() {
int k;
long double x = 1e100L;
for (int i = 1; i <= n; i++)
if (!v[i] && (d[i] + a[i]) / (1 - a[i]) <= x)
x = (d[i] + a[i]) / (1 - a[i]), k = i;
v[k] = 1, d[k] = (d[k] + a[k]) / (1 - a[k]);
if (k == 1) return 1;
for (int i = 1; i <= n; i++)
if (!v[i]) d[i] += a[i] * p[i][k] * (d[k] + 1), a[i] *= 1 - p[i][k];
return 0;
}
int main() {
cin >> n;
for (int i = 1, x; i <= n; i++) {
for (int j = 1; j <= n; j++) cin >> x, p[i][j] = 0.01L * x;
a[i] = 1;
}
a[n] = 0;
for (int i = 1; i <= n; i++)
if (work()) {
cout << fixed << setprecision(15) << d[1];
return 0;
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const double eps = 1e-15;
const double INF = 1e100;
template <typename T>
void chkmax(T &x, T y) {
x = max(x, y);
}
template <typename T>
void chkmin(T &x, T y) {
x = min(x, y);
}
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int n, m;
bool vis[MAXN];
double p[MAXN][MAXN], lft[MAXN], sum[MAXN], ans[MAXN];
int main() {
read(n);
for (int i = 1; i <= n - 1; i++) ans[i] = INF, sum[i] = 1, lft[i] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
read(x);
p[i][j] = 0.01 * x;
}
for (int t = 1; t <= n; t++) {
int pos = 0;
for (int i = 1; i <= n; i++)
if (!vis[i] && (pos == 0 || ans[i] < ans[pos])) pos = i;
vis[pos] = true;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
sum[i] += lft[i] * p[i][pos] * ans[pos];
lft[i] *= (1 - p[i][pos]);
double tres = INF;
if (1 - lft[i] > eps) tres = sum[i] / (1 - lft[i]);
chkmin(ans[i], tres);
}
}
printf("%.10lf\n", ans[1]);
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const double eps = 1e-15;
const double INF = 1e100;
template <typename T>
void chkmax(T &x, T y) {
x = max(x, y);
}
template <typename T>
void chkmin(T &x, T y) {
x = min(x, y);
}
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
int n, m;
bool vis[MAXN];
double p[MAXN][MAXN], lft[MAXN], sum[MAXN], ans[MAXN];
int main() {
read(n);
for (int i = 1; i <= n - 1; i++) ans[i] = INF, sum[i] = 1, lft[i] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
read(x);
p[i][j] = 0.01 * x;
}
for (int t = 1; t <= n; t++) {
int pos = 0;
for (int i = 1; i <= n; i++)
if (!vis[i] && (pos == 0 || ans[i] < ans[pos])) pos = i;
vis[pos] = true;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
sum[i] += lft[i] * p[i][pos] * ans[pos];
lft[i] *= (1 - p[i][pos]);
double tres = INF;
if (1 - lft[i] > eps) tres = sum[i] / (1 - lft[i]);
chkmin(ans[i], tres);
}
}
printf("%.10lf\n", ans[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-15;
int n;
double f[1005], p[1005][1005], prod[1005];
bool vis[1005];
inline int read() {
int x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x;
}
int main() {
n = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = read() / 100.0;
for (int i = 1; i <= n; ++i) prod[i] = 1;
for (int i = 1; i <= n; ++i) {
int u = -1;
double minExp = 1e50;
if (i != 1) {
for (int j = 1; j <= n; ++j)
if (!vis[j] && (1 + f[j]) / (1 - prod[j]) < minExp) {
u = j;
minExp = (1 + f[u]) / (1 - prod[u]);
}
} else {
u = n;
prod[u] = 0;
}
vis[u] = true;
if (u != n) f[u] = (1 + f[u]) / (1 - prod[u]);
for (int v = 1; v <= n; ++v) {
if (vis[v]) continue;
f[v] = f[v] + prod[v] * p[v][u] * f[u];
prod[v] = prod[v] * (1 - p[v][u]);
}
}
printf("%.10lf", f[1]);
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-15;
int n;
double f[1005], p[1005][1005], prod[1005];
bool vis[1005];
inline int read() {
int x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x;
}
int main() {
n = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = read() / 100.0;
for (int i = 1; i <= n; ++i) prod[i] = 1;
for (int i = 1; i <= n; ++i) {
int u = -1;
double minExp = 1e50;
if (i != 1) {
for (int j = 1; j <= n; ++j)
if (!vis[j] && (1 + f[j]) / (1 - prod[j]) < minExp) {
u = j;
minExp = (1 + f[u]) / (1 - prod[u]);
}
} else {
u = n;
prod[u] = 0;
}
vis[u] = true;
if (u != n) f[u] = (1 + f[u]) / (1 - prod[u]);
for (int v = 1; v <= n; ++v) {
if (vis[v]) continue;
f[v] = f[v] + prod[v] * p[v][u] * f[u];
prod[v] = prod[v] * (1 - p[v][u]);
}
}
printf("%.10lf", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
bool t = false;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') t = true, ch = getchar();
while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
return t ? -x : x;
}
int n, Q[1010], h, t;
bool use[1010], vis[1010];
double E[1010], p[1010][1010], prod[1010];
int main() {
n = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = read() / 100.00;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i) E[i] = 1, prod[i] = 1 - p[i][n];
E[n] = 0;
vis[n] = true;
for (int i = 1; i <= n; ++i) {
int u = 0;
double mn = 1e18;
for (int j = 1; j <= n; ++j)
if (!vis[j] && E[j] / (1 - prod[j]) < mn)
u = j, mn = E[j] / (1 - prod[j]);
vis[u] = true;
if (u == 1) {
printf("%.10lf\n", E[1] / (1 - prod[1]));
return 0;
}
for (int j = 1; j <= n; ++j)
E[j] += (E[u] / (1 - prod[u])) * p[j][u] * prod[j],
prod[j] *= (1 - p[j][u]);
}
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
bool t = false;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') t = true, ch = getchar();
while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar();
return t ? -x : x;
}
int n, Q[1010], h, t;
bool use[1010], vis[1010];
double E[1010], p[1010][1010], prod[1010];
int main() {
n = read();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) p[i][j] = read() / 100.00;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i) E[i] = 1, prod[i] = 1 - p[i][n];
E[n] = 0;
vis[n] = true;
for (int i = 1; i <= n; ++i) {
int u = 0;
double mn = 1e18;
for (int j = 1; j <= n; ++j)
if (!vis[j] && E[j] / (1 - prod[j]) < mn)
u = j, mn = E[j] / (1 - prod[j]);
vis[u] = true;
if (u == 1) {
printf("%.10lf\n", E[1] / (1 - prod[1]));
return 0;
}
for (int j = 1; j <= n; ++j)
E[j] += (E[u] / (1 - prod[u])) * p[j][u] * prod[j],
prod[j] *= (1 - p[j][u]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
int n;
bool vis[1050];
double pro[1050][1050], sum[1050], p[1050], dis[1050];
int a[1050];
int main() {
n = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) pro[i][j] = read() * 0.01;
}
for (int i = 1; i <= n; i++) {
sum[i] = 1.0;
p[i] = 1.0;
}
dis[0] = 1145141919810893;
vis[n] = 1;
a[1] = n;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
sum[j] = sum[j] + (dis[a[i - 1]] * pro[j][a[i - 1]] * p[j]);
p[j] = (1 - pro[j][a[i - 1]]) * p[j];
dis[j] = sum[j] / (1 - p[j]);
}
}
int minpos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dis[minpos] > dis[j]) minpos = j;
}
vis[minpos] = 1;
a[i] = minpos;
}
cout << fixed << setprecision(10) << dis[1] << endl;
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
int n;
bool vis[1050];
double pro[1050][1050], sum[1050], p[1050], dis[1050];
int a[1050];
int main() {
n = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) pro[i][j] = read() * 0.01;
}
for (int i = 1; i <= n; i++) {
sum[i] = 1.0;
p[i] = 1.0;
}
dis[0] = 1145141919810893;
vis[n] = 1;
a[1] = n;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
sum[j] = sum[j] + (dis[a[i - 1]] * pro[j][a[i - 1]] * p[j]);
p[j] = (1 - pro[j][a[i - 1]]) * p[j];
dis[j] = sum[j] / (1 - p[j]);
}
}
int minpos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dis[minpos] > dis[j]) minpos = j;
}
vis[minpos] = 1;
a[i] = minpos;
}
cout << fixed << setprecision(10) << dis[1] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename tp>
inline void read(tp &x) {
x = 0;
char c = getchar();
int f = 0;
for (; c < '0' || c > '9'; f |= c == '-', c = getchar())
;
for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
;
if (f) x = -x;
}
const int N = 1111;
int n, book[N];
double p[N][N], d[N], pro[N], pre[N];
int main(void) {
read(n);
for (register int i = 1; i <= (n); i++)
for (register int j = 1; j <= (n); j++)
scanf("%lf", &p[i][j]), p[i][j] /= 100;
for (register int i = 1; i <= (n); i++) d[i] = 1e99, pro[i] = 1;
d[n] = 0;
for (register int i = 1; i <= (n); i++) {
int u = -1;
for (register int j = 1; j <= (n); j++)
if (!book[j] && (u == -1 || d[u] > d[j])) u = j;
if (u == -1) break;
book[u] = true;
for (register int v = 1; v <= (n); v++) {
if (fabs(p[v][u]) < 1e-6 || u == v || book[v]) continue;
double npre = pre[v] + pro[v] * p[v][u] * d[u];
double npro = pro[v] * (1 - p[v][u]);
double cost = (npre + 1) / (1 - npro);
if (d[v] > cost) {
d[v] = cost;
pro[v] = npro;
pre[v] = npre;
}
}
}
printf("%.10f\n", d[1]);
}
|
### Prompt
Please create a solution in Cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename tp>
inline void read(tp &x) {
x = 0;
char c = getchar();
int f = 0;
for (; c < '0' || c > '9'; f |= c == '-', c = getchar())
;
for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar())
;
if (f) x = -x;
}
const int N = 1111;
int n, book[N];
double p[N][N], d[N], pro[N], pre[N];
int main(void) {
read(n);
for (register int i = 1; i <= (n); i++)
for (register int j = 1; j <= (n); j++)
scanf("%lf", &p[i][j]), p[i][j] /= 100;
for (register int i = 1; i <= (n); i++) d[i] = 1e99, pro[i] = 1;
d[n] = 0;
for (register int i = 1; i <= (n); i++) {
int u = -1;
for (register int j = 1; j <= (n); j++)
if (!book[j] && (u == -1 || d[u] > d[j])) u = j;
if (u == -1) break;
book[u] = true;
for (register int v = 1; v <= (n); v++) {
if (fabs(p[v][u]) < 1e-6 || u == v || book[v]) continue;
double npre = pre[v] + pro[v] * p[v][u] * d[u];
double npro = pro[v] * (1 - p[v][u]);
double cost = (npre + 1) / (1 - npro);
if (d[v] > cost) {
d[v] = cost;
pro[v] = npro;
pre[v] = npre;
}
}
}
printf("%.10f\n", d[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1011;
double p[N][N];
double a[N], b[N];
double q[N];
bool vis[N];
const double inf = 1e30;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
double x;
scanf("%lf", &x);
p[i][j] = x;
p[i][j] /= 100;
}
}
for (int i = 1; i <= n; i++) a[i] = b[i] = 1, q[i] = inf;
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
que;
que.push(make_pair(0, n));
q[n] = 0;
while (!que.empty()) {
pair<double, int> now = que.top();
que.pop();
double qw = now.first;
int pos = now.second;
if (vis[pos]) continue;
if (qw > q[pos]) continue;
vis[pos] = true;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
a[i] += b[i] * p[i][pos] * q[pos];
b[i] *= (1 - p[i][pos]);
if (abs(1 - b[i]) <= 1e-12)
q[i] = inf;
else
q[i] = a[i] / (1 - b[i]);
que.push(make_pair(q[i], i));
}
}
printf("%.9f\n", (double)q[1]);
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1011;
double p[N][N];
double a[N], b[N];
double q[N];
bool vis[N];
const double inf = 1e30;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
double x;
scanf("%lf", &x);
p[i][j] = x;
p[i][j] /= 100;
}
}
for (int i = 1; i <= n; i++) a[i] = b[i] = 1, q[i] = inf;
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
que;
que.push(make_pair(0, n));
q[n] = 0;
while (!que.empty()) {
pair<double, int> now = que.top();
que.pop();
double qw = now.first;
int pos = now.second;
if (vis[pos]) continue;
if (qw > q[pos]) continue;
vis[pos] = true;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
a[i] += b[i] * p[i][pos] * q[pos];
b[i] *= (1 - p[i][pos]);
if (abs(1 - b[i]) <= 1e-12)
q[i] = inf;
else
q[i] = a[i] / (1 - b[i]);
que.push(make_pair(q[i], i));
}
}
printf("%.9f\n", (double)q[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double p[1005][1005], q[1005], dis[1005];
int vis[1005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
for (int i = 1; i <= n; i++) {
dis[i] = 1;
q[i] = 1 - p[i][n];
}
dis[n] = 0, vis[n] = 1;
for (int i = 1; i <= n; i++) {
int u;
double mind = 1e50;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dis[j] / (1 - q[j]) < mind)
mind = dis[j] / (1 - q[j]), u = j;
}
vis[u] = 1;
if (u == 1) break;
for (int v = 1; v <= n; v++) {
dis[v] += dis[u] / (1 - q[u]) * p[v][u] * q[v];
q[v] *= 1 - p[v][u];
}
}
printf("%.10lf\n", dis[1] / (1 - q[1]));
return 0;
}
|
### Prompt
Create a solution in cpp for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double p[1005][1005], q[1005], dis[1005];
int vis[1005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
for (int i = 1; i <= n; i++) {
dis[i] = 1;
q[i] = 1 - p[i][n];
}
dis[n] = 0, vis[n] = 1;
for (int i = 1; i <= n; i++) {
int u;
double mind = 1e50;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dis[j] / (1 - q[j]) < mind)
mind = dis[j] / (1 - q[j]), u = j;
}
vis[u] = 1;
if (u == 1) break;
for (int v = 1; v <= n; v++) {
dis[v] += dis[u] / (1 - q[u]) * p[v][u] * q[v];
q[v] *= 1 - p[v][u];
}
}
printf("%.10lf\n", dis[1] / (1 - q[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1010][1010];
bool book[1010];
double f[1010], s[1010], sp[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
f[i] = 1e18;
sp[i] = 1;
s[i] = 0;
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
f[n] = 0;
for (int i = 1; i <= n; i++) {
int u = 0;
double mn = 1e18;
for (int j = 1; j <= n; j++)
if (!book[j] && f[j] < mn) {
mn = f[j];
u = j;
}
book[u] = true;
for (int v = 1; v <= n; v++)
if (!book[v]) {
double P = p[v][u];
s[v] += P * sp[v] * f[u];
sp[v] *= (1 - P);
if (1 - sp[v] > 1e-8) f[v] = (s[v] + 1) / (1 - sp[v]);
}
}
printf("%.10lf\n", f[1]);
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1010][1010];
bool book[1010];
double f[1010], s[1010], sp[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
f[i] = 1e18;
sp[i] = 1;
s[i] = 0;
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
f[n] = 0;
for (int i = 1; i <= n; i++) {
int u = 0;
double mn = 1e18;
for (int j = 1; j <= n; j++)
if (!book[j] && f[j] < mn) {
mn = f[j];
u = j;
}
book[u] = true;
for (int v = 1; v <= n; v++)
if (!book[v]) {
double P = p[v][u];
s[v] += P * sp[v] * f[u];
sp[v] *= (1 - P);
if (1 - sp[v] > 1e-8) f[v] = (s[v] + 1) / (1 - sp[v]);
}
}
printf("%.10lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
i >> p.first >> p.second;
return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
for (auto& v : t) {
i >> v;
}
return i;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
o << p.first << ' ' << p.second;
return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
if (t.empty()) o << '\n';
for (size_t i = 0; i < t.size(); ++i) {
o << t[i] << " \n"[i == t.size() - 1];
}
return o;
}
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T>
bool in(T a, T b, T c) {
return a <= b && b < c;
}
unsigned int logceil(int first) {
return 8 * sizeof(int) - __builtin_clz(first);
}
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
hash<T> t;
hash<U> u;
size_t operator()(const pair<T, U>& p) const {
return t(p.first) ^ (u(p.second) << 7);
}
};
} // namespace std
template <typename T, typename F>
T bsh(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
l = m + 1;
r = m;
} else {
h = m - 1;
}
}
return r;
}
template <typename F>
double bshd(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
l = m;
} else {
h = m;
}
}
return (l + h) / 2;
}
template <typename T, typename F>
T bsl(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
h = m - 1;
r = m;
} else {
l = m + 1;
}
}
return r;
}
template <typename F>
double bsld(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
h = m;
} else {
l = m;
}
}
return (l + h) / 2;
}
template <typename T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
template <typename T>
class vector2 : public vector<vector<T>> {
public:
vector2() {}
vector2(size_t a, size_t b, T t = T())
: vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T>
class vector3 : public vector<vector2<T>> {
public:
vector3() {}
vector3(size_t a, size_t b, size_t c, T t = T())
: vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T>
class vector4 : public vector<vector3<T>> {
public:
vector4() {}
vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
: vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T>
class vector5 : public vector<vector4<T>> {
public:
vector5() {}
vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
: vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};
class intergalaxytrips {
public:
void solve(istream& cin, ostream& cout) {
int N;
cin >> N;
vector2<int> W(N, N, 0);
cin >> W;
vector2<long double> Z(N, N, 0);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
Z[i][j] = W[i][j] / 100.0;
}
}
vector<long double> P(N, 1e10), A(N, 0), B(N, 1);
P[N - 1] = 0;
for (int i = 0; i < N - 1; ++i) B[i] = 1 - Z[i][N - 1];
while (P[0] > 1e9) {
pair<long double, int> Q(1e20, -1);
for (int i = 0; i < N; ++i) {
if (P[i] > 1e9 && B[i] < 1 - 1e-9) {
Q = min(Q, {(1 + A[i]) / (1 - B[i]), i});
}
}
int j = Q.second;
if (j != -1) {
P[j] = Q.first;
for (int i = 0; i < N; ++i) {
if (P[i] > 1e9) {
A[i] += B[i] * P[j] * Z[i][j];
B[i] *= 1 - Z[i][j];
if (B[i] < 1e-10) {
B[i] = 0;
}
}
}
}
}
cout << fixed << setprecision(15) << P[0] << endl;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
intergalaxytrips solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
i >> p.first >> p.second;
return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
for (auto& v : t) {
i >> v;
}
return i;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
o << p.first << ' ' << p.second;
return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
if (t.empty()) o << '\n';
for (size_t i = 0; i < t.size(); ++i) {
o << t[i] << " \n"[i == t.size() - 1];
}
return o;
}
template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
template <typename T>
bool in(T a, T b, T c) {
return a <= b && b < c;
}
unsigned int logceil(int first) {
return 8 * sizeof(int) - __builtin_clz(first);
}
namespace std {
template <typename T, typename U>
struct hash<pair<T, U>> {
hash<T> t;
hash<U> u;
size_t operator()(const pair<T, U>& p) const {
return t(p.first) ^ (u(p.second) << 7);
}
};
} // namespace std
template <typename T, typename F>
T bsh(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
l = m + 1;
r = m;
} else {
h = m - 1;
}
}
return r;
}
template <typename F>
double bshd(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
l = m;
} else {
h = m;
}
}
return (l + h) / 2;
}
template <typename T, typename F>
T bsl(T l, T h, const F& f) {
T r = -1, m;
while (l <= h) {
m = (l + h) / 2;
if (f(m)) {
h = m - 1;
r = m;
} else {
l = m + 1;
}
}
return r;
}
template <typename F>
double bsld(double l, double h, const F& f, double p = 1e-9) {
unsigned int r = 3 + (unsigned int)log2((h - l) / p);
while (r--) {
double m = (l + h) / 2;
if (f(m)) {
h = m;
} else {
l = m;
}
}
return (l + h) / 2;
}
template <typename T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
template <typename T>
class vector2 : public vector<vector<T>> {
public:
vector2() {}
vector2(size_t a, size_t b, T t = T())
: vector<vector<T>>(a, vector<T>(b, t)) {}
};
template <typename T>
class vector3 : public vector<vector2<T>> {
public:
vector3() {}
vector3(size_t a, size_t b, size_t c, T t = T())
: vector<vector2<T>>(a, vector2<T>(b, c, t)) {}
};
template <typename T>
class vector4 : public vector<vector3<T>> {
public:
vector4() {}
vector4(size_t a, size_t b, size_t c, size_t d, T t = T())
: vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {}
};
template <typename T>
class vector5 : public vector<vector4<T>> {
public:
vector5() {}
vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T())
: vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {}
};
class intergalaxytrips {
public:
void solve(istream& cin, ostream& cout) {
int N;
cin >> N;
vector2<int> W(N, N, 0);
cin >> W;
vector2<long double> Z(N, N, 0);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
Z[i][j] = W[i][j] / 100.0;
}
}
vector<long double> P(N, 1e10), A(N, 0), B(N, 1);
P[N - 1] = 0;
for (int i = 0; i < N - 1; ++i) B[i] = 1 - Z[i][N - 1];
while (P[0] > 1e9) {
pair<long double, int> Q(1e20, -1);
for (int i = 0; i < N; ++i) {
if (P[i] > 1e9 && B[i] < 1 - 1e-9) {
Q = min(Q, {(1 + A[i]) / (1 - B[i]), i});
}
}
int j = Q.second;
if (j != -1) {
P[j] = Q.first;
for (int i = 0; i < N; ++i) {
if (P[i] > 1e9) {
A[i] += B[i] * P[j] * Z[i][j];
B[i] *= 1 - Z[i][j];
if (B[i] < 1e-10) {
B[i] = 0;
}
}
}
}
}
cout << fixed << setprecision(15) << P[0] << endl;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
intergalaxytrips solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
bool fix[N];
double p[N][N], e[N], X[N], Y[N];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout << setprecision(6) << fixed;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
int x;
cin >> x;
p[i][j] = x / 100.;
}
fill(e, e + N, 1e12);
fill(Y, Y + N, 1.);
fill(X, X + N, 0.);
e[n - 1] = 0., fix[n - 1] = true, X[n - 1] = 0., Y[n - 1] = 0.;
int v = n - 1;
bool fnd = true;
while (fnd) {
fnd = false;
for (int i = 0; i < n; i++)
if (!fix[i]) {
X[i] += Y[i] * p[i][v] * (e[v] + 1.);
Y[i] *= 1. - p[i][v];
e[i] = (X[i] + Y[i]) / (1. - Y[i]);
}
double mn = 1e9;
for (int i = 0; i < n; i++)
if (!fix[i] && e[i] < mn) mn = e[i], v = i, fnd = true;
fix[v] = true;
}
cout << e[0] << '\n';
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
bool fix[N];
double p[N][N], e[N], X[N], Y[N];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout << setprecision(6) << fixed;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
int x;
cin >> x;
p[i][j] = x / 100.;
}
fill(e, e + N, 1e12);
fill(Y, Y + N, 1.);
fill(X, X + N, 0.);
e[n - 1] = 0., fix[n - 1] = true, X[n - 1] = 0., Y[n - 1] = 0.;
int v = n - 1;
bool fnd = true;
while (fnd) {
fnd = false;
for (int i = 0; i < n; i++)
if (!fix[i]) {
X[i] += Y[i] * p[i][v] * (e[v] + 1.);
Y[i] *= 1. - p[i][v];
e[i] = (X[i] + Y[i]) / (1. - Y[i]);
}
double mn = 1e9;
for (int i = 0; i < n; i++)
if (!fix[i] && e[i] < mn) mn = e[i], v = i, fnd = true;
fix[v] = true;
}
cout << e[0] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long poww(long long a, long long b, long long md) {
return (!b ? 1
: (b & 1 ? a * poww(a * a % md, b / 2, md) % md
: poww(a * a % md, b / 2, md) % md));
}
const long long MAXN = 1e3 + 10;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
int n;
long double A[MAXN][MAXN], dist[MAXN], B[MAXN], C[MAXN], P[MAXN];
bool marked[MAXN];
int main() {
fill(dist, dist + MAXN, INF);
fill(B, B + MAXN, INF);
fill(P, P + MAXN, 1);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int t;
scanf("%d", &t);
A[i][j] = t / 100.0;
}
}
dist[n] = 0;
for (int t = 0; t < n; t++) {
int v = 0;
for (int i = 1; i <= n; i++)
if (dist[i] < dist[v] && !marked[i]) v = i;
if (v == 0) break;
marked[v] = true;
for (int u = 1; u <= n; u++) {
if (marked[u] || B[u] <= dist[v]) continue;
C[u] += P[u] * A[u][v] * dist[v];
P[u] *= (1 - A[u][v]);
dist[u] = (C[u] + 1) * (1 / (1 - P[u]));
if (A[v][u] > 0) B[u] = min(B[u], dist[u] + 1 / (A[u][v]));
}
}
printf("%.10Lf", dist[1]);
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long poww(long long a, long long b, long long md) {
return (!b ? 1
: (b & 1 ? a * poww(a * a % md, b / 2, md) % md
: poww(a * a % md, b / 2, md) % md));
}
const long long MAXN = 1e3 + 10;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
int n;
long double A[MAXN][MAXN], dist[MAXN], B[MAXN], C[MAXN], P[MAXN];
bool marked[MAXN];
int main() {
fill(dist, dist + MAXN, INF);
fill(B, B + MAXN, INF);
fill(P, P + MAXN, 1);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int t;
scanf("%d", &t);
A[i][j] = t / 100.0;
}
}
dist[n] = 0;
for (int t = 0; t < n; t++) {
int v = 0;
for (int i = 1; i <= n; i++)
if (dist[i] < dist[v] && !marked[i]) v = i;
if (v == 0) break;
marked[v] = true;
for (int u = 1; u <= n; u++) {
if (marked[u] || B[u] <= dist[v]) continue;
C[u] += P[u] * A[u][v] * dist[v];
P[u] *= (1 - A[u][v]);
dist[u] = (C[u] + 1) * (1 / (1 - P[u]));
if (A[v][u] > 0) B[u] = min(B[u], dist[u] + 1 / (A[u][v]));
}
}
printf("%.10Lf", dist[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b) {
return a;
} else {
return b;
}
}
long long min(long long a, long long b) {
if (a < b) {
return a;
} else {
return b;
}
}
vector<string> split(const string& s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c)) v.emplace_back(x);
return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", ";
err(++it, args...);
}
template <typename T>
struct number_iterator : std::iterator<random_access_iterator_tag, T> {
T v;
number_iterator(T _v) : v(_v) {}
operator T&() { return v; }
T operator*() const { return v; }
};
template <typename T>
struct number_range {
T b, e;
number_range(T b, T e) : b(b), e(e) {}
number_iterator<T> begin() { return b; }
number_iterator<T> end() { return e; }
};
template <typename T>
number_range<T> range(T e) {
return number_range<T>(1, e + 1);
}
template <typename T>
number_range<T> range(T b, T e) {
return number_range<T>(b, e + 1);
}
int n, a[2001][2001];
double v[2001];
double re[2001], b[2001];
struct E {
int id;
double val;
E() {}
E(int id, double val) : id(id), val(val) {}
};
bool operator<(E u, E v) { return u.val > v.val; }
priority_queue<E> Q;
int f[2001];
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); i++)
for (int j = (1); j <= (n); j++) scanf("%d", &a[i][j]);
for (int i = (1); i <= (n); i++) v[i] = -1.00;
for (int i = (1); i <= (n); i++) f[i] = 1;
for (int i = (1); i <= (n); i++) re[i] = 1.00;
Q.push(E(n, 0.00));
while (Q.size()) {
E u = Q.top();
Q.pop();
if (f[u.id]) {
v[u.id] = u.val;
f[u.id] = 0;
for (int i = (1); i <= (n); i++)
if (f[i] && a[i][u.id] && re[i]) {
double x = (re[i]) * (a[i][u.id]) / 100.00;
double nb = x * (1 + v[u.id]);
double left = re[i] - x;
double nv =
(v[i] * (1 - re[i]) - (v[i] < -0.55 ? 0.00 : re[i]) + nb + left) /
(1 - left);
if (v[i] < -0.55 || v[i] > nv) {
Q.push(E(i, v[i] = nv));
re[i] -= x;
}
}
}
}
printf("%.12lf\n", v[1]);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b) {
return a;
} else {
return b;
}
}
long long min(long long a, long long b) {
if (a < b) {
return a;
} else {
return b;
}
}
vector<string> split(const string& s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c)) v.emplace_back(x);
return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", ";
err(++it, args...);
}
template <typename T>
struct number_iterator : std::iterator<random_access_iterator_tag, T> {
T v;
number_iterator(T _v) : v(_v) {}
operator T&() { return v; }
T operator*() const { return v; }
};
template <typename T>
struct number_range {
T b, e;
number_range(T b, T e) : b(b), e(e) {}
number_iterator<T> begin() { return b; }
number_iterator<T> end() { return e; }
};
template <typename T>
number_range<T> range(T e) {
return number_range<T>(1, e + 1);
}
template <typename T>
number_range<T> range(T b, T e) {
return number_range<T>(b, e + 1);
}
int n, a[2001][2001];
double v[2001];
double re[2001], b[2001];
struct E {
int id;
double val;
E() {}
E(int id, double val) : id(id), val(val) {}
};
bool operator<(E u, E v) { return u.val > v.val; }
priority_queue<E> Q;
int f[2001];
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); i++)
for (int j = (1); j <= (n); j++) scanf("%d", &a[i][j]);
for (int i = (1); i <= (n); i++) v[i] = -1.00;
for (int i = (1); i <= (n); i++) f[i] = 1;
for (int i = (1); i <= (n); i++) re[i] = 1.00;
Q.push(E(n, 0.00));
while (Q.size()) {
E u = Q.top();
Q.pop();
if (f[u.id]) {
v[u.id] = u.val;
f[u.id] = 0;
for (int i = (1); i <= (n); i++)
if (f[i] && a[i][u.id] && re[i]) {
double x = (re[i]) * (a[i][u.id]) / 100.00;
double nb = x * (1 + v[u.id]);
double left = re[i] - x;
double nv =
(v[i] * (1 - re[i]) - (v[i] < -0.55 ? 0.00 : re[i]) + nb + left) /
(1 - left);
if (v[i] < -0.55 || v[i] > nv) {
Q.push(E(i, v[i] = nv));
re[i] -= x;
}
}
}
}
printf("%.12lf\n", v[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
static double p[1005][1005], d[1005], a[1005], b[1005];
scanf("%d", &n);
for (int i = 1; i <= n; ++i) d[i] = 1e30, a[i] = b[i] = 1.00000;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
scanf("%lf", &p[i][j]);
p[i][j] = p[i][j] / 100.0;
}
d[n] = 0;
static bool v[1005];
while (1) {
double minn = 1e30;
int f = -1;
for (int i = 1; i <= n; ++i)
if (!v[i] && minn > d[i]) {
minn = d[i];
f = i;
}
if (f == -1) break;
v[f] = 1;
for (int i = 1; i <= n; ++i)
if (!v[i]) {
b[i] += a[i] * d[f] * p[i][f];
a[i] *= (1 - p[i][f]);
d[i] = b[i] / (1 - a[i]);
}
}
printf("%.10lf\n", d[1]);
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
static double p[1005][1005], d[1005], a[1005], b[1005];
scanf("%d", &n);
for (int i = 1; i <= n; ++i) d[i] = 1e30, a[i] = b[i] = 1.00000;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
scanf("%lf", &p[i][j]);
p[i][j] = p[i][j] / 100.0;
}
d[n] = 0;
static bool v[1005];
while (1) {
double minn = 1e30;
int f = -1;
for (int i = 1; i <= n; ++i)
if (!v[i] && minn > d[i]) {
minn = d[i];
f = i;
}
if (f == -1) break;
v[f] = 1;
for (int i = 1; i <= n; ++i)
if (!v[i]) {
b[i] += a[i] * d[f] * p[i][f];
a[i] *= (1 - p[i][f]);
d[i] = b[i] / (1 - a[i]);
}
}
printf("%.10lf\n", d[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
const int maxn = 1010;
double f[maxn], e[maxn];
inline double calc(int id) { return (f[id] + 1) / (1 - e[id]); }
int p[maxn][maxn], vis[maxn], n;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0);
std::cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) std::cin >> p[i][j];
for (int i = 1; i < n; ++i) f[i] = 0, e[i] = 1;
f[n] = -1;
for (int i = 1; i <= n; ++i) {
int pos = -1;
for (int j = 1; j <= n; ++j)
if (!vis[j] && (pos == -1 || calc(j) < calc(pos))) pos = j;
vis[pos] = 1;
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
f[j] += e[j] * calc(pos) * p[j][pos] / 100;
e[j] *= 1 - p[j][pos] / 100.;
}
}
std::cout << std::setprecision(10) << calc(1) << '\n';
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int maxn = 1010;
double f[maxn], e[maxn];
inline double calc(int id) { return (f[id] + 1) / (1 - e[id]); }
int p[maxn][maxn], vis[maxn], n;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0);
std::cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) std::cin >> p[i][j];
for (int i = 1; i < n; ++i) f[i] = 0, e[i] = 1;
f[n] = -1;
for (int i = 1; i <= n; ++i) {
int pos = -1;
for (int j = 1; j <= n; ++j)
if (!vis[j] && (pos == -1 || calc(j) < calc(pos))) pos = j;
vis[pos] = 1;
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
f[j] += e[j] * calc(pos) * p[j][pos] / 100;
e[j] *= 1 - p[j][pos] / 100.;
}
}
std::cout << std::setprecision(10) << calc(1) << '\n';
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline int chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar())
sum = (sum << 3) + (sum << 1) + (ch - '0');
return sum * fl;
}
const int maxn = 1000 + 5;
int N;
double P[maxn][maxn];
double sum[maxn], pre[maxn], F[maxn];
bool vis[maxn];
inline void update(int first) {
for (int i = 1; i <= N; ++i)
if (!vis[i]) {
sum[i] += F[first] * P[i][first] * pre[i];
pre[i] *= 1 - P[i][first];
F[i] = (1 + sum[i]) / (1 - pre[i]);
}
}
inline void solve() {
for (int i = 1; i <= N; ++i) pre[i] = 1;
vis[N] = 1;
update(N);
for (int i = 1; i < N; ++i) {
int id = 0;
for (int j = 1; j <= N; ++j)
if (!vis[j] && (id == 0 || F[j] < F[id])) id = j;
vis[id] = 1;
update(id);
}
printf("%.10lf\n", F[1]);
}
inline void input() {
N = read<int>();
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j) P[i][j] = read<int>() / 100.0;
}
int main() {
input();
solve();
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline int chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline T read() {
T sum = 0, fl = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar())
sum = (sum << 3) + (sum << 1) + (ch - '0');
return sum * fl;
}
const int maxn = 1000 + 5;
int N;
double P[maxn][maxn];
double sum[maxn], pre[maxn], F[maxn];
bool vis[maxn];
inline void update(int first) {
for (int i = 1; i <= N; ++i)
if (!vis[i]) {
sum[i] += F[first] * P[i][first] * pre[i];
pre[i] *= 1 - P[i][first];
F[i] = (1 + sum[i]) / (1 - pre[i]);
}
}
inline void solve() {
for (int i = 1; i <= N; ++i) pre[i] = 1;
vis[N] = 1;
update(N);
for (int i = 1; i < N; ++i) {
int id = 0;
for (int j = 1; j <= N; ++j)
if (!vis[j] && (id == 0 || F[j] < F[id])) id = j;
vis[id] = 1;
update(id);
}
printf("%.10lf\n", F[1]);
}
inline void input() {
N = read<int>();
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j) P[i][j] = read<int>() / 100.0;
}
int main() {
input();
solve();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbs(string str, T t) {
cerr << str << " : " << t << "\n";
}
template <class T, class... S>
void dbs(string str, T t, S... s) {
int idx = str.find(',');
cerr << str.substr(0, idx) << " : " << t << ",";
dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const map<S, T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
void prc(T a, T b) {
cerr << "[";
for (T i = a; i != b; ++i) {
if (i != a) cerr << ", ";
cerr << *i;
}
cerr << "]\n";
}
const int N = 1010;
int prob[N + 1][N + 1];
double q[N + 1];
double answer1[N + 1];
double pq[N + 1];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(0);
int n;
cin >> n;
for (int i = (int)(0); i <= (int)(n - 1); i++)
for (int j = (int)(0); j <= (int)(n - 1); j++) cin >> prob[i][j];
q[n - 1] = 0.0;
for (int i = (int)(0); i <= (int)(n - 1); i++)
q[i] = 0.0, pq[i] = 1.0, answer1[i] = 1e9;
pq[n - 1] = 0, answer1[n - 1] = 0;
set<pair<double, int>> s1;
s1.insert(make_pair(0.0, n - 1));
int times = 0;
while (!s1.empty() and times <= 10000) {
int u = s1.begin()->second;
s1.erase(s1.begin());
for (int i = (int)(0); i <= (int)(n - 1); i++) {
if (answer1[i] < answer1[u] + 1e-9) continue;
if (!prob[i][u]) continue;
s1.erase(make_pair(answer1[i], i));
q[i] += pq[i] * 0.01 * prob[i][u] * answer1[u];
pq[i] *= (1 - 0.01 * prob[i][u]);
answer1[i] = (1 + q[i]) / (1 - pq[i]);
s1.insert(make_pair(answer1[i], i));
}
times++;
}
cout << fixed << setprecision(10) << answer1[0] << "\n";
}
|
### Prompt
Develop a solution in CPP to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbs(string str, T t) {
cerr << str << " : " << t << "\n";
}
template <class T, class... S>
void dbs(string str, T t, S... s) {
int idx = str.find(',');
cerr << str.substr(0, idx) << " : " << t << ",";
dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const map<S, T>& p) {
os << "[ ";
for (auto& it : p) os << it << " ";
return os << "]";
}
template <class T>
void prc(T a, T b) {
cerr << "[";
for (T i = a; i != b; ++i) {
if (i != a) cerr << ", ";
cerr << *i;
}
cerr << "]\n";
}
const int N = 1010;
int prob[N + 1][N + 1];
double q[N + 1];
double answer1[N + 1];
double pq[N + 1];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(0);
int n;
cin >> n;
for (int i = (int)(0); i <= (int)(n - 1); i++)
for (int j = (int)(0); j <= (int)(n - 1); j++) cin >> prob[i][j];
q[n - 1] = 0.0;
for (int i = (int)(0); i <= (int)(n - 1); i++)
q[i] = 0.0, pq[i] = 1.0, answer1[i] = 1e9;
pq[n - 1] = 0, answer1[n - 1] = 0;
set<pair<double, int>> s1;
s1.insert(make_pair(0.0, n - 1));
int times = 0;
while (!s1.empty() and times <= 10000) {
int u = s1.begin()->second;
s1.erase(s1.begin());
for (int i = (int)(0); i <= (int)(n - 1); i++) {
if (answer1[i] < answer1[u] + 1e-9) continue;
if (!prob[i][u]) continue;
s1.erase(make_pair(answer1[i], i));
q[i] += pq[i] * 0.01 * prob[i][u] * answer1[u];
pq[i] *= (1 - 0.01 * prob[i][u]);
answer1[i] = (1 + q[i]) / (1 - pq[i]);
s1.insert(make_pair(answer1[i], i));
}
times++;
}
cout << fixed << setprecision(10) << answer1[0] << "\n";
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1011;
const double Eps = 1e-8;
int p[N][N];
double acc[N];
double old[N];
double ans[N];
bool vis[N];
int main() {
ios ::sync_with_stdio(false);
int n;
cin >> n;
if (n == 1) {
cout << 0 << '\n';
return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> p[i][j];
ans[n] = 1;
vis[n] = true;
for (int i = 1; i <= n; i++) {
old[i] = 1 - p[i][n] / 100.0;
acc[i] = 0;
}
while (true) {
int tid = -1;
double tans = 1e60;
for (int i = 1; i <= n; i++)
if (!vis[i] && old[i] + Eps < 1 && (acc[i] + 1) / (1 - old[i]) < tans) {
tid = i;
tans = (acc[i] + 1) / (1 - old[i]);
}
if (tid == 1) {
cout << setprecision(12) << tans << '\n';
return 0;
}
vis[tid] = true;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
acc[i] += old[i] * p[i][tid] / 100.0 * tans;
old[i] *= 1 - p[i][tid] / 100.0;
}
}
}
|
### Prompt
Generate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1011;
const double Eps = 1e-8;
int p[N][N];
double acc[N];
double old[N];
double ans[N];
bool vis[N];
int main() {
ios ::sync_with_stdio(false);
int n;
cin >> n;
if (n == 1) {
cout << 0 << '\n';
return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> p[i][j];
ans[n] = 1;
vis[n] = true;
for (int i = 1; i <= n; i++) {
old[i] = 1 - p[i][n] / 100.0;
acc[i] = 0;
}
while (true) {
int tid = -1;
double tans = 1e60;
for (int i = 1; i <= n; i++)
if (!vis[i] && old[i] + Eps < 1 && (acc[i] + 1) / (1 - old[i]) < tans) {
tid = i;
tans = (acc[i] + 1) / (1 - old[i]);
}
if (tid == 1) {
cout << setprecision(12) << tans << '\n';
return 0;
}
vis[tid] = true;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
acc[i] += old[i] * p[i][tid] / 100.0 * tans;
old[i] *= 1 - p[i][tid] / 100.0;
}
}
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int v = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = v * 10 + c - '0';
c = getchar();
}
return v * f;
}
const int Maxn = 1005;
double p[Maxn][Maxn];
int n;
double f[Maxn], s[Maxn], S[Maxn], F[Maxn];
bool pp[Maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int v;
scanf("%d", &v);
p[i][j] = (double)v / 100.0;
}
}
for (int i = 0; i < Maxn; i++) f[i] = 1e18, s[i] = 1.0;
f[n] = 0;
for (int _ = 1; _ <= n; _++) {
int Mn = 0;
for (int i = 1; i <= n; i++) {
if (f[i] < f[Mn] && !pp[i]) {
Mn = i;
}
}
pp[Mn] = 1;
for (int i = 1; i <= n; i++) {
if (!pp[i] && p[i][Mn]) {
double nG = s[i] * p[i][Mn];
double sG = S[i] + nG;
double sf = F[i] + f[Mn] * nG;
double E = (sf + 1) / sG;
f[i] = E;
F[i] = sf;
S[i] = sG;
s[i] *= (1.0 - p[i][Mn]);
}
}
}
printf("%.10lf\n", f[1]);
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int v = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = v * 10 + c - '0';
c = getchar();
}
return v * f;
}
const int Maxn = 1005;
double p[Maxn][Maxn];
int n;
double f[Maxn], s[Maxn], S[Maxn], F[Maxn];
bool pp[Maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int v;
scanf("%d", &v);
p[i][j] = (double)v / 100.0;
}
}
for (int i = 0; i < Maxn; i++) f[i] = 1e18, s[i] = 1.0;
f[n] = 0;
for (int _ = 1; _ <= n; _++) {
int Mn = 0;
for (int i = 1; i <= n; i++) {
if (f[i] < f[Mn] && !pp[i]) {
Mn = i;
}
}
pp[Mn] = 1;
for (int i = 1; i <= n; i++) {
if (!pp[i] && p[i][Mn]) {
double nG = s[i] * p[i][Mn];
double sG = S[i] + nG;
double sf = F[i] + f[Mn] * nG;
double E = (sf + 1) / sG;
f[i] = E;
F[i] = sf;
S[i] = sG;
s[i] *= (1.0 - p[i][Mn]);
}
}
}
printf("%.10lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
const int N = 1005;
const long double eps = 1e-10;
int n, x;
bool vis[N];
long double p[N][N], f[N], prod[N], sum[N];
bool zero(long double x) { return std::abs(x) < eps; }
void update(int x) {
vis[x] = 1;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
sum[i] += (1 + f[x]) * p[i][x] * prod[i];
prod[i] *= (1 - p[i][x]);
}
}
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::cin >> n;
for (int i = 1; i <= n; ++i) prod[i] = 1, sum[i] = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) std::cin >> x, p[i][j] = x / 100.;
f[n] = 0, update(n);
for (int i = 1; i < n; ++i) {
int x = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j] && !zero(1 - prod[j])) {
f[j] = (sum[j] + prod[j]) / (1 - prod[j]);
if (!x || f[j] < f[x]) x = j;
}
if (x == 1) break;
update(x);
}
std::cout << std::fixed << std::setprecision(20) << f[1] << std::endl;
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 1005;
const long double eps = 1e-10;
int n, x;
bool vis[N];
long double p[N][N], f[N], prod[N], sum[N];
bool zero(long double x) { return std::abs(x) < eps; }
void update(int x) {
vis[x] = 1;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
sum[i] += (1 + f[x]) * p[i][x] * prod[i];
prod[i] *= (1 - p[i][x]);
}
}
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::cin >> n;
for (int i = 1; i <= n; ++i) prod[i] = 1, sum[i] = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) std::cin >> x, p[i][j] = x / 100.;
f[n] = 0, update(n);
for (int i = 1; i < n; ++i) {
int x = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j] && !zero(1 - prod[j])) {
f[j] = (sum[j] + prod[j]) / (1 - prod[j]);
if (!x || f[j] < f[x]) x = j;
}
if (x == 1) break;
update(x);
}
std::cout << std::fixed << std::setprecision(20) << f[1] << std::endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
bool SR(int &x) { return scanf("%d", &x) == 1; }
bool SR(long long &x) { return scanf("%lld", &x) == 1; }
bool SR(double &x) { return scanf("%lf", &x) == 1; }
bool SR(char *s) { return scanf("%s", s) == 1; }
bool RI() { return true; }
template <typename I, typename... T>
bool RI(I &x, T &...tail) {
return SR(x) && RI(tail...);
}
void SP(const int x) { printf("%d", x); }
void SP(const long long x) { printf("%lld", x); }
void SP(const double x) { printf("%.16lf", x); }
void SP(const char *s) { printf("%s", s); }
void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I x, const T... tail) {
SP(x);
if (sizeof...(tail)) putchar(' ');
PL(tail...);
}
const int maxn = 1e3 + 3;
long double p[maxn][maxn];
int n;
void read() {
RI(n);
for (int i = (1); i <= int(n); i++)
for (int j = (1); j <= int(n); j++) {
int x;
RI(x);
p[i][j] = x;
}
}
struct Want {
long double rp, sum;
Want() : rp(1), sum(1) {}
void add(long double d, long double pp) {
sum += rp * pp * d;
rp *= (1 - pp);
}
long double get() { return sum / (1 - rp); }
};
Want w[maxn];
bitset<maxn> vis;
void relax(int k) {
vis[k] = 1;
long double d = w[k].get();
for (int i = (1); i <= int(n); i++)
if (!vis[i]) w[i].add(d, p[i][k]);
}
long double eps = 1e-18;
void build() {
for (int i = (1); i <= int(n); i++)
for (int j = (1); j <= int(n); j++) p[i][j] = max(p[i][j] / 100, eps);
w[n].rp = w[n].sum = 0;
relax(n);
while ((int)vis.count() < n) {
int id = 0;
long double d = 1;
for (int i = (1); i <= int(n); i++)
if (!vis[i])
if (!id || w[i].get() < d) d = w[id = i].get();
relax(id);
}
}
void sol() { PL((double)w[1].get()); }
int main() {
read();
build();
sol();
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
bool SR(int &x) { return scanf("%d", &x) == 1; }
bool SR(long long &x) { return scanf("%lld", &x) == 1; }
bool SR(double &x) { return scanf("%lf", &x) == 1; }
bool SR(char *s) { return scanf("%s", s) == 1; }
bool RI() { return true; }
template <typename I, typename... T>
bool RI(I &x, T &...tail) {
return SR(x) && RI(tail...);
}
void SP(const int x) { printf("%d", x); }
void SP(const long long x) { printf("%lld", x); }
void SP(const double x) { printf("%.16lf", x); }
void SP(const char *s) { printf("%s", s); }
void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I x, const T... tail) {
SP(x);
if (sizeof...(tail)) putchar(' ');
PL(tail...);
}
const int maxn = 1e3 + 3;
long double p[maxn][maxn];
int n;
void read() {
RI(n);
for (int i = (1); i <= int(n); i++)
for (int j = (1); j <= int(n); j++) {
int x;
RI(x);
p[i][j] = x;
}
}
struct Want {
long double rp, sum;
Want() : rp(1), sum(1) {}
void add(long double d, long double pp) {
sum += rp * pp * d;
rp *= (1 - pp);
}
long double get() { return sum / (1 - rp); }
};
Want w[maxn];
bitset<maxn> vis;
void relax(int k) {
vis[k] = 1;
long double d = w[k].get();
for (int i = (1); i <= int(n); i++)
if (!vis[i]) w[i].add(d, p[i][k]);
}
long double eps = 1e-18;
void build() {
for (int i = (1); i <= int(n); i++)
for (int j = (1); j <= int(n); j++) p[i][j] = max(p[i][j] / 100, eps);
w[n].rp = w[n].sum = 0;
relax(n);
while ((int)vis.count() < n) {
int id = 0;
long double d = 1;
for (int i = (1); i <= int(n); i++)
if (!vis[i])
if (!id || w[i].get() < d) d = w[id = i].get();
relax(id);
}
}
void sol() { PL((double)w[1].get()); }
int main() {
read();
build();
sol();
return 0;
}
```
|
#include <bits/stdc++.h>
const int N = 1005;
int n, p[N][N];
double f[N], g[N];
bool vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &p[i][j]);
for (int i = 1; i < n; i++) f[i] = g[i] = 1;
for (int i = 1; i <= n; i++) {
double mn = 1e9;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && g[j] < 1 && f[j] / (1 - g[j]) < mn)
mn = f[j] / (1 - g[j]), pos = j;
if (!pos) break;
vis[pos] = 1;
f[pos] /= (1 - g[pos]);
for (int j = 1; j <= n; j++)
if (!vis[j]) {
f[j] += g[j] * f[pos] * ((double)p[j][pos] / 100);
g[j] *= (double)(100 - p[j][pos]) / 100;
}
}
printf("%.10lf", f[1]);
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int N = 1005;
int n, p[N][N];
double f[N], g[N];
bool vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &p[i][j]);
for (int i = 1; i < n; i++) f[i] = g[i] = 1;
for (int i = 1; i <= n; i++) {
double mn = 1e9;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && g[j] < 1 && f[j] / (1 - g[j]) < mn)
mn = f[j] / (1 - g[j]), pos = j;
if (!pos) break;
vis[pos] = 1;
f[pos] /= (1 - g[pos]);
for (int j = 1; j <= n; j++)
if (!vis[j]) {
f[j] += g[j] * f[pos] * ((double)p[j][pos] / 100);
g[j] *= (double)(100 - p[j][pos]) / 100;
}
}
printf("%.10lf", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const long double DINF = pow(2, 100);
const int MAXN = 1007;
const long double EPS = 1e-9;
int n;
long double p[MAXN][MAXN];
long double dp[MAXN];
long double nv[MAXN];
long double ap[MAXN];
bool vis[MAXN];
int ord[MAXN];
void init();
void input();
void work();
int main() {
init();
input();
work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
scanf("%d", &n);
int x;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
scanf("%d", &x), p[i][j] = static_cast<long double>(x) / 100;
}
void work() {
dp[n] = 0;
ord[1] = n;
for (int i = 1; i < n; ++i)
nv[i] = (p[i][n] > EPS) ? (1 / p[i][n]) : DINF, ap[i] = p[i][n];
nv[n] = DINF;
vis[n] = true;
int mp;
for (int i = 2; i <= n; ++i) {
mp = min_element(nv + 1, nv + 1 + n) - nv;
dp[mp] = nv[mp];
if (mp == 1) break;
ord[i] = mp;
nv[mp] = DINF;
vis[mp] = true;
for (int j = 1; j <= n; ++j) {
if (!vis[j]) {
if (p[j][mp] > EPS) {
if (ap[j] > EPS) {
nv[j] *= ap[j];
nv[j] += (1 - ap[j]) * p[j][mp] * dp[mp];
ap[j] += (1 - ap[j]) * p[j][mp];
nv[j] /= ap[j];
} else {
ap[j] = p[j][mp];
nv[j] = (p[j][mp] * dp[mp] + 1) / ap[j];
}
}
}
}
}
cout << setprecision(13) << fixed << dp[1] << endl;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const long double DINF = pow(2, 100);
const int MAXN = 1007;
const long double EPS = 1e-9;
int n;
long double p[MAXN][MAXN];
long double dp[MAXN];
long double nv[MAXN];
long double ap[MAXN];
bool vis[MAXN];
int ord[MAXN];
void init();
void input();
void work();
int main() {
init();
input();
work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
scanf("%d", &n);
int x;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
scanf("%d", &x), p[i][j] = static_cast<long double>(x) / 100;
}
void work() {
dp[n] = 0;
ord[1] = n;
for (int i = 1; i < n; ++i)
nv[i] = (p[i][n] > EPS) ? (1 / p[i][n]) : DINF, ap[i] = p[i][n];
nv[n] = DINF;
vis[n] = true;
int mp;
for (int i = 2; i <= n; ++i) {
mp = min_element(nv + 1, nv + 1 + n) - nv;
dp[mp] = nv[mp];
if (mp == 1) break;
ord[i] = mp;
nv[mp] = DINF;
vis[mp] = true;
for (int j = 1; j <= n; ++j) {
if (!vis[j]) {
if (p[j][mp] > EPS) {
if (ap[j] > EPS) {
nv[j] *= ap[j];
nv[j] += (1 - ap[j]) * p[j][mp] * dp[mp];
ap[j] += (1 - ap[j]) * p[j][mp];
nv[j] /= ap[j];
} else {
ap[j] = p[j][mp];
nv[j] = (p[j][mp] * dp[mp] + 1) / ap[j];
}
}
}
}
}
cout << setprecision(13) << fixed << dp[1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
int n, vis[1005];
double E[1005], a[1005][1005], prod[1005];
int main() {
n = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x = read();
a[i][j] = 1.0 * x / 100;
}
}
if (n == 1) return puts("0"), 0;
for (int i = 1; i < n; i++) {
E[i] = 1;
prod[i] = 1 - a[i][n];
}
vis[n] = 1;
for (int i = 1; i <= n; i++) {
double minn = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++) {
if (E[j] / (1 - prod[j]) < minn && !vis[j]) {
minn = E[j] / (1 - prod[j]);
pos = j;
}
}
vis[pos] = 1;
if (pos == 1) return printf("%.12lf\n", E[pos] / (1 - prod[pos])), 0;
for (int j = 1; j <= n; j++) {
E[j] += E[pos] / (1 - prod[pos]) * a[j][pos] * prod[j];
prod[j] *= (1 - a[j][pos]);
}
}
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
int n, vis[1005];
double E[1005], a[1005][1005], prod[1005];
int main() {
n = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x = read();
a[i][j] = 1.0 * x / 100;
}
}
if (n == 1) return puts("0"), 0;
for (int i = 1; i < n; i++) {
E[i] = 1;
prod[i] = 1 - a[i][n];
}
vis[n] = 1;
for (int i = 1; i <= n; i++) {
double minn = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++) {
if (E[j] / (1 - prod[j]) < minn && !vis[j]) {
minn = E[j] / (1 - prod[j]);
pos = j;
}
}
vis[pos] = 1;
if (pos == 1) return printf("%.12lf\n", E[pos] / (1 - prod[pos])), 0;
for (int j = 1; j <= n; j++) {
E[j] += E[pos] / (1 - prod[pos]) * a[j][pos] * prod[j];
prod[j] *= (1 - a[j][pos]);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
double p[maxn][maxn], dis[maxn], v[maxn];
int vis[maxn], n;
int main() {
memset(dis, 0, sizeof(dis));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
v[i] = 1;
}
v[n] = 0;
for (int i = 1; i <= n; i++) {
int u = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
if (v[j] < 1 &&
(!u || ((dis[u] + 1) / (1 - v[u]) > (dis[j] + 1) / (1 - v[j]))))
u = j;
}
}
vis[u] = 1;
if (u != n) dis[u] = (dis[u] + 1) / (1 - v[u]);
if (u == 1) break;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
dis[j] += v[j] * p[j][u] * dis[u];
v[j] *= (1 - p[j][u]);
}
}
}
printf("%.12lf\n", dis[1]);
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
double p[maxn][maxn], dis[maxn], v[maxn];
int vis[maxn], n;
int main() {
memset(dis, 0, sizeof(dis));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
v[i] = 1;
}
v[n] = 0;
for (int i = 1; i <= n; i++) {
int u = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
if (v[j] < 1 &&
(!u || ((dis[u] + 1) / (1 - v[u]) > (dis[j] + 1) / (1 - v[j]))))
u = j;
}
}
vis[u] = 1;
if (u != n) dis[u] = (dis[u] + 1) / (1 - v[u]);
if (u == 1) break;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
dis[j] += v[j] * p[j][u] * dis[u];
v[j] *= (1 - p[j][u]);
}
}
}
printf("%.12lf\n", dis[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, p[1010][1010], vis[1010];
double A[1010], B[1010], ex[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &p[i][j]);
for (int i = 1; i <= n - 1; i++) ex[i] = 1e9, A[i] = B[i] = 1;
ex[n] = 0;
for (int x = 1; x <= n; x++) {
double mn = 1e60;
int u;
for (int i = 1; i <= n; i++)
if (!vis[i] && mn > ex[i]) mn = ex[i], u = i;
vis[u] = 1;
for (int v = 1; v <= n; v++)
if (!vis[v]) {
B[v] += p[v][u] / 100. * A[v] * ex[u];
A[v] = A[v] * (1 - p[v][u] / 100.);
ex[v] = B[v] / (1 - A[v]);
}
}
printf("%.15lf\n", ex[1]);
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, p[1010][1010], vis[1010];
double A[1010], B[1010], ex[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &p[i][j]);
for (int i = 1; i <= n - 1; i++) ex[i] = 1e9, A[i] = B[i] = 1;
ex[n] = 0;
for (int x = 1; x <= n; x++) {
double mn = 1e60;
int u;
for (int i = 1; i <= n; i++)
if (!vis[i] && mn > ex[i]) mn = ex[i], u = i;
vis[u] = 1;
for (int v = 1; v <= n; v++)
if (!vis[v]) {
B[v] += p[v][u] / 100. * A[v] * ex[u];
A[v] = A[v] * (1 - p[v][u] / 100.);
ex[v] = B[v] / (1 - A[v]);
}
}
printf("%.15lf\n", ex[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const double eps = 1e-9;
const int maxn = 1010;
double d[maxn];
double p[maxn][maxn];
double sum[maxn];
double prod[maxn];
int used[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; (i) < (n); ++i)
for (int j = 0; (j) < (n); ++j) {
int x;
scanf("%d", &x);
p[i][j] = x * 0.01;
}
for (int i = 0; (i) < (n - 1); ++i) d[i] = 1e100;
for (int i = 0; (i) < (n); ++i) sum[i] = 0, prod[i] = 1;
for (int _ = 0; (_) < (n); ++_) {
int x = -1;
for (int i = 0; (i) < (n); ++i)
if (used[i] == 0 && (x == -1 || d[x] > d[i])) x = i;
used[x] = 1;
for (int i = 0; (i) < (n); ++i)
if (!used[i]) {
sum[i] += prod[i] * p[i][x] * d[x];
prod[i] *= (1 - p[i][x]);
if (prod[i] < 1 - eps) d[i] = (1 + sum[i]) / (1 - prod[i]);
}
}
printf("%.10f\n", d[0]);
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const double eps = 1e-9;
const int maxn = 1010;
double d[maxn];
double p[maxn][maxn];
double sum[maxn];
double prod[maxn];
int used[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; (i) < (n); ++i)
for (int j = 0; (j) < (n); ++j) {
int x;
scanf("%d", &x);
p[i][j] = x * 0.01;
}
for (int i = 0; (i) < (n - 1); ++i) d[i] = 1e100;
for (int i = 0; (i) < (n); ++i) sum[i] = 0, prod[i] = 1;
for (int _ = 0; (_) < (n); ++_) {
int x = -1;
for (int i = 0; (i) < (n); ++i)
if (used[i] == 0 && (x == -1 || d[x] > d[i])) x = i;
used[x] = 1;
for (int i = 0; (i) < (n); ++i)
if (!used[i]) {
sum[i] += prod[i] * p[i][x] * d[x];
prod[i] *= (1 - p[i][x]);
if (prod[i] < 1 - eps) d[i] = (1 + sum[i]) / (1 - prod[i]);
}
}
printf("%.10f\n", d[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "inline")
using namespace std;
template <class T>
inline void read(T &x) {
int ch = 0, f = 0;
x = 0;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = 1;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if (f) x = -x;
}
const int N = 1005;
double f[N], w[N], p[N][N];
int vis[N], n;
inline double calc(int x) { return (f[x] + 1.0) / (1.0 - w[x]); }
int main() {
read(n);
if (n == 1) return puts("0"), 0;
for (int i = 1, x; i <= n; i++)
for (int j = 1; j <= n; j++) read(x), p[i][j] = 1.0 * x / 100.0;
vis[n] = 1;
for (int i = 1; i < n; i++) w[i] = 1 - p[i][n];
for (int i = 1; i < n; i++) {
double mn = 1.0 / 0.0;
int id = 0;
for (int j = 1; j < n; j++)
if (!vis[j] && calc(j) < mn) mn = calc(j), id = j;
if (id == 1) return printf("%.10lf", calc(1)), 0;
vis[id] = 1;
for (int j = 1; j < n; j++)
if (!vis[j]) {
f[j] += p[j][id] * w[j] * calc(id);
w[j] = w[j] * (1 - p[j][id]);
}
}
return 0;
}
|
### Prompt
In CPP, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "inline")
using namespace std;
template <class T>
inline void read(T &x) {
int ch = 0, f = 0;
x = 0;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = 1;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if (f) x = -x;
}
const int N = 1005;
double f[N], w[N], p[N][N];
int vis[N], n;
inline double calc(int x) { return (f[x] + 1.0) / (1.0 - w[x]); }
int main() {
read(n);
if (n == 1) return puts("0"), 0;
for (int i = 1, x; i <= n; i++)
for (int j = 1; j <= n; j++) read(x), p[i][j] = 1.0 * x / 100.0;
vis[n] = 1;
for (int i = 1; i < n; i++) w[i] = 1 - p[i][n];
for (int i = 1; i < n; i++) {
double mn = 1.0 / 0.0;
int id = 0;
for (int j = 1; j < n; j++)
if (!vis[j] && calc(j) < mn) mn = calc(j), id = j;
if (id == 1) return printf("%.10lf", calc(1)), 0;
vis[id] = 1;
for (int j = 1; j < n; j++)
if (!vis[j]) {
f[j] += p[j][id] * w[j] * calc(id);
w[j] = w[j] * (1 - p[j][id]);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
double P[1001][1001], f[1001], p[1001];
int N, vi[1001];
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) {
scanf("%lf", P[i] + j);
P[i][j] /= 100;
}
for (int i = 1; i < N; i++) {
f[i] = 1;
p[i] = 1 - P[i][N];
}
f[N] = 0;
vi[N] = 1;
while (!vi[1]) {
double W = 1e60;
int u;
for (int j = 1; j <= N; j++)
if (!vi[j] && f[j] / (1 - p[j]) < W) {
W = f[j] / (1 - p[j]);
u = j;
}
f[u] = W;
vi[u] = 1;
for (int j = 1; j <= N; j++)
if (!vi[j]) {
f[j] += f[u] * p[j] * P[j][u];
p[j] *= 1 - P[j][u];
}
}
printf("%.15lf\n", f[1]);
return 0;
}
|
### Prompt
Please formulate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
double P[1001][1001], f[1001], p[1001];
int N, vi[1001];
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) {
scanf("%lf", P[i] + j);
P[i][j] /= 100;
}
for (int i = 1; i < N; i++) {
f[i] = 1;
p[i] = 1 - P[i][N];
}
f[N] = 0;
vi[N] = 1;
while (!vi[1]) {
double W = 1e60;
int u;
for (int j = 1; j <= N; j++)
if (!vi[j] && f[j] / (1 - p[j]) < W) {
W = f[j] / (1 - p[j]);
u = j;
}
f[u] = W;
vi[u] = 1;
for (int j = 1; j <= N; j++)
if (!vi[j]) {
f[j] += f[u] * p[j] * P[j][u];
p[j] *= 1 - P[j][u];
}
}
printf("%.15lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n;
bool vis[N];
double e[N], p[N][N], prod[N], mn;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= n; i++) e[i] = 1, prod[i] = 1 - p[i][n];
e[n] = 0, vis[n] = true;
for (int i = 1, t; i <= n; i++) {
t = 0;
mn = 1e18;
for (int j = 1; j <= n; j++)
if (!vis[j] && e[j] / (1 - prod[j]) < mn)
t = j, mn = e[j] / (1 - prod[j]);
vis[t] = true;
if (t == 1) break;
for (int j = 1; j <= n; j++)
e[j] += mn * p[j][t] * prod[j], prod[j] *= (1 - p[j][t]);
}
printf("%.10lf\n", mn);
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n;
bool vis[N];
double e[N], p[N][N], prod[N], mn;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= n; i++) e[i] = 1, prod[i] = 1 - p[i][n];
e[n] = 0, vis[n] = true;
for (int i = 1, t; i <= n; i++) {
t = 0;
mn = 1e18;
for (int j = 1; j <= n; j++)
if (!vis[j] && e[j] / (1 - prod[j]) < mn)
t = j, mn = e[j] / (1 - prod[j]);
vis[t] = true;
if (t == 1) break;
for (int j = 1; j <= n; j++)
e[j] += mn * p[j][t] * prod[j], prod[j] *= (1 - p[j][t]);
}
printf("%.10lf\n", mn);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int s[N][N];
bool w[N], us[N];
double p[N], f[N], g[N];
inline int gi() {
int x = 0, o = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) s[i][j] = gi();
w[n] = 1, f[0] = 1e100;
for (int t = 1; t <= n; t++) {
int x = 0;
for (int i = 1; i <= n; i++)
if (!us[i] && w[i] && f[i] < f[x]) x = i;
if (x == 1) break;
us[x] = 1;
for (int y = 1; y <= n; y++)
if (s[y][x] && !us[y]) {
if (!w[y]) w[y] = 1, p[y] = 1;
g[y] += p[y] * s[y][x] / 100 * (f[x] + 1), p[y] *= 1 - s[y][x] / 100.0;
f[y] = (g[y] + p[y]) / (1 - p[y]);
}
}
printf("%.10f", f[1]);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int s[N][N];
bool w[N], us[N];
double p[N], f[N], g[N];
inline int gi() {
int x = 0, o = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) s[i][j] = gi();
w[n] = 1, f[0] = 1e100;
for (int t = 1; t <= n; t++) {
int x = 0;
for (int i = 1; i <= n; i++)
if (!us[i] && w[i] && f[i] < f[x]) x = i;
if (x == 1) break;
us[x] = 1;
for (int y = 1; y <= n; y++)
if (s[y][x] && !us[y]) {
if (!w[y]) w[y] = 1, p[y] = 1;
g[y] += p[y] * s[y][x] / 100 * (f[x] + 1), p[y] *= 1 - s[y][x] / 100.0;
f[y] = (g[y] + p[y]) / (1 - p[y]);
}
}
printf("%.10f", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double inf = 1e20;
const double eps = 1e-9;
const int maxn = 1e3 + 3;
double p[maxn][maxn], f[maxn], g[maxn], P[maxn];
int n, vis[maxn];
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; i++)
for (register int j = 1; j <= n; j++)
scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (register int i = 1; i <= n; i++) g[i] = inf, f[i] = P[i] = 1;
f[n] = g[n] = 0;
for (register int i = 1; i <= n - 1; i++) {
double tmp = inf;
int bj = 0;
for (register int j = 1; j <= n; j++)
if (!vis[j] && g[j] < tmp) tmp = g[j], bj = j;
vis[bj] = 1;
for (register int j = 1; j <= n; j++) {
if (vis[j]) continue;
f[j] += tmp * P[j] * p[j][bj];
P[j] *= 1.0 - p[j][bj];
if (1.0 - P[j] < eps)
g[j] = inf;
else
g[j] = f[j] / (1.0 - P[j]);
}
}
printf("%.10lf\n", g[1]);
return 0;
}
|
### Prompt
Your task is to create a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double inf = 1e20;
const double eps = 1e-9;
const int maxn = 1e3 + 3;
double p[maxn][maxn], f[maxn], g[maxn], P[maxn];
int n, vis[maxn];
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; i++)
for (register int j = 1; j <= n; j++)
scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (register int i = 1; i <= n; i++) g[i] = inf, f[i] = P[i] = 1;
f[n] = g[n] = 0;
for (register int i = 1; i <= n - 1; i++) {
double tmp = inf;
int bj = 0;
for (register int j = 1; j <= n; j++)
if (!vis[j] && g[j] < tmp) tmp = g[j], bj = j;
vis[bj] = 1;
for (register int j = 1; j <= n; j++) {
if (vis[j]) continue;
f[j] += tmp * P[j] * p[j][bj];
P[j] *= 1.0 - p[j][bj];
if (1.0 - P[j] < eps)
g[j] = inf;
else
g[j] = f[j] / (1.0 - P[j]);
}
}
printf("%.10lf\n", g[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename Type>
inline bool chkmin(Type &x, const Type &y) {
return x > y ? (x = y, 1) : 0;
}
template <typename Type>
inline bool chkmax(Type &x, const Type &y) {
return x < y ? (x = y, 1) : 0;
}
template <typename Type>
inline Type read(Type &f) {
f = 0;
int x = 1;
char c = getchar();
while (!isdigit(c)) x = (c == '-' ? -1 : 1), c = getchar();
while (isdigit(c)) (f *= 10) += c & 15, c = getchar();
return f = x * f;
}
const int N = 1000 + 5;
int n, x, vis[N];
double p[N][N], f[N], g[N];
int main() {
read(n);
for (int i = (1), ___ = (n); i <= ___; ++i)
for (int j = (1), ___ = (n); j <= ___; ++j)
p[i][j] = (double)read(x) / 100.0;
for (int i = (1), ___ = (n - 1); i <= ___; ++i) g[i] = f[i] = 1;
for (int i = (1), ___ = (n); i <= ___; ++i) {
double mn = 1e9;
int pd = 0;
for (int j = (1), ___ = (n); j <= ___; ++j) {
if (!vis[j] && g[j] < 1 && f[j] / (1 - g[j]) < mn) {
mn = f[j] / (1 - g[j]), pd = j;
}
}
if (!pd) break;
vis[pd] = 1, f[pd] /= 1 - g[pd];
for (int j = (1), ___ = (n); j <= ___; ++j)
if (!vis[j]) {
f[j] += g[j] * f[pd] * p[j][pd];
g[j] *= 1 - p[j][pd];
}
}
printf("%.10lf\n", f[1]);
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename Type>
inline bool chkmin(Type &x, const Type &y) {
return x > y ? (x = y, 1) : 0;
}
template <typename Type>
inline bool chkmax(Type &x, const Type &y) {
return x < y ? (x = y, 1) : 0;
}
template <typename Type>
inline Type read(Type &f) {
f = 0;
int x = 1;
char c = getchar();
while (!isdigit(c)) x = (c == '-' ? -1 : 1), c = getchar();
while (isdigit(c)) (f *= 10) += c & 15, c = getchar();
return f = x * f;
}
const int N = 1000 + 5;
int n, x, vis[N];
double p[N][N], f[N], g[N];
int main() {
read(n);
for (int i = (1), ___ = (n); i <= ___; ++i)
for (int j = (1), ___ = (n); j <= ___; ++j)
p[i][j] = (double)read(x) / 100.0;
for (int i = (1), ___ = (n - 1); i <= ___; ++i) g[i] = f[i] = 1;
for (int i = (1), ___ = (n); i <= ___; ++i) {
double mn = 1e9;
int pd = 0;
for (int j = (1), ___ = (n); j <= ___; ++j) {
if (!vis[j] && g[j] < 1 && f[j] / (1 - g[j]) < mn) {
mn = f[j] / (1 - g[j]), pd = j;
}
}
if (!pd) break;
vis[pd] = 1, f[pd] /= 1 - g[pd];
for (int j = (1), ___ = (n); j <= ___; ++j)
if (!vis[j]) {
f[j] += g[j] * f[pd] * p[j][pd];
g[j] *= 1 - p[j][pd];
}
}
printf("%.10lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + c - '0';
x *= f;
}
int n;
double g[N][N];
double E[N], prod[N];
bool vis[N];
int main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
read(x);
g[i][j] = 1. * x / 100;
}
if (n == 1) return puts("0"), 0;
vis[n] = true;
for (int i = 1; i < n; i++) {
E[i] = 1;
prod[i] = 1 - g[i][n];
}
for (int i = 1; i <= n; i++) {
double low = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && E[j] / (1 - prod[j]) < low)
low = E[j] / (1 - prod[j]), pos = j;
if (pos == 1) return printf("%.10lf\n", E[1] / (1 - prod[1])), 0;
vis[pos] = true;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - prod[pos]) * g[j][pos] * prod[j],
prod[j] *= (1 - g[j][pos]);
}
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + c - '0';
x *= f;
}
int n;
double g[N][N];
double E[N], prod[N];
bool vis[N];
int main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
read(x);
g[i][j] = 1. * x / 100;
}
if (n == 1) return puts("0"), 0;
vis[n] = true;
for (int i = 1; i < n; i++) {
E[i] = 1;
prod[i] = 1 - g[i][n];
}
for (int i = 1; i <= n; i++) {
double low = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && E[j] / (1 - prod[j]) < low)
low = E[j] / (1 - prod[j]), pos = j;
if (pos == 1) return printf("%.10lf\n", E[1] / (1 - prod[1])), 0;
vis[pos] = true;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - prod[pos]) * g[j][pos] * prod[j],
prod[j] *= (1 - g[j][pos]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double p[1005][1005], f[1005], prod[1005];
bool vis[1005];
int main() {
int n, x;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &x), p[i][j] = x / 100.0;
for (int i = 1; i < n; i++) prod[i] = 1, f[i] = 1;
for (int i = 1; i <= n; i++) {
double mn = 1e18;
int now = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && f[j] / (1 - prod[j]) < mn)
mn = f[j] / (1 - prod[j]), now = j;
vis[now] = 1;
f[now] /= (1 - prod[now]);
if (now == 1) {
printf("%.10lf", f[1]);
return 0;
}
for (int j = 1; j <= n; j++)
if (!vis[j]) {
f[j] += p[j][now] * prod[j] * f[now];
prod[j] *= 1 - p[j][now];
}
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double p[1005][1005], f[1005], prod[1005];
bool vis[1005];
int main() {
int n, x;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &x), p[i][j] = x / 100.0;
for (int i = 1; i < n; i++) prod[i] = 1, f[i] = 1;
for (int i = 1; i <= n; i++) {
double mn = 1e18;
int now = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && f[j] / (1 - prod[j]) < mn)
mn = f[j] / (1 - prod[j]), now = j;
vis[now] = 1;
f[now] /= (1 - prod[now]);
if (now == 1) {
printf("%.10lf", f[1]);
return 0;
}
for (int j = 1; j <= n; j++)
if (!vis[j]) {
f[j] += p[j][now] * prod[j] * f[now];
prod[j] *= 1 - p[j][now];
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
void read(int &digit) {
digit = 0;
char c;
for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
;
bool type = false;
if (c == '-') type = true, c = getchar();
for (; c >= '0' && c <= '9'; digit = digit * 10 + c - '0', c = getchar())
;
if (type == true) digit = -digit;
}
long double f[1010];
long double g[1010];
double b[1010][1010];
int n;
int que[1010], r;
int pos[1010];
bool vis[1010];
void add(int t) {
r++;
que[r] = t;
pos[t] = r;
int now = r;
while (now > 1) {
if (f[que[now]] < f[que[now / 2]])
swap(que[now], que[now / 2]), swap(pos[que[now]], pos[que[now / 2]]);
else
break;
now /= 2;
}
}
void change(int t) {
int now = t;
while (now > 1) {
if (f[que[now]] < f[que[now / 2]])
swap(que[now], que[now / 2]), swap(pos[que[now]], pos[que[now / 2]]);
else
break;
now /= 2;
}
}
void del() {
pos[que[1]] = -1;
if (r == 1) return;
pos[que[r]] = 1;
que[1] = que[r];
que[r--] = 0;
int now = 1;
while (2 * now <= r) {
int son = now * 2;
if (son + 1 <= r)
if (f[que[son + 1]] < f[que[son]]) son = son + 1;
if (f[que[son]] < f[que[now]])
swap(que[now], que[son]), swap(pos[que[now]], pos[que[son]]), now = son;
else
break;
}
}
const double eps = 1e-9;
int main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
read(x);
b[i][j] = x / 100.0;
}
for (int i = 1; i < n; i++) f[i] = 1e9, g[i] = 1;
for (int i = 1; i <= n; i++) add(i);
for (int i = 1; i <= n; i++) {
int u = que[1];
vis[u] = true;
del();
for (int j = 1; j <= n; j++)
if (!vis[j]) {
long double A = f[j] * (1.0 - g[j]);
if (g[j] == 1) A = 1;
long double B = g[j] * b[j][u] * f[u];
g[j] = g[j] * (1.0 - b[j][u]);
if (fabs(1 - g[j]) < eps)
f[j] = 1e9;
else
f[j] = (A + B) / (1.0 - g[j]);
change(pos[j]);
}
}
printf("%.8lf\n", (double)f[1]);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
void read(int &digit) {
digit = 0;
char c;
for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
;
bool type = false;
if (c == '-') type = true, c = getchar();
for (; c >= '0' && c <= '9'; digit = digit * 10 + c - '0', c = getchar())
;
if (type == true) digit = -digit;
}
long double f[1010];
long double g[1010];
double b[1010][1010];
int n;
int que[1010], r;
int pos[1010];
bool vis[1010];
void add(int t) {
r++;
que[r] = t;
pos[t] = r;
int now = r;
while (now > 1) {
if (f[que[now]] < f[que[now / 2]])
swap(que[now], que[now / 2]), swap(pos[que[now]], pos[que[now / 2]]);
else
break;
now /= 2;
}
}
void change(int t) {
int now = t;
while (now > 1) {
if (f[que[now]] < f[que[now / 2]])
swap(que[now], que[now / 2]), swap(pos[que[now]], pos[que[now / 2]]);
else
break;
now /= 2;
}
}
void del() {
pos[que[1]] = -1;
if (r == 1) return;
pos[que[r]] = 1;
que[1] = que[r];
que[r--] = 0;
int now = 1;
while (2 * now <= r) {
int son = now * 2;
if (son + 1 <= r)
if (f[que[son + 1]] < f[que[son]]) son = son + 1;
if (f[que[son]] < f[que[now]])
swap(que[now], que[son]), swap(pos[que[now]], pos[que[son]]), now = son;
else
break;
}
}
const double eps = 1e-9;
int main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
read(x);
b[i][j] = x / 100.0;
}
for (int i = 1; i < n; i++) f[i] = 1e9, g[i] = 1;
for (int i = 1; i <= n; i++) add(i);
for (int i = 1; i <= n; i++) {
int u = que[1];
vis[u] = true;
del();
for (int j = 1; j <= n; j++)
if (!vis[j]) {
long double A = f[j] * (1.0 - g[j]);
if (g[j] == 1) A = 1;
long double B = g[j] * b[j][u] * f[u];
g[j] = g[j] * (1.0 - b[j][u]);
if (fabs(1 - g[j]) < eps)
f[j] = 1e9;
else
f[j] = (A + B) / (1.0 - g[j]);
change(pos[j]);
}
}
printf("%.8lf\n", (double)f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
struct Reader {
istream& is;
template <typename T>
operator T() const {
T t;
is >> t;
return t;
}
};
const int N = 1000;
double p[N][N], prod[N], E[N];
bool v[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Reader r{cin};
int n = int(r) - 1;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j) p[i][j] = int(r) / 100.0;
E[n] = 0.0;
v[n] = true;
for (int i = 0; i < n; ++i) {
E[i] = 1.0;
prod[i] = 1.0 - p[i][n];
}
for (int i = 0; i <= n; ++i) {
int k = -1;
for (int j = 0; j <= n; ++j)
if (!v[j] && (!~k || E[j] / (1.0 - prod[j]) < E[k] / (1.0 - prod[k])))
k = j;
if (!~k) break;
v[k] = true;
for (int j = 0; j <= n; ++j)
if (E[k] / (1.0 - prod[k]) < E[j] / (1.0 - prod[j])) {
E[j] += E[k] / (1.0 - prod[k]) * prod[j] * p[j][k];
prod[j] *= 1.0 - p[j][k];
}
}
cout << fixed << setprecision(16) << E[0] / (1.0 - prod[0]);
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
struct Reader {
istream& is;
template <typename T>
operator T() const {
T t;
is >> t;
return t;
}
};
const int N = 1000;
double p[N][N], prod[N], E[N];
bool v[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Reader r{cin};
int n = int(r) - 1;
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j) p[i][j] = int(r) / 100.0;
E[n] = 0.0;
v[n] = true;
for (int i = 0; i < n; ++i) {
E[i] = 1.0;
prod[i] = 1.0 - p[i][n];
}
for (int i = 0; i <= n; ++i) {
int k = -1;
for (int j = 0; j <= n; ++j)
if (!v[j] && (!~k || E[j] / (1.0 - prod[j]) < E[k] / (1.0 - prod[k])))
k = j;
if (!~k) break;
v[k] = true;
for (int j = 0; j <= n; ++j)
if (E[k] / (1.0 - prod[k]) < E[j] / (1.0 - prod[j])) {
E[j] += E[k] / (1.0 - prod[k]) * prod[j] * p[j][k];
prod[j] *= 1.0 - p[j][k];
}
}
cout << fixed << setprecision(16) << E[0] / (1.0 - prod[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
double p[N][N], E[N], g[N], f[N];
bool vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (int i = 1; i <= n; i++) g[i] = 1 - p[i][n];
vis[n] = 1;
for (int i = 1; i <= n; i++) {
double mn = 1e18;
int id = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && (f[j] + 1) / (1 - g[j]) < mn) {
mn = (f[j] + 1) / (1 - g[j]);
id = j;
}
vis[id] = 1;
E[id] = (f[id] + 1) / (1 - g[id]);
for (int j = 1; j <= n; j++)
if (!vis[j])
f[j] += g[j] * p[j][id] * E[id], g[j] = g[j] * (1 - p[j][id]);
}
printf("%.6lf\n", E[1]);
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n;
double p[N][N], E[N], g[N], f[N];
bool vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (int i = 1; i <= n; i++) g[i] = 1 - p[i][n];
vis[n] = 1;
for (int i = 1; i <= n; i++) {
double mn = 1e18;
int id = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && (f[j] + 1) / (1 - g[j]) < mn) {
mn = (f[j] + 1) / (1 - g[j]);
id = j;
}
vis[id] = 1;
E[id] = (f[id] + 1) / (1 - g[id]);
for (int j = 1; j <= n; j++)
if (!vis[j])
f[j] += g[j] * p[j][id] * E[id], g[j] = g[j] * (1 - p[j][id]);
}
printf("%.6lf\n", E[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 1;
const long long INF = ~0ULL >> 1;
template <class T>
inline void read(T &n) {
char c;
int flag = 1;
for (c = getchar(); !(c >= '0' && c <= '9' || c == '-'); c = getchar())
;
if (c == '-')
flag = -1, n = 0;
else
n = c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0';
n *= flag;
}
int N, id[1200], vis[1200];
int p[1200][1200];
double dp[2000], disc[2000], tmp[2000];
bool cmp(const int &a, const int &b) { return p[a][N] > p[b][N]; }
int main() {
scanf("%d", &N);
for (int i = (1); i <= (N); ++i)
for (int j = (1); j <= (N); ++j) scanf("%d", &p[i][j]);
for (int i = (1); i <= (N); ++i) {
dp[i] = 1. / (p[i][N] / 100. + 1e-20);
disc[i] = 1;
}
dp[N] = 0;
for (int i = (1); i <= (N); ++i) {
int u = -1;
for (int j = (1); j <= (N); ++j)
if (!vis[j])
if (u == -1 || dp[j] < dp[u]) u = j;
vis[u] = true;
id[i] = u;
for (int j = (1); j <= (N); ++j)
if (!vis[j]) {
tmp[j] += (dp[u] + 1) * disc[j] * p[j][u] / 100.;
disc[j] *= 1. - p[j][u] / 100.;
dp[j] = (tmp[j] + disc[j]) / (1. - disc[j]);
}
}
printf("%.10lf\n", dp[1]);
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 1;
const long long INF = ~0ULL >> 1;
template <class T>
inline void read(T &n) {
char c;
int flag = 1;
for (c = getchar(); !(c >= '0' && c <= '9' || c == '-'); c = getchar())
;
if (c == '-')
flag = -1, n = 0;
else
n = c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0';
n *= flag;
}
int N, id[1200], vis[1200];
int p[1200][1200];
double dp[2000], disc[2000], tmp[2000];
bool cmp(const int &a, const int &b) { return p[a][N] > p[b][N]; }
int main() {
scanf("%d", &N);
for (int i = (1); i <= (N); ++i)
for (int j = (1); j <= (N); ++j) scanf("%d", &p[i][j]);
for (int i = (1); i <= (N); ++i) {
dp[i] = 1. / (p[i][N] / 100. + 1e-20);
disc[i] = 1;
}
dp[N] = 0;
for (int i = (1); i <= (N); ++i) {
int u = -1;
for (int j = (1); j <= (N); ++j)
if (!vis[j])
if (u == -1 || dp[j] < dp[u]) u = j;
vis[u] = true;
id[i] = u;
for (int j = (1); j <= (N); ++j)
if (!vis[j]) {
tmp[j] += (dp[u] + 1) * disc[j] * p[j][u] / 100.;
disc[j] *= 1. - p[j][u] / 100.;
dp[j] = (tmp[j] + disc[j]) / (1. - disc[j]);
}
}
printf("%.10lf\n", dp[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int N;
bool used[maxn];
double A[maxn], B[maxn], P[maxn][maxn], ans[maxn];
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
heap;
void SP() {
heap.push(pair<double, int>(0, N));
while (!heap.empty()) {
pair<double, int> x = heap.top();
heap.pop();
if (used[x.second]) continue;
int &node = x.second;
ans[node] = x.first;
used[node] = 1;
for (int i = 1; i <= N; i++)
if (!used[i] && P[i][node]) {
B[i] += A[i] * P[i][node] * (ans[node] + 1);
A[i] *= 1 - P[i][node];
heap.push(pair<double, int>((A[i] + B[i]) / (1 - A[i]), i));
}
}
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) A[i] = 1;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) scanf("%lf", &P[i][j]), P[i][j] /= 100;
memset(ans, -1, sizeof ans);
SP();
printf("%lf\n", ans[1]);
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int N;
bool used[maxn];
double A[maxn], B[maxn], P[maxn][maxn], ans[maxn];
priority_queue<pair<double, int>, vector<pair<double, int> >,
greater<pair<double, int> > >
heap;
void SP() {
heap.push(pair<double, int>(0, N));
while (!heap.empty()) {
pair<double, int> x = heap.top();
heap.pop();
if (used[x.second]) continue;
int &node = x.second;
ans[node] = x.first;
used[node] = 1;
for (int i = 1; i <= N; i++)
if (!used[i] && P[i][node]) {
B[i] += A[i] * P[i][node] * (ans[node] + 1);
A[i] *= 1 - P[i][node];
heap.push(pair<double, int>((A[i] + B[i]) / (1 - A[i]), i));
}
}
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) A[i] = 1;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) scanf("%lf", &P[i][j]), P[i][j] /= 100;
memset(ans, -1, sizeof ans);
SP();
printf("%lf\n", ans[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
double tab[1007][1007];
int jusz[1007];
double tak[1007];
double wyn[1007];
double tam;
double tu;
double nie;
int v;
int zero(double v) { return abs(v) < 0.000000001; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &tab[i][j]);
tab[i][j] /= 100.0;
}
}
jusz[n] = 1;
for (int i = 1; i < n; i++) {
tak[i] = tab[i][n];
wyn[i] = tak[i];
}
for (int h = 1; h < n; h++) {
tam = 1000000000.0;
v = -1;
for (int i = 1; i <= n; i++) {
if (jusz[i] || zero(tak[i])) continue;
nie = 1.0 - tak[i];
if (zero(nie)) {
tu = wyn[i];
} else {
tu = wyn[i] / tak[i] + (nie / (1.0 - nie));
}
if (tu < tam) {
tam = tu;
v = i;
}
}
if (v == -1) break;
jusz[v] = 1;
wyn[v] = tam;
for (int i = 1; i <= n; i++) {
if (jusz[i] || zero(tab[i][v])) continue;
nie = 1.0 - tak[i];
wyn[i] += nie * tab[i][v] * (wyn[v] + 1.0);
nie *= (1.0 - tab[i][v]);
tak[i] = 1.0 - nie;
}
}
printf("%.9lf\n", wyn[1]);
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
double tab[1007][1007];
int jusz[1007];
double tak[1007];
double wyn[1007];
double tam;
double tu;
double nie;
int v;
int zero(double v) { return abs(v) < 0.000000001; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &tab[i][j]);
tab[i][j] /= 100.0;
}
}
jusz[n] = 1;
for (int i = 1; i < n; i++) {
tak[i] = tab[i][n];
wyn[i] = tak[i];
}
for (int h = 1; h < n; h++) {
tam = 1000000000.0;
v = -1;
for (int i = 1; i <= n; i++) {
if (jusz[i] || zero(tak[i])) continue;
nie = 1.0 - tak[i];
if (zero(nie)) {
tu = wyn[i];
} else {
tu = wyn[i] / tak[i] + (nie / (1.0 - nie));
}
if (tu < tam) {
tam = tu;
v = i;
}
}
if (v == -1) break;
jusz[v] = 1;
wyn[v] = tam;
for (int i = 1; i <= n; i++) {
if (jusz[i] || zero(tab[i][v])) continue;
nie = 1.0 - tak[i];
wyn[i] += nie * tab[i][v] * (wyn[v] + 1.0);
nie *= (1.0 - tab[i][v]);
tak[i] = 1.0 - nie;
}
}
printf("%.9lf\n", wyn[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1005;
double p[Maxn][Maxn], e[Maxn], f[Maxn];
int vis[Maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100;
for (int i = 1; i < n; i++) f[i] = 1, e[i] = 1;
for (int i = 1; i <= n; i++) {
double val = 1e60;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && e[j] / (1 - f[j]) < val) {
val = e[j] / (1 - f[j]);
pos = j;
}
e[pos] = val;
vis[pos] = true;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
e[i] += e[pos] * p[i][pos] * f[i];
f[i] *= (1 - p[i][pos]);
}
}
cout << fixed << setprecision(7) << e[1] << "\n";
return 0;
}
|
### Prompt
In cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1005;
double p[Maxn][Maxn], e[Maxn], f[Maxn];
int vis[Maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100;
for (int i = 1; i < n; i++) f[i] = 1, e[i] = 1;
for (int i = 1; i <= n; i++) {
double val = 1e60;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && e[j] / (1 - f[j]) < val) {
val = e[j] / (1 - f[j]);
pos = j;
}
e[pos] = val;
vis[pos] = true;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
e[i] += e[pos] * p[i][pos] * f[i];
f[i] *= (1 - p[i][pos]);
}
}
cout << fixed << setprecision(7) << e[1] << "\n";
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
double dist[1050], pre[1050];
bool vis[1050];
double P[1050][1050];
int main() {
scanf("%d", &n);
if (n == 1) {
printf("0.0\n");
return 0;
}
for (int i = 1; i <= n; ++i) {
pre[i] = dist[i] = 1;
for (int j = 1; j <= n; ++j) {
int Po;
scanf("%d", &Po);
P[i][j] = Po / 100.0;
}
}
dist[n] = 0;
int pos = n;
vis[n] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
dist[j] += pre[j] * (dist[pos]) * P[j][pos];
pre[j] = pre[j] * (1 - P[j][pos]);
}
double minn = 1e100;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dist[j] / (1 - pre[j]) < minn)
minn = dist[j] / (1 - pre[j]), pos = j;
dist[pos] = dist[pos] / (1 - pre[pos]);
vis[pos] = 1;
if (pos == 1) {
printf("%.10lf\n", dist[1]);
return 0;
}
}
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
double dist[1050], pre[1050];
bool vis[1050];
double P[1050][1050];
int main() {
scanf("%d", &n);
if (n == 1) {
printf("0.0\n");
return 0;
}
for (int i = 1; i <= n; ++i) {
pre[i] = dist[i] = 1;
for (int j = 1; j <= n; ++j) {
int Po;
scanf("%d", &Po);
P[i][j] = Po / 100.0;
}
}
dist[n] = 0;
int pos = n;
vis[n] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
dist[j] += pre[j] * (dist[pos]) * P[j][pos];
pre[j] = pre[j] * (1 - P[j][pos]);
}
double minn = 1e100;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dist[j] / (1 - pre[j]) < minn)
minn = dist[j] / (1 - pre[j]), pos = j;
dist[pos] = dist[pos] / (1 - pre[pos]);
vis[pos] = 1;
if (pos == 1) {
printf("%.10lf\n", dist[1]);
return 0;
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, a[N][N], p;
double mini, f[N], g1[N], g2[N], sum[N];
bool o[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
f[i] = 1.0 / a[i][n];
g1[i] = 1;
}
f[n] = 0;
for (int i = 1; i <= n; i++) {
mini = 100000000;
for (int j = 1; j <= n; j++)
if (!o[j] && f[j] < mini) mini = f[j], p = j;
o[p] = 1;
for (int j = 1; j < n; j++)
if (!o[j]) {
sum[j] += g1[j] * a[j][p] / 100.0 * (f[p] + 1);
g1[j] *= 1 - a[j][p] / 100.0;
f[j] = (sum[j] + g1[j]) / (1 - g1[j]);
}
}
printf("%.7f", f[1]);
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, a[N][N], p;
double mini, f[N], g1[N], g2[N], sum[N];
bool o[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
f[i] = 1.0 / a[i][n];
g1[i] = 1;
}
f[n] = 0;
for (int i = 1; i <= n; i++) {
mini = 100000000;
for (int j = 1; j <= n; j++)
if (!o[j] && f[j] < mini) mini = f[j], p = j;
o[p] = 1;
for (int j = 1; j < n; j++)
if (!o[j]) {
sum[j] += g1[j] * a[j][p] / 100.0 * (f[p] + 1);
g1[j] *= 1 - a[j][p] / 100.0;
f[j] = (sum[j] + g1[j]) / (1 - g1[j]);
}
}
printf("%.7f", f[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 1010;
const double INF = 1e100;
double p[MAXN + 1][MAXN + 1], P[MAXN + 1], E[MAXN + 1];
int N;
bool vis[MAXN + 1];
int main() {
N = read();
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) p[i][j] = read(), p[i][j] /= (double)100;
if (N == 1) {
printf("0\n");
return 0;
}
for (int i = 1; i <= N; i++) E[i] = 1, P[i] = 1 - p[i][N];
E[N] = 0, vis[N] = 1;
for (int i = 1; i <= N; i++) {
int pos = 0;
double Mn = INF;
for (int j = 1; j <= N; j++) {
if (vis[j]) continue;
double tmp = E[j] / (1 - P[j]);
if (tmp < Mn) Mn = tmp, pos = j;
}
vis[pos] = 1;
if (pos == 1) {
printf("%.6lf\n", Mn);
return 0;
}
for (int j = 1; j <= N; j++) {
E[j] += Mn * p[j][pos] * P[j];
P[j] *= (double)(1 - p[j][pos]);
}
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 1010;
const double INF = 1e100;
double p[MAXN + 1][MAXN + 1], P[MAXN + 1], E[MAXN + 1];
int N;
bool vis[MAXN + 1];
int main() {
N = read();
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) p[i][j] = read(), p[i][j] /= (double)100;
if (N == 1) {
printf("0\n");
return 0;
}
for (int i = 1; i <= N; i++) E[i] = 1, P[i] = 1 - p[i][N];
E[N] = 0, vis[N] = 1;
for (int i = 1; i <= N; i++) {
int pos = 0;
double Mn = INF;
for (int j = 1; j <= N; j++) {
if (vis[j]) continue;
double tmp = E[j] / (1 - P[j]);
if (tmp < Mn) Mn = tmp, pos = j;
}
vis[pos] = 1;
if (pos == 1) {
printf("%.6lf\n", Mn);
return 0;
}
for (int j = 1; j <= N; j++) {
E[j] += Mn * p[j][pos] * P[j];
P[j] *= (double)(1 - p[j][pos]);
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 1;
inline long long input(void) {
char t;
long long x = 0;
int neg = 0;
t = getchar();
while ((t < 48 || t > 57) && t != '-') t = getchar();
if (t == '-') {
neg = 1;
t = getchar();
}
while (t >= 48 && t <= 57) {
x = (x << 3) + (x << 1) + t - 48;
t = getchar();
}
if (neg) x = -x;
return x;
}
inline void output(long long x) {
char a[20];
int i = 0, j;
a[0] = '0';
if (x < 0) {
putchar('-');
x = -x;
}
if (x == 0) putchar('0');
while (x) {
a[i++] = x % 10 + 48;
x /= 10;
}
for (j = i - 1; j >= 0; j--) {
putchar(a[j]);
}
putchar('\n');
}
double dis[1010], p[1010][1010], prob[1010], num[1010];
set<pair<double, long long> > s;
int used[1010];
int main() {
long long n, x;
n = input();
memset(used, 0, sizeof(used));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) p[i][j] = 0.01 * input();
for (int i = 0; i < n; ++i) {
dis[i] = INF;
num[i] = 0;
prob[i] = 1.0;
}
dis[n - 1] = 0;
for (int i = 0; i < n; ++i) s.insert(make_pair(dis[i], i));
while (!s.empty()) {
x = s.begin()->second;
s.erase(s.begin());
used[x] = 1;
for (int i = 0; i < n; ++i) {
if (used[i]) continue;
s.erase(make_pair(dis[i], i));
num[i] += dis[x] * p[i][x] * prob[i];
prob[i] *= (1 - p[i][x]);
dis[i] = (1 + num[i]) / (1 - prob[i]);
s.insert(make_pair(dis[i], i));
}
}
printf("%lf\n", dis[0]);
return 0;
}
|
### Prompt
Generate a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 1;
inline long long input(void) {
char t;
long long x = 0;
int neg = 0;
t = getchar();
while ((t < 48 || t > 57) && t != '-') t = getchar();
if (t == '-') {
neg = 1;
t = getchar();
}
while (t >= 48 && t <= 57) {
x = (x << 3) + (x << 1) + t - 48;
t = getchar();
}
if (neg) x = -x;
return x;
}
inline void output(long long x) {
char a[20];
int i = 0, j;
a[0] = '0';
if (x < 0) {
putchar('-');
x = -x;
}
if (x == 0) putchar('0');
while (x) {
a[i++] = x % 10 + 48;
x /= 10;
}
for (j = i - 1; j >= 0; j--) {
putchar(a[j]);
}
putchar('\n');
}
double dis[1010], p[1010][1010], prob[1010], num[1010];
set<pair<double, long long> > s;
int used[1010];
int main() {
long long n, x;
n = input();
memset(used, 0, sizeof(used));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) p[i][j] = 0.01 * input();
for (int i = 0; i < n; ++i) {
dis[i] = INF;
num[i] = 0;
prob[i] = 1.0;
}
dis[n - 1] = 0;
for (int i = 0; i < n; ++i) s.insert(make_pair(dis[i], i));
while (!s.empty()) {
x = s.begin()->second;
s.erase(s.begin());
used[x] = 1;
for (int i = 0; i < n; ++i) {
if (used[i]) continue;
s.erase(make_pair(dis[i], i));
num[i] += dis[x] * p[i][x] * prob[i];
prob[i] *= (1 - p[i][x]);
dis[i] = (1 + num[i]) / (1 - prob[i]);
s.insert(make_pair(dis[i], i));
}
}
printf("%lf\n", dis[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
int n, i, j, k, a[1010][1010];
double cd, co, d[1010], o[1010], p[1010][1010];
bool was[1010];
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
p[i][j] = a[i][j] / 100.;
}
for (i = 1; i < n; i++) d[i] = 1e20;
while (true) {
for (k = -1, i = 1; i <= n; i++)
if (!was[i] && (k == -1 || d[i] < d[k])) k = i;
was[k] = true;
if (k == 1) break;
for (i = 1; i <= n; i++)
if (!was[i] && a[i][k] > 0) {
if (d[i] > 1e19) {
o[i] = 1 - p[i][k];
d[i] = (1 + p[i][k] * d[k]) / (1 - o[i]);
} else {
cd = d[i] * (1 - o[i]) + o[i] * p[i][k] * d[k];
co = o[i] * (1 - p[i][k]);
if (cd / (1 - co) < d[i]) {
d[i] = cd / (1 - co);
o[i] = co;
}
}
}
}
printf("%.14lf\n", d[1]);
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
int n, i, j, k, a[1010][1010];
double cd, co, d[1010], o[1010], p[1010][1010];
bool was[1010];
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
p[i][j] = a[i][j] / 100.;
}
for (i = 1; i < n; i++) d[i] = 1e20;
while (true) {
for (k = -1, i = 1; i <= n; i++)
if (!was[i] && (k == -1 || d[i] < d[k])) k = i;
was[k] = true;
if (k == 1) break;
for (i = 1; i <= n; i++)
if (!was[i] && a[i][k] > 0) {
if (d[i] > 1e19) {
o[i] = 1 - p[i][k];
d[i] = (1 + p[i][k] * d[k]) / (1 - o[i]);
} else {
cd = d[i] * (1 - o[i]) + o[i] * p[i][k] * d[k];
co = o[i] * (1 - p[i][k]);
if (cd / (1 - co) < d[i]) {
d[i] = cd / (1 - co);
o[i] = co;
}
}
}
}
printf("%.14lf\n", d[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
const int INF = (int)1e9 + 100;
const long long lINF = 1e18;
const double EPS = 1e-12;
using namespace std;
const int maxn = 1100;
int n;
long double a[maxn][maxn];
long double curexp[maxn];
long double lft[maxn];
int used[maxn];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int c;
scanf("%d", &c);
a[i][j] = (long double)c / 100;
}
}
for (int i = 0; i < n - 1; i++) {
lft[i] = 1;
curexp[i] = 1;
}
{
used[n - 1] = 1;
for (int i = 0; i < n - 1; i++) {
lft[i] *= (1 - a[i][n - 1]);
}
}
for (int i = 0; i < n - 1; i++) {
int mxi = -1;
for (int j = 0; j < n; j++) {
if (used[j] == 1) {
continue;
}
if (lft[j] > 1 - EPS) {
continue;
}
if (mxi == -1 ||
(curexp[j] / (1 - lft[j])) < (curexp[mxi] / (1 - lft[mxi]))) {
mxi = j;
}
}
used[mxi] = 1;
curexp[mxi] = curexp[mxi] / (1 - lft[mxi]);
for (int j = 0; j < n; j++) {
if (used[j] == 1) {
continue;
}
curexp[j] += lft[j] * a[j][mxi] * curexp[mxi];
lft[j] *= (1 - a[j][mxi]);
}
}
printf("%.18f\n", (double)curexp[0]);
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int INF = (int)1e9 + 100;
const long long lINF = 1e18;
const double EPS = 1e-12;
using namespace std;
const int maxn = 1100;
int n;
long double a[maxn][maxn];
long double curexp[maxn];
long double lft[maxn];
int used[maxn];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int c;
scanf("%d", &c);
a[i][j] = (long double)c / 100;
}
}
for (int i = 0; i < n - 1; i++) {
lft[i] = 1;
curexp[i] = 1;
}
{
used[n - 1] = 1;
for (int i = 0; i < n - 1; i++) {
lft[i] *= (1 - a[i][n - 1]);
}
}
for (int i = 0; i < n - 1; i++) {
int mxi = -1;
for (int j = 0; j < n; j++) {
if (used[j] == 1) {
continue;
}
if (lft[j] > 1 - EPS) {
continue;
}
if (mxi == -1 ||
(curexp[j] / (1 - lft[j])) < (curexp[mxi] / (1 - lft[mxi]))) {
mxi = j;
}
}
used[mxi] = 1;
curexp[mxi] = curexp[mxi] / (1 - lft[mxi]);
for (int j = 0; j < n; j++) {
if (used[j] == 1) {
continue;
}
curexp[j] += lft[j] * a[j][mxi] * curexp[mxi];
lft[j] *= (1 - a[j][mxi]);
}
}
printf("%.18f\n", (double)curexp[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int N = 1010;
int n;
double p[N][N];
double pi[N];
double e[N];
int vis[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100.0;
}
for (int i = 0; i < n; i++) e[i] = -1, pi[i] = 1;
e[n - 1] = 0;
for (int i = 0; i < n; i++) {
int k = -1;
for (int j = 0; j < n; j++)
if (!vis[j]) {
if (e[j] > -eps && ((e[j] < e[k] + eps) || k == -1)) k = j;
}
if (k == -1) break;
vis[k] = 1;
for (int j = 0; j < n; j++)
if (vis[j] == 0) {
if (e[j] < eps) {
if (p[j][k] > eps) {
e[j] = 1.0 / p[j][k] + e[k];
}
} else
e[j] = (pi[j] * p[j][k] * e[k] + (1 - pi[j]) * e[j]) /
(1 - pi[j] * (1 - p[j][k]));
pi[j] = pi[j] * (1 - p[j][k]);
}
}
printf("%.10f\n", e[0]);
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int N = 1010;
int n;
double p[N][N];
double pi[N];
double e[N];
int vis[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100.0;
}
for (int i = 0; i < n; i++) e[i] = -1, pi[i] = 1;
e[n - 1] = 0;
for (int i = 0; i < n; i++) {
int k = -1;
for (int j = 0; j < n; j++)
if (!vis[j]) {
if (e[j] > -eps && ((e[j] < e[k] + eps) || k == -1)) k = j;
}
if (k == -1) break;
vis[k] = 1;
for (int j = 0; j < n; j++)
if (vis[j] == 0) {
if (e[j] < eps) {
if (p[j][k] > eps) {
e[j] = 1.0 / p[j][k] + e[k];
}
} else
e[j] = (pi[j] * p[j][k] * e[k] + (1 - pi[j]) * e[j]) /
(1 - pi[j] * (1 - p[j][k]));
pi[j] = pi[j] * (1 - p[j][k]);
}
}
printf("%.10f\n", e[0]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
long double p[1005][1005];
long double E[1005];
long double prob[1005];
bool bio[1005];
int pp[1005][1005];
int main() {
cin >> n;
for (int i = (int)(0); i < (int)(n); ++i)
for (int j = (int)(0); j < (int)(n); ++j)
scanf("%d", pp[i] + j), p[i][j] = pp[i][j] / 100.0;
vector<int> V = {n - 1};
vector<int> V2;
for (int i = (int)(0); i < (int)(n - 1); ++i) V2.push_back(i);
bio[n - 1] = true;
for (int i = (int)(0); i < (int)(n); ++i) {
if (bio[i]) continue;
E[i] = 1;
prob[i] = 1;
prob[i] *= 1 - p[i][n - 1];
}
while (!V2.empty()) {
int id = -1;
long double cal = 1e9;
for (int i = (int)(0); i < (int)(V2.size()); ++i) {
if (prob[V2[i]] > 0.999) continue;
long double cc = E[V2[i]] / (1 - prob[V2[i]]);
if (cc < cal) cal = cc, id = i;
}
if (id == -1) break;
int first = V2[id];
V2.erase(V2.begin() + id);
bio[first] = true;
E[first] /= 1 - prob[first];
for (int i = (int)(0); i < (int)(n); ++i) {
if (bio[i]) continue;
if (pp[i][first] == 0) continue;
E[i] += prob[i] * p[i][first] * E[first];
prob[i] *= 1 - p[i][first];
}
V.push_back(first);
}
E[n - 1] = 0;
for (int first = (int)(1); first < (int)(V.size()); ++first) {
int i = V[first];
E[i] = 1;
prob[i] = 1;
for (int second = (int)(0); second < (int)(first); ++second) {
int j = V[second];
if (pp[i][j] == 0) continue;
E[i] += prob[i] * p[i][j] * E[j];
prob[i] *= 1 - p[i][j];
}
E[i] /= 1 - prob[i];
}
printf("%.15lf\n", (double)E[0]);
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
long double p[1005][1005];
long double E[1005];
long double prob[1005];
bool bio[1005];
int pp[1005][1005];
int main() {
cin >> n;
for (int i = (int)(0); i < (int)(n); ++i)
for (int j = (int)(0); j < (int)(n); ++j)
scanf("%d", pp[i] + j), p[i][j] = pp[i][j] / 100.0;
vector<int> V = {n - 1};
vector<int> V2;
for (int i = (int)(0); i < (int)(n - 1); ++i) V2.push_back(i);
bio[n - 1] = true;
for (int i = (int)(0); i < (int)(n); ++i) {
if (bio[i]) continue;
E[i] = 1;
prob[i] = 1;
prob[i] *= 1 - p[i][n - 1];
}
while (!V2.empty()) {
int id = -1;
long double cal = 1e9;
for (int i = (int)(0); i < (int)(V2.size()); ++i) {
if (prob[V2[i]] > 0.999) continue;
long double cc = E[V2[i]] / (1 - prob[V2[i]]);
if (cc < cal) cal = cc, id = i;
}
if (id == -1) break;
int first = V2[id];
V2.erase(V2.begin() + id);
bio[first] = true;
E[first] /= 1 - prob[first];
for (int i = (int)(0); i < (int)(n); ++i) {
if (bio[i]) continue;
if (pp[i][first] == 0) continue;
E[i] += prob[i] * p[i][first] * E[first];
prob[i] *= 1 - p[i][first];
}
V.push_back(first);
}
E[n - 1] = 0;
for (int first = (int)(1); first < (int)(V.size()); ++first) {
int i = V[first];
E[i] = 1;
prob[i] = 1;
for (int second = (int)(0); second < (int)(first); ++second) {
int j = V[second];
if (pp[i][j] == 0) continue;
E[i] += prob[i] * p[i][j] * E[j];
prob[i] *= 1 - p[i][j];
}
E[i] /= 1 - prob[i];
}
printf("%.15lf\n", (double)E[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, d[1005], cnt;
double p[1005][1005], f[1005], g[1005], h[1005];
bool vis[1005];
int main() {
cin >> n;
int x;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> x;
p[i][j] = (double)x / 100.0;
}
}
vis[n] = 1;
d[++cnt] = n;
for (int i = 1; i <= n; ++i) g[i] = 1, h[i] = 0;
for (int j = 1; j < n; ++j) {
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
h[i] += f[d[cnt]] * g[i] * p[i][d[cnt]];
g[i] *= (1 - p[i][d[cnt]]);
f[i] = (h[i] + 1) / (1 - g[i]);
}
double mxx = 1e9;
int mxi = 0;
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
if (f[i] < mxx) {
mxi = i;
mxx = f[i];
}
}
vis[mxi] = 1;
d[++cnt] = mxi;
}
printf("%.7lf", f[1]);
return 0;
}
|
### Prompt
Please create a solution in Cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, d[1005], cnt;
double p[1005][1005], f[1005], g[1005], h[1005];
bool vis[1005];
int main() {
cin >> n;
int x;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> x;
p[i][j] = (double)x / 100.0;
}
}
vis[n] = 1;
d[++cnt] = n;
for (int i = 1; i <= n; ++i) g[i] = 1, h[i] = 0;
for (int j = 1; j < n; ++j) {
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
h[i] += f[d[cnt]] * g[i] * p[i][d[cnt]];
g[i] *= (1 - p[i][d[cnt]]);
f[i] = (h[i] + 1) / (1 - g[i]);
}
double mxx = 1e9;
int mxi = 0;
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
if (f[i] < mxx) {
mxi = i;
mxx = f[i];
}
}
vis[mxi] = 1;
d[++cnt] = mxi;
}
printf("%.7lf", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n;
double p[N][N], d[N], pr[N];
int vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
p[i][j] = x / 100.0;
}
for (int i = 1; i <= n; i++) d[i] = 1, pr[i] = 1 - p[i][n];
if (n == 1) {
printf("0.000000000");
return 0;
}
vis[n] = 1;
d[n] = 0;
for (int i = 1; i < n; i++) {
int u = 0;
double mn = 1e18;
for (int j = 1; j <= n; j++)
if (!vis[j] && d[j] / (1 - pr[j]) < mn) mn = d[j] / (1 - pr[j]), u = j;
vis[u] = 1;
if (u == 1) {
printf("%.10lf", d[1] / (1 - pr[1]));
break;
}
for (int j = 1; j <= n; j++)
d[j] += (d[u] / (1 - pr[u])) * p[j][u] * pr[j], pr[j] *= (1 - p[j][u]);
}
return 0;
}
|
### Prompt
Your challenge is to write a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n;
double p[N][N], d[N], pr[N];
int vis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int x;
scanf("%d", &x);
p[i][j] = x / 100.0;
}
for (int i = 1; i <= n; i++) d[i] = 1, pr[i] = 1 - p[i][n];
if (n == 1) {
printf("0.000000000");
return 0;
}
vis[n] = 1;
d[n] = 0;
for (int i = 1; i < n; i++) {
int u = 0;
double mn = 1e18;
for (int j = 1; j <= n; j++)
if (!vis[j] && d[j] / (1 - pr[j]) < mn) mn = d[j] / (1 - pr[j]), u = j;
vis[u] = 1;
if (u == 1) {
printf("%.10lf", d[1] / (1 - pr[1]));
break;
}
for (int j = 1; j <= n; j++)
d[j] += (d[u] / (1 - pr[u])) * p[j][u] * pr[j], pr[j] *= (1 - p[j][u]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000001000;
const long long INFL = 2000000000000001000;
int solve();
int main() {
srand(2317);
cout.precision(10);
cout.setf(ios::fixed);
int tn = 1;
for (int i = 0; i < tn; ++i) solve();
}
const int maxn = 1001;
double p[maxn][maxn];
bool used[maxn];
double M[maxn];
double CM[maxn];
double rem[maxn];
int n;
void calccm(int u) {
if (rem[u] == 1) {
CM[u] = 1e18;
return;
}
CM[u] = (M[u] + 1.0) / (1 - rem[u]);
}
void get(int u) {
used[u] = true;
for (int i = 0; i < int(n); ++i) {
if (used[i]) continue;
M[i] += CM[u] * rem[i] * p[i][u];
rem[i] *= (1 - p[i][u]);
calccm(i);
}
}
int solve() {
cin >> n;
for (int i = 0; i < int(n); ++i)
for (int j = 0; j < int(n); ++j) {
int per;
scanf("%d", &per);
p[i][j] = (double)per / 100.0;
}
for (int i = 0; i < int(n - 1); ++i) rem[i] = 1;
fill(CM, CM + n - 1, 1e18);
get(n - 1);
while (!used[0]) {
int u = -1;
for (int i = 0; i < int(n); ++i) {
if (used[i]) continue;
if (u == -1 || CM[i] < CM[u]) u = i;
}
assert(u != -1);
assert(CM[u] < 1e18);
get(u);
}
cout << CM[0] << '\n';
return 0;
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000001000;
const long long INFL = 2000000000000001000;
int solve();
int main() {
srand(2317);
cout.precision(10);
cout.setf(ios::fixed);
int tn = 1;
for (int i = 0; i < tn; ++i) solve();
}
const int maxn = 1001;
double p[maxn][maxn];
bool used[maxn];
double M[maxn];
double CM[maxn];
double rem[maxn];
int n;
void calccm(int u) {
if (rem[u] == 1) {
CM[u] = 1e18;
return;
}
CM[u] = (M[u] + 1.0) / (1 - rem[u]);
}
void get(int u) {
used[u] = true;
for (int i = 0; i < int(n); ++i) {
if (used[i]) continue;
M[i] += CM[u] * rem[i] * p[i][u];
rem[i] *= (1 - p[i][u]);
calccm(i);
}
}
int solve() {
cin >> n;
for (int i = 0; i < int(n); ++i)
for (int j = 0; j < int(n); ++j) {
int per;
scanf("%d", &per);
p[i][j] = (double)per / 100.0;
}
for (int i = 0; i < int(n - 1); ++i) rem[i] = 1;
fill(CM, CM + n - 1, 1e18);
get(n - 1);
while (!used[0]) {
int u = -1;
for (int i = 0; i < int(n); ++i) {
if (used[i]) continue;
if (u == -1 || CM[i] < CM[u]) u = i;
}
assert(u != -1);
assert(CM[u] < 1e18);
get(u);
}
cout << CM[0] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
double p[1005][1005];
double ps[1024], val[1024];
bool vis[1024];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
for (int i = 1; i <= n; i++) {
ps[i] = 1;
val[i] = 1;
}
ps[n] = 0;
val[n] = 0;
while (1) {
double d = 1000000000000000000LL;
int wz = 0;
for (int i = 1; i <= n; i++) {
if (ps[i] > 0.99999999 || vis[i]) {
continue;
}
if (val[i] / (1 - ps[i]) < d) {
d = val[i] / (1 - ps[i]);
wz = i;
}
}
if (!wz) {
break;
}
vis[wz] = true;
for (int i = 1; i <= n; i++) {
if (vis[i]) {
continue;
}
val[i] += ps[i] * p[i][wz] * d;
ps[i] *= (1 - p[i][wz]);
}
}
printf("%.10f\n", val[1] / (1 - ps[1]));
return 0;
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
double p[1005][1005];
double ps[1024], val[1024];
bool vis[1024];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
for (int i = 1; i <= n; i++) {
ps[i] = 1;
val[i] = 1;
}
ps[n] = 0;
val[n] = 0;
while (1) {
double d = 1000000000000000000LL;
int wz = 0;
for (int i = 1; i <= n; i++) {
if (ps[i] > 0.99999999 || vis[i]) {
continue;
}
if (val[i] / (1 - ps[i]) < d) {
d = val[i] / (1 - ps[i]);
wz = i;
}
}
if (!wz) {
break;
}
vis[wz] = true;
for (int i = 1; i <= n; i++) {
if (vis[i]) {
continue;
}
val[i] += ps[i] * p[i][wz] * d;
ps[i] *= (1 - p[i][wz]);
}
}
printf("%.10f\n", val[1] / (1 - ps[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 50;
const double INF = 1e18;
int n;
double p[N][N], dp[N], coe[N];
bool vs[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (int i = 1; i <= n; i++) coe[i] = dp[i] = 1;
dp[n] = coe[n] = 0;
for (int i = 1; i <= n; i++) {
int u = 0;
double mn = INF;
for (int j = 1; j <= n; j++)
if (!vs[j] && dp[j] / (1 - coe[j]) < mn) u = j, mn = dp[j] / (1 - coe[j]);
vs[u] = true;
if (u == 1) {
printf("%.10lf", mn);
return 0;
}
for (int j = 1; j <= n; j++)
dp[j] += mn * p[j][u] * coe[j], coe[j] *= 1 - p[j][u];
}
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 50;
const double INF = 1e18;
int n;
double p[N][N], dp[N], coe[N];
bool vs[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
for (int i = 1; i <= n; i++) coe[i] = dp[i] = 1;
dp[n] = coe[n] = 0;
for (int i = 1; i <= n; i++) {
int u = 0;
double mn = INF;
for (int j = 1; j <= n; j++)
if (!vs[j] && dp[j] / (1 - coe[j]) < mn) u = j, mn = dp[j] / (1 - coe[j]);
vs[u] = true;
if (u == 1) {
printf("%.10lf", mn);
return 0;
}
for (int j = 1; j <= n; j++)
dp[j] += mn * p[j][u] * coe[j], coe[j] *= 1 - p[j][u];
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
int p[1013][1013];
bool done[1013];
double ans[1013];
double tot[1013];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) {
ans[i] = 1 / 0.;
tot[i] = 1;
}
ans[n - 1] = 0;
for (int r = 0; r < n; r++) {
int i = -1;
for (int j = 0; j < n; j++)
if (!done[j] && (i == -1 || ans[j] < ans[i])) i = j;
done[i] = 1;
for (int j = 0; j < n; j++)
if (!done[j] && p[j][i]) {
double cur = (ans[j] == 1 / 0.) ? 1 : ans[j] * (1 - tot[j]);
cur += tot[j] * p[j][i] / 100. * ans[i];
cur /= (1 - tot[j] * (1 - p[j][i] / 100.));
if (cur < ans[j]) {
ans[j] = cur;
tot[j] *= (1 - p[j][i] / 100.);
}
}
}
printf("%1.9f\n", ans[0]);
return 0;
}
|
### Prompt
Create a solution in Cpp for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int p[1013][1013];
bool done[1013];
double ans[1013];
double tot[1013];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) {
ans[i] = 1 / 0.;
tot[i] = 1;
}
ans[n - 1] = 0;
for (int r = 0; r < n; r++) {
int i = -1;
for (int j = 0; j < n; j++)
if (!done[j] && (i == -1 || ans[j] < ans[i])) i = j;
done[i] = 1;
for (int j = 0; j < n; j++)
if (!done[j] && p[j][i]) {
double cur = (ans[j] == 1 / 0.) ? 1 : ans[j] * (1 - tot[j]);
cur += tot[j] * p[j][i] / 100. * ans[i];
cur /= (1 - tot[j] * (1 - p[j][i] / 100.));
if (cur < ans[j]) {
ans[j] = cur;
tot[j] *= (1 - p[j][i] / 100.);
}
}
}
printf("%1.9f\n", ans[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
double p[N][N], E[N], s[N];
bool vis[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
vis[n] = true;
if (n == 1) return puts("0"), 0;
for (int i = 1; i < n; i++) {
E[i] = 1.0;
s[i] = 1 - p[i][n];
}
for (int i = 1; i <= n; i++) {
double Min = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && E[j] / (1.0 - s[j]) < Min)
Min = E[j] / (1.0 - s[j]), pos = j;
vis[pos] = true;
if (pos == 1) return printf("%.10lf\n", E[1] / (1 - s[1])), 0;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - s[pos]) * s[j] * p[j][pos], s[j] *= (1 - p[j][pos]);
}
return 0;
}
|
### Prompt
Create a solution in CPP for the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
double p[N][N], E[N], s[N];
bool vis[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100.0;
vis[n] = true;
if (n == 1) return puts("0"), 0;
for (int i = 1; i < n; i++) {
E[i] = 1.0;
s[i] = 1 - p[i][n];
}
for (int i = 1; i <= n; i++) {
double Min = 1e18;
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && E[j] / (1.0 - s[j]) < Min)
Min = E[j] / (1.0 - s[j]), pos = j;
vis[pos] = true;
if (pos == 1) return printf("%.10lf\n", E[1] / (1 - s[1])), 0;
for (int j = 1; j <= n; j++)
E[j] += E[pos] / (1 - s[pos]) * s[j] * p[j][pos], s[j] *= (1 - p[j][pos]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
template <typename tn>
void read(tn &a) {
tn x = 0, f = 1;
char c = ' ';
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
a = x * f;
}
const int N = 1010;
int n, vis[N];
double mp[N][N], f[N], g[N], p[N];
int main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int p;
read(p);
mp[i][j] = p / 100.;
}
for (int i = 1; i < n; i++) f[i] = 1e9, p[i] = 1;
for (int t = 1; t <= n; t++) {
int x = 0;
for (int i = 1; i <= n; i++)
if (!vis[i] && (!x || f[i] < f[x])) x = i;
vis[x] = 1;
for (int i = 1; i <= n; i++)
if (!vis[i] &&
(g[i] + p[i] * mp[i][x] * (f[x] + 1) + (p[i] * (1 - mp[i][x]))) /
(1 - p[i] * (1 - mp[i][x])) <
f[i]) {
f[i] = (g[i] + p[i] * mp[i][x] * (f[x] + 1) + (p[i] * (1 - mp[i][x]))) /
(1 - p[i] * (1 - mp[i][x]));
g[i] = (g[i] + p[i] * mp[i][x] * (f[x] + 1));
p[i] = p[i] * (1 - mp[i][x]);
}
}
cout << fixed << setprecision(10) << f[1] << '\n';
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename tn>
void read(tn &a) {
tn x = 0, f = 1;
char c = ' ';
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
a = x * f;
}
const int N = 1010;
int n, vis[N];
double mp[N][N], f[N], g[N], p[N];
int main() {
read(n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
int p;
read(p);
mp[i][j] = p / 100.;
}
for (int i = 1; i < n; i++) f[i] = 1e9, p[i] = 1;
for (int t = 1; t <= n; t++) {
int x = 0;
for (int i = 1; i <= n; i++)
if (!vis[i] && (!x || f[i] < f[x])) x = i;
vis[x] = 1;
for (int i = 1; i <= n; i++)
if (!vis[i] &&
(g[i] + p[i] * mp[i][x] * (f[x] + 1) + (p[i] * (1 - mp[i][x]))) /
(1 - p[i] * (1 - mp[i][x])) <
f[i]) {
f[i] = (g[i] + p[i] * mp[i][x] * (f[x] + 1) + (p[i] * (1 - mp[i][x]))) /
(1 - p[i] * (1 - mp[i][x]));
g[i] = (g[i] + p[i] * mp[i][x] * (f[x] + 1));
p[i] = p[i] * (1 - mp[i][x]);
}
}
cout << fixed << setprecision(10) << f[1] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
double A[1010], B[1010];
double p[1010][1010];
double dis(int u) { return (B[u] + 1) / (1 - A[u]); }
int mark[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
for (int i = 1; i <= n - 1; i++) A[i] = 1;
A[n] = 0, B[n] = -1;
for (int k = 1; k <= n; k++) {
int u = -1;
for (int i = 1; i <= n; i++) {
if ((u == -1 || dis(i) < dis(u) && !mark[i])) u = i;
}
mark[u] = 1;
for (int v = 1; v <= n; v++) {
if (mark[v]) continue;
B[v] += A[v] * p[v][u] * dis(u);
A[v] *= (1 - p[v][u]);
}
}
printf("%.10f\n", dis(1));
return 0;
}
|
### Prompt
Your challenge is to write a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
double A[1010], B[1010];
double p[1010][1010];
double dis(int u) { return (B[u] + 1) / (1 - A[u]); }
int mark[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
for (int i = 1; i <= n - 1; i++) A[i] = 1;
A[n] = 0, B[n] = -1;
for (int k = 1; k <= n; k++) {
int u = -1;
for (int i = 1; i <= n; i++) {
if ((u == -1 || dis(i) < dis(u) && !mark[i])) u = i;
}
mark[u] = 1;
for (int v = 1; v <= n; v++) {
if (mark[v]) continue;
B[v] += A[v] * p[v][u] * dis(u);
A[v] *= (1 - p[v][u]);
}
}
printf("%.10f\n", dis(1));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n, vis[N];
double dp[N], p[N][N], tmp[N];
template <typename _Tp>
inline void IN(_Tp& x) {
char ch;
bool flag = 0;
x = 0;
while (ch = getchar(), !isdigit(ch))
if (ch == '-') flag = 1;
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
if (flag) x = -x;
}
int main() {
IN(n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) IN(p[i][j]), p[i][j] = p[i][j] / 100.00;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i) dp[i] = 1, tmp[i] = 1 - p[i][n];
dp[n] = 0, vis[n] = true;
for (int i = 1; i <= n; ++i) {
int id = 0;
double Mi = 1e18;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dp[j] / (1 - tmp[j]) < Mi)
id = j, Mi = dp[j] / (1 - tmp[j]);
vis[id] = true;
if (id == 1) {
printf("%.10lf\n", dp[1] / (1 - tmp[1]));
return 0;
}
for (int j = 1; j <= n; ++j)
dp[j] += (dp[id] / (1 - tmp[id])) * p[j][id] * tmp[j],
tmp[j] *= (1 - p[j][id]);
}
return 0;
}
|
### Prompt
Please provide a Cpp coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int n, vis[N];
double dp[N], p[N][N], tmp[N];
template <typename _Tp>
inline void IN(_Tp& x) {
char ch;
bool flag = 0;
x = 0;
while (ch = getchar(), !isdigit(ch))
if (ch == '-') flag = 1;
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
if (flag) x = -x;
}
int main() {
IN(n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) IN(p[i][j]), p[i][j] = p[i][j] / 100.00;
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i) dp[i] = 1, tmp[i] = 1 - p[i][n];
dp[n] = 0, vis[n] = true;
for (int i = 1; i <= n; ++i) {
int id = 0;
double Mi = 1e18;
for (int j = 1; j <= n; ++j)
if (!vis[j] && dp[j] / (1 - tmp[j]) < Mi)
id = j, Mi = dp[j] / (1 - tmp[j]);
vis[id] = true;
if (id == 1) {
printf("%.10lf\n", dp[1] / (1 - tmp[1]));
return 0;
}
for (int j = 1; j <= n; ++j)
dp[j] += (dp[id] / (1 - tmp[id])) * p[j][id] * tmp[j],
tmp[j] *= (1 - p[j][id]);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
const long double eps = 1e-10;
int n, vis[N];
long double p[N][N], cnt[N], dis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1, x; j <= n; ++j)
scanf("%d", &x), p[i][j] = (long double)(x) / (long double)(100);
}
for (int i = 1; i < n; ++i) dis[i] = 1.0, cnt[i] = 1.0;
cnt[n] = 0, dis[n] = 0;
for (int i = 1; i <= n; ++i) {
long double mn = 1e10;
int cur = -1;
for (int j = 1; j <= n; ++j)
if (!vis[j] && cnt[j] + eps < 1 && dis[j] / (1 - cnt[j]) < mn)
mn = dis[j] / (1 - cnt[j]), cur = j;
vis[cur] = 1;
dis[cur] = dis[cur] / (1 - cnt[cur]);
if (cur == n) dis[cur] = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
dis[j] += cnt[j] * p[j][cur] * dis[cur];
cnt[j] *= (1 - p[j][cur]);
}
}
printf("%.15Lf", dis[1]);
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
const long double eps = 1e-10;
int n, vis[N];
long double p[N][N], cnt[N], dis[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1, x; j <= n; ++j)
scanf("%d", &x), p[i][j] = (long double)(x) / (long double)(100);
}
for (int i = 1; i < n; ++i) dis[i] = 1.0, cnt[i] = 1.0;
cnt[n] = 0, dis[n] = 0;
for (int i = 1; i <= n; ++i) {
long double mn = 1e10;
int cur = -1;
for (int j = 1; j <= n; ++j)
if (!vis[j] && cnt[j] + eps < 1 && dis[j] / (1 - cnt[j]) < mn)
mn = dis[j] / (1 - cnt[j]), cur = j;
vis[cur] = 1;
dis[cur] = dis[cur] / (1 - cnt[cur]);
if (cur == n) dis[cur] = 0;
for (int j = 1; j <= n; ++j)
if (!vis[j]) {
dis[j] += cnt[j] * p[j][cur] * dis[cur];
cnt[j] *= (1 - p[j][cur]);
}
}
printf("%.15Lf", dis[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
int n, vis[1001], a[1001];
double p[1001][1001], d[1001], pr[1001], sum[1001];
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; ++i) {
register int now = 0;
sum[i] = pr[i] = 1.0;
for (register int j = 1; j <= n; ++j)
scanf("%d", &now), p[i][j] = now * 0.01;
}
vis[n] = 1;
a[1] = n;
d[0] = 1e18;
for (register int i = 1; i < n; ++i) {
for (register int j = 1; j <= n; ++j) {
if (vis[j]) continue;
sum[j] += d[a[i]] * p[j][a[i]] * pr[j];
pr[j] *= (1 - p[j][a[i]]);
d[j] = sum[j] / (1 - pr[j]);
}
register int pos = 0;
for (register int j = 1; j <= n; ++j)
if (!vis[j] && d[pos] > d[j]) pos = j;
a[i + 1] = pos;
vis[pos] = 1;
}
printf("%.10lf\n", d[1]);
}
|
### Prompt
Please formulate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
int n, vis[1001], a[1001];
double p[1001][1001], d[1001], pr[1001], sum[1001];
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; ++i) {
register int now = 0;
sum[i] = pr[i] = 1.0;
for (register int j = 1; j <= n; ++j)
scanf("%d", &now), p[i][j] = now * 0.01;
}
vis[n] = 1;
a[1] = n;
d[0] = 1e18;
for (register int i = 1; i < n; ++i) {
for (register int j = 1; j <= n; ++j) {
if (vis[j]) continue;
sum[j] += d[a[i]] * p[j][a[i]] * pr[j];
pr[j] *= (1 - p[j][a[i]]);
d[j] = sum[j] / (1 - pr[j]);
}
register int pos = 0;
for (register int j = 1; j <= n; ++j)
if (!vis[j] && d[pos] > d[j]) pos = j;
a[i + 1] = pos;
vis[pos] = 1;
}
printf("%.10lf\n", d[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
int n, vi[N];
double p[N][N], dp[N], sum[N][N], all[N];
inline int read() {
int f = 1, x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
return x * f;
}
signed main() {
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = (double)read() / 100.0;
for (int i = 1; i < n; i++) dp[i] = 1e9, sum[i][0] = all[i] = 1;
for (int i = 1; i <= n; i++) {
double MIN = 1e9;
int wh;
for (int j = 1; j <= n; j++)
if (!vi[j] && dp[j] < MIN) MIN = dp[j], wh = j;
if (wh == 1) break;
vi[wh] = 1;
for (int j = 1; j <= n; j++) {
if (vi[j]) continue;
all[j] += p[j][wh] * sum[j][i - 1] * dp[wh];
sum[j][i] = sum[j][i - 1] * (1 - p[j][wh]);
dp[j] = all[j] / (1.0 - sum[j][i]);
}
}
printf("%.9lf\n", dp[1]);
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
int n, vi[N];
double p[N][N], dp[N], sum[N][N], all[N];
inline int read() {
int f = 1, x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
return x * f;
}
signed main() {
n = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) p[i][j] = (double)read() / 100.0;
for (int i = 1; i < n; i++) dp[i] = 1e9, sum[i][0] = all[i] = 1;
for (int i = 1; i <= n; i++) {
double MIN = 1e9;
int wh;
for (int j = 1; j <= n; j++)
if (!vi[j] && dp[j] < MIN) MIN = dp[j], wh = j;
if (wh == 1) break;
vi[wh] = 1;
for (int j = 1; j <= n; j++) {
if (vi[j]) continue;
all[j] += p[j][wh] * sum[j][i - 1] * dp[wh];
sum[j][i] = sum[j][i - 1] * (1 - p[j][wh]);
dp[j] = all[j] / (1.0 - sum[j][i]);
}
}
printf("%.9lf\n", dp[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
constexpr int inf32 = 0x3f3f3f3f;
constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout.precision(12), cout << fixed;
int n;
cin >> n;
int cnt = n;
vector<vector<double>> p(n, vector<double>(n));
for (int u = 0; u < (n); ++u)
for (int v = 0; v < (n); ++v) {
int first;
cin >> first, p[u][v] = first / 100.;
}
vector<int> vis(n, false);
vector<double> f(n), now(n, 1);
auto calc = [&](int u) -> double { return (f[u] + now[u]) / (1 - now[u]); };
int u = n - 1;
auto trans = [&]() -> void {
if (u != n - 1) f[u] = calc(u);
vis[u] = true, --cnt;
for (int v = 0; v < (n); ++v)
if (!vis[v]) {
f[v] += now[v] * p[v][u] * (f[u] + 1);
now[v] *= 1 - p[v][u];
}
u = -1;
};
for (trans(); cnt; trans())
for (int v = 0; v < (n); ++v)
if (!vis[v] && (!~u || calc(v) < calc(u))) u = v;
cout << f[0] << '\n';
return 0;
}
|
### Prompt
Develop a solution in Cpp to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int inf32 = 0x3f3f3f3f;
constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout.precision(12), cout << fixed;
int n;
cin >> n;
int cnt = n;
vector<vector<double>> p(n, vector<double>(n));
for (int u = 0; u < (n); ++u)
for (int v = 0; v < (n); ++v) {
int first;
cin >> first, p[u][v] = first / 100.;
}
vector<int> vis(n, false);
vector<double> f(n), now(n, 1);
auto calc = [&](int u) -> double { return (f[u] + now[u]) / (1 - now[u]); };
int u = n - 1;
auto trans = [&]() -> void {
if (u != n - 1) f[u] = calc(u);
vis[u] = true, --cnt;
for (int v = 0; v < (n); ++v)
if (!vis[v]) {
f[v] += now[v] * p[v][u] * (f[u] + 1);
now[v] *= 1 - p[v][u];
}
u = -1;
};
for (trans(); cnt; trans())
for (int v = 0; v < (n); ++v)
if (!vis[v] && (!~u || calc(v) < calc(u))) u = v;
cout << f[0] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const double eps = 1e-8;
double p[maxn][maxn], lp[maxn], dis[maxn], cur[maxn];
int n;
bool vis[maxn];
inline void dijkstra() {
register int i, j, t;
for (i = 0; i < n; i++) vis[i] = 0, dis[i] = 1. / 0., cur[i] = 0, lp[i] = 1;
dis[n - 1] = 0;
for (i = 0; i < n; i++) {
t = -1;
for (j = 0; j < n; j++)
if (!vis[j] && dis[j] != 1. / 0. && (t == -1 || dis[j] < dis[t])) t = j;
if (t == -1) return;
vis[t] = 1;
for (j = 0; j < n; j++)
if (!vis[j] && p[j][t] > eps &&
dis[j] >
(cur[j] + lp[j] * p[j][t] + 1) / (1 - lp[j] * (1 - p[j][t]))) {
cur[j] += lp[j] * p[j][t] * dis[t];
lp[j] *= 1 - p[j][t];
dis[j] = (cur[j] + 1) / (1 - lp[j]);
}
}
}
int main() {
int i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100;
for (i = 0; i < n; i++) p[i][i] = 0;
dijkstra();
printf("%.12lf\n", dis[0]);
return 0;
}
|
### Prompt
Generate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const double eps = 1e-8;
double p[maxn][maxn], lp[maxn], dis[maxn], cur[maxn];
int n;
bool vis[maxn];
inline void dijkstra() {
register int i, j, t;
for (i = 0; i < n; i++) vis[i] = 0, dis[i] = 1. / 0., cur[i] = 0, lp[i] = 1;
dis[n - 1] = 0;
for (i = 0; i < n; i++) {
t = -1;
for (j = 0; j < n; j++)
if (!vis[j] && dis[j] != 1. / 0. && (t == -1 || dis[j] < dis[t])) t = j;
if (t == -1) return;
vis[t] = 1;
for (j = 0; j < n; j++)
if (!vis[j] && p[j][t] > eps &&
dis[j] >
(cur[j] + lp[j] * p[j][t] + 1) / (1 - lp[j] * (1 - p[j][t]))) {
cur[j] += lp[j] * p[j][t] * dis[t];
lp[j] *= 1 - p[j][t];
dis[j] = (cur[j] + 1) / (1 - lp[j]);
}
}
}
int main() {
int i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) scanf("%lf", &p[i][j]), p[i][j] /= 100;
for (i = 0; i < n; i++) p[i][i] = 0;
dijkstra();
printf("%.12lf\n", dis[0]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n;
int p[N][N];
bool f[N];
double d[N], K[N], B[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
scanf("%d", &p[i][j]);
if (i == j) p[i][j];
}
for (int i = 1; i <= n; ++i) {
d[i] = 1e60;
K[i] = 1;
B[i] = 1;
}
d[n] = 0;
while (1) {
int x = -1;
for (int i = 1; i <= n; ++i)
if (!f[i] && (!~x || d[i] < d[x])) x = i;
f[x] = 1;
if (x == 1) break;
for (int i = 1; i <= n; ++i) {
if (f[i] || !p[i][x]) continue;
B[i] += K[i] * 0.01 * p[i][x] * d[x];
K[i] *= 1 - 0.01 * p[i][x];
d[i] = min(d[i], B[i] / (1 - K[i]));
}
}
printf("%.12f\n", d[1]);
return 0;
}
|
### Prompt
Please formulate a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n;
int p[N][N];
bool f[N];
double d[N], K[N], B[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
scanf("%d", &p[i][j]);
if (i == j) p[i][j];
}
for (int i = 1; i <= n; ++i) {
d[i] = 1e60;
K[i] = 1;
B[i] = 1;
}
d[n] = 0;
while (1) {
int x = -1;
for (int i = 1; i <= n; ++i)
if (!f[i] && (!~x || d[i] < d[x])) x = i;
f[x] = 1;
if (x == 1) break;
for (int i = 1; i <= n; ++i) {
if (f[i] || !p[i][x]) continue;
B[i] += K[i] * 0.01 * p[i][x] * d[x];
K[i] *= 1 - 0.01 * p[i][x];
d[i] = min(d[i], B[i] / (1 - K[i]));
}
}
printf("%.12f\n", d[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
bool vis[N];
int n;
double f[N];
double p[N][N], pro[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1, tmp; j <= n; j++) {
scanf("%d", &tmp);
p[i][j] = 1.0 * tmp / 100.0;
}
}
if (n == 1) {
printf("0\n");
return 0;
}
vis[n] = true;
for (int i = 1; i < n; i++) {
f[i] = 1;
pro[i] = 1 - p[i][n];
}
for (int i = 1; i <= n; i++) {
double minn = 1e18 + 7;
int pos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && f[j] / (1 - pro[j]) < minn) {
minn = f[j] / (1 - pro[j]);
pos = j;
}
}
if (pos == 1) {
printf("%.12lf\n", f[1] / (1 - pro[1]));
return 0;
}
vis[pos] = true;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
f[j] += f[pos] / (1 - pro[pos]) * p[j][pos] * pro[j];
pro[j] *= (1 - p[j][pos]);
}
}
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
bool vis[N];
int n;
double f[N];
double p[N][N], pro[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1, tmp; j <= n; j++) {
scanf("%d", &tmp);
p[i][j] = 1.0 * tmp / 100.0;
}
}
if (n == 1) {
printf("0\n");
return 0;
}
vis[n] = true;
for (int i = 1; i < n; i++) {
f[i] = 1;
pro[i] = 1 - p[i][n];
}
for (int i = 1; i <= n; i++) {
double minn = 1e18 + 7;
int pos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && f[j] / (1 - pro[j]) < minn) {
minn = f[j] / (1 - pro[j]);
pos = j;
}
}
if (pos == 1) {
printf("%.12lf\n", f[1] / (1 - pro[1]));
return 0;
}
vis[pos] = true;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
f[j] += f[pos] / (1 - pro[pos]) * p[j][pos] * pro[j];
pro[j] *= (1 - p[j][pos]);
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
bool vis[N];
int n;
double f[N];
double p[N][N], pro[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1, tmp; j <= n; j++) {
scanf("%d", &tmp);
p[i][j] = 1.0 * tmp / 100.0;
}
}
if (n == 1) {
printf("0\n");
return 0;
}
vis[n] = true;
for (int i = 1; i < n; i++) {
f[i] = 1;
pro[i] = 1 - p[i][n];
}
for (int i = 1; i <= n; i++) {
double minn = 1e18 + 7;
int pos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && f[j] / (1 - pro[j]) < minn) {
minn = f[j] / (1 - pro[j]);
pos = j;
}
}
if (pos == 1) {
printf("%.12lf\n", f[1] / (1 - pro[1]));
return 0;
}
vis[pos] = true;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
f[j] += f[pos] / (1 - pro[pos]) * p[j][pos] * pro[j];
pro[j] *= (1 - p[j][pos]);
}
}
}
return 0;
}
|
### Prompt
Generate a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
bool vis[N];
int n;
double f[N];
double p[N][N], pro[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1, tmp; j <= n; j++) {
scanf("%d", &tmp);
p[i][j] = 1.0 * tmp / 100.0;
}
}
if (n == 1) {
printf("0\n");
return 0;
}
vis[n] = true;
for (int i = 1; i < n; i++) {
f[i] = 1;
pro[i] = 1 - p[i][n];
}
for (int i = 1; i <= n; i++) {
double minn = 1e18 + 7;
int pos = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && f[j] / (1 - pro[j]) < minn) {
minn = f[j] / (1 - pro[j]);
pos = j;
}
}
if (pos == 1) {
printf("%.12lf\n", f[1] / (1 - pro[1]));
return 0;
}
vis[pos] = true;
for (int j = 1; j <= n; j++) {
if (!vis[j]) {
f[j] += f[pos] / (1 - pro[pos]) * p[j][pos] * pro[j];
pro[j] *= (1 - p[j][pos]);
}
}
}
return 0;
}
```
|
#include <bits/stdc++.h>
const double INF = 1e80;
const int N = 1e3;
int n;
double p[N + 5][N + 5], f[N + 5], g[N + 5];
bool vis[N + 5];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
for (int i = 1; i <= n; i++) f[i] = g[i] = 1;
f[n] = g[n] = 0;
for (int t = 1; t <= n; t++) {
int u = 0;
double min = INF;
for (int i = 1; i <= n; i++) {
if (!vis[i] && f[i] / (1 - g[i]) < min) {
u = i, min = f[i] / (1 - g[i]);
}
}
if (u == 0) break;
vis[u] = true, f[u] /= 1 - g[u];
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
f[i] += f[u] * p[i][u] * g[i];
g[i] *= 1 - p[i][u];
}
}
}
printf("%.10lf\n", f[1]);
return 0;
}
|
### Prompt
In Cpp, your task is to solve the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const double INF = 1e80;
const int N = 1e3;
int n;
double p[N + 5][N + 5], f[N + 5], g[N + 5];
bool vis[N + 5];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &p[i][j]);
p[i][j] /= 100;
}
}
for (int i = 1; i <= n; i++) f[i] = g[i] = 1;
f[n] = g[n] = 0;
for (int t = 1; t <= n; t++) {
int u = 0;
double min = INF;
for (int i = 1; i <= n; i++) {
if (!vis[i] && f[i] / (1 - g[i]) < min) {
u = i, min = f[i] / (1 - g[i]);
}
}
if (u == 0) break;
vis[u] = true, f[u] /= 1 - g[u];
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
f[i] += f[u] * p[i][u] * g[i];
g[i] *= 1 - p[i][u];
}
}
}
printf("%.10lf\n", f[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10;
const int MN = 1e3 + 10;
int n;
long double E[MN], D[MN], T[MN], p[MN][MN];
bool mk[MN];
void relax(int v) {
for (int u = 0; u < n; ++u)
if (!mk[u]) {
D[u] += T[u] * p[u][v] * E[v];
T[u] *= (1 - p[u][v]);
if (T[u] == 0)
E[u] = 1 + D[u];
else
E[u] = (1 + D[u]) / (1 - T[u]);
}
}
int main() {
ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
int t;
cin >> t;
p[i][j] = (long double)t / 100;
}
for (int i = 0; i < n; ++i) E[i] = 1e18, T[i] = 1;
E[n - 1] = 0;
for (int i = 0; i < n; ++i) {
int mn = -1;
for (int i = 0; i < n; ++i)
if (!mk[i] && (mn == -1 || E[i] < E[mn])) mn = i;
mk[mn] = true;
relax(mn);
}
cout << setprecision(12) << fixed << E[0] << endl;
return 0;
}
|
### Prompt
Your challenge is to write a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10;
const int MN = 1e3 + 10;
int n;
long double E[MN], D[MN], T[MN], p[MN][MN];
bool mk[MN];
void relax(int v) {
for (int u = 0; u < n; ++u)
if (!mk[u]) {
D[u] += T[u] * p[u][v] * E[v];
T[u] *= (1 - p[u][v]);
if (T[u] == 0)
E[u] = 1 + D[u];
else
E[u] = (1 + D[u]) / (1 - T[u]);
}
}
int main() {
ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
int t;
cin >> t;
p[i][j] = (long double)t / 100;
}
for (int i = 0; i < n; ++i) E[i] = 1e18, T[i] = 1;
E[n - 1] = 0;
for (int i = 0; i < n; ++i) {
int mn = -1;
for (int i = 0; i < n; ++i)
if (!mk[i] && (mn == -1 || E[i] < E[mn])) mn = i;
mk[mn] = true;
relax(mn);
}
cout << setprecision(12) << fixed << E[0] << endl;
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 1005;
int n, x;
double adj[MX][MX], p, res[MX], no[MX], acu[MX];
bitset<MX> bs;
bool igual(double a, double b) { return fabs(a - b) < 1e-12; }
bool le(double a, double b) { return a < b && !igual(a, b); }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
fill(res, res + MX, -1);
fill(no, no + MX, 1);
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", &x);
adj[i][j] = double(x) / 100;
}
bs[n - 1] = 1;
res[n - 1] = 0;
for (int i = 0; i < n - 1; i++)
if (adj[i][n - 1]) {
acu[i] += no[i] * adj[i][n - 1] * (res[n - 1] + 1);
no[i] *= 1 - adj[i][n - 1];
res[i] = (acu[i] + no[i]) / (1 - no[i]);
}
for (int _ = 1; _ < n; _++) {
int u = 0;
for (int i = 0; i < n; i++)
if (!bs[i] && res[i] != -1 && (res[u] == -1 || le(res[i], res[u]))) u = i;
bs[u] = 1;
for (int v = 0; v < n - 1; v++)
if (!bs[v] && adj[v][u]) {
acu[v] += no[v] * adj[v][u] * (res[u] + 1);
no[v] *= 1 - adj[v][u];
res[v] = (acu[v] + no[v]) / (1 - no[v]);
}
}
cout << fixed << setprecision(10) << res[0] << '\n';
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int MX = 1005;
int n, x;
double adj[MX][MX], p, res[MX], no[MX], acu[MX];
bitset<MX> bs;
bool igual(double a, double b) { return fabs(a - b) < 1e-12; }
bool le(double a, double b) { return a < b && !igual(a, b); }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
fill(res, res + MX, -1);
fill(no, no + MX, 1);
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
scanf("%d", &x);
adj[i][j] = double(x) / 100;
}
bs[n - 1] = 1;
res[n - 1] = 0;
for (int i = 0; i < n - 1; i++)
if (adj[i][n - 1]) {
acu[i] += no[i] * adj[i][n - 1] * (res[n - 1] + 1);
no[i] *= 1 - adj[i][n - 1];
res[i] = (acu[i] + no[i]) / (1 - no[i]);
}
for (int _ = 1; _ < n; _++) {
int u = 0;
for (int i = 0; i < n; i++)
if (!bs[i] && res[i] != -1 && (res[u] == -1 || le(res[i], res[u]))) u = i;
bs[u] = 1;
for (int v = 0; v < n - 1; v++)
if (!bs[v] && adj[v][u]) {
acu[v] += no[v] * adj[v][u] * (res[u] + 1);
no[v] *= 1 - adj[v][u];
res[v] = (acu[v] + no[v]) / (1 - no[v]);
}
}
cout << fixed << setprecision(10) << res[0] << '\n';
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int a = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) a = (a << 3) + (a << 1) + ch - '0';
return a * f;
}
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
const int inf = 0x3f3f3f3f;
const int INF = 2147483647;
const double PI = acos(-1);
int n, vis[N], a[N];
double sum[N], pre[N];
double p[N][N], d[N];
signed main() {
n = read();
for (int i = 1; i <= n; i++) {
sum[i] = pre[i] = 1.0;
for (int j = 1; j <= n; j++) p[i][j] = 0.01 * read();
}
vis[n] = 1;
a[1] = n;
d[0] = 1e18;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
sum[j] += d[a[i - 1]] * p[j][a[i - 1]] * pre[j];
pre[j] *= (1 - p[j][a[i - 1]]);
d[j] = sum[j] / (1.0 - pre[j]);
}
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && d[pos] > d[j]) pos = j;
vis[pos] = 1;
a[i] = pos;
}
printf("%.10lf\n", d[1]);
return 0;
}
|
### Prompt
Your task is to create a Cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int a = 0, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) a = (a << 3) + (a << 1) + ch - '0';
return a * f;
}
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
const int inf = 0x3f3f3f3f;
const int INF = 2147483647;
const double PI = acos(-1);
int n, vis[N], a[N];
double sum[N], pre[N];
double p[N][N], d[N];
signed main() {
n = read();
for (int i = 1; i <= n; i++) {
sum[i] = pre[i] = 1.0;
for (int j = 1; j <= n; j++) p[i][j] = 0.01 * read();
}
vis[n] = 1;
a[1] = n;
d[0] = 1e18;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
sum[j] += d[a[i - 1]] * p[j][a[i - 1]] * pre[j];
pre[j] *= (1 - p[j][a[i - 1]]);
d[j] = sum[j] / (1.0 - pre[j]);
}
int pos = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && d[pos] > d[j]) pos = j;
vis[pos] = 1;
a[i] = pos;
}
printf("%.10lf\n", d[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1010][1010], a[1010], b[1010], d[1010];
bool vis[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", p[i] + j), p[i][j] /= 100;
for (int i = 1; i <= n; i++) d[i] = 1e9, a[i] = b[i] = 1;
d[n] = 0;
for (int i = 1; i <= n; i++) {
double mn = 1e9;
int x;
for (int j = 1; j <= n; j++)
if (!vis[j] && d[j] < mn) {
mn = d[j];
x = j;
}
vis[x] = 1;
for (int y = 1; y <= n; y++)
if (!vis[y]) {
b[y] += p[y][x] * a[y] * d[x];
a[y] *= 1 - p[y][x];
d[y] = b[y] / (1 - a[y]);
}
}
printf("%.7lf\n", d[1]);
}
|
### Prompt
Generate a CPP solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n;
double p[1010][1010], a[1010], b[1010], d[1010];
bool vis[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%lf", p[i] + j), p[i][j] /= 100;
for (int i = 1; i <= n; i++) d[i] = 1e9, a[i] = b[i] = 1;
d[n] = 0;
for (int i = 1; i <= n; i++) {
double mn = 1e9;
int x;
for (int j = 1; j <= n; j++)
if (!vis[j] && d[j] < mn) {
mn = d[j];
x = j;
}
vis[x] = 1;
for (int y = 1; y <= n; y++)
if (!vis[y]) {
b[y] += p[y][x] * a[y] * d[x];
a[y] *= 1 - p[y][x];
d[y] = b[y] / (1 - a[y]);
}
}
printf("%.7lf\n", d[1]);
}
```
|
#include <bits/stdc++.h>
using namespace std;
char buf[1000000], *P1, *P2;
int n;
double dp[1005], nw[1005], a[1005][1005];
bool vs[1005];
int rd() {
int res = 0;
char c = 0;
while (!isdigit(c))
c = (P1 == P2 && (P2 = (P1 = buf) + fread(buf, 1, 1000000, stdin), P1 == P2)
? EOF
: *P1++);
while (isdigit(c))
res = res * 10 + c - 48,
c = (P1 == P2 && (P2 = (P1 = buf) + fread(buf, 1, 1000000, stdin), P1 == P2)
? EOF
: *P1++);
return res;
}
double calc(int u) { return (dp[u] + nw[u]) / (1 - nw[u]); }
void release(int u) {
vs[u] = 1;
for (int i = 1; i <= n; ++i)
if (!vs[i]) dp[i] += (calc(u) + 1) * a[i][u] * nw[i], nw[i] *= 1 - a[i][u];
}
int main() {
n = rd();
for (int i = 1; i < n; ++i) nw[i] = 1;
for (int i = 1, x; i <= n; ++i)
for (int j = 1; j <= n; ++j) x = rd(), a[i][j] = 0.01 * x;
release(n);
for (int i = 1, t = 0; i < n; ++i, t = 0) {
for (int j = 1; j <= n; ++j)
if (!vs[j] && (!t || calc(j) < calc(t))) t = j;
if (t == 1) break;
release(t);
}
printf("%.10lf\n", calc(1));
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
char buf[1000000], *P1, *P2;
int n;
double dp[1005], nw[1005], a[1005][1005];
bool vs[1005];
int rd() {
int res = 0;
char c = 0;
while (!isdigit(c))
c = (P1 == P2 && (P2 = (P1 = buf) + fread(buf, 1, 1000000, stdin), P1 == P2)
? EOF
: *P1++);
while (isdigit(c))
res = res * 10 + c - 48,
c = (P1 == P2 && (P2 = (P1 = buf) + fread(buf, 1, 1000000, stdin), P1 == P2)
? EOF
: *P1++);
return res;
}
double calc(int u) { return (dp[u] + nw[u]) / (1 - nw[u]); }
void release(int u) {
vs[u] = 1;
for (int i = 1; i <= n; ++i)
if (!vs[i]) dp[i] += (calc(u) + 1) * a[i][u] * nw[i], nw[i] *= 1 - a[i][u];
}
int main() {
n = rd();
for (int i = 1; i < n; ++i) nw[i] = 1;
for (int i = 1, x; i <= n; ++i)
for (int j = 1; j <= n; ++j) x = rd(), a[i][j] = 0.01 * x;
release(n);
for (int i = 1, t = 0; i < n; ++i, t = 0) {
for (int j = 1; j <= n; ++j)
if (!vs[j] && (!t || calc(j) < calc(t))) t = j;
if (t == 1) break;
release(t);
}
printf("%.10lf\n", calc(1));
return 0;
}
```
|
#include <bits/stdc++.h>
const int mod = 1000000007;
const int inf = 1000000009;
const long long INF = 1000000000000000009;
const long long big = 1000000000000000;
const long double eps = 0.000000000000000000001;
using namespace std;
double E[100005], CE[100005], P[100005];
double licz(int x) {
if (P[x] == 1) return inf;
return (CE[x] + P[x]) / ((double)1.0 - P[x]);
}
struct cmp {
bool operator()(int a, int b) const {
if (E[a] != E[b])
return E[a] < E[b];
else
return a < b;
}
};
set<int, cmp> S;
double T[1005][1005];
bool O[1005];
int main() {
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int a;
cin >> a;
T[i][j] = (double)a / 100.0;
}
P[i] = 1.0;
}
for (int i = 1; i < n; i++) {
P[i] = (double)1.0 - T[i][n];
CE[i] = T[i][n];
E[i] = licz(i);
S.insert(i);
}
O[n] = 1;
while (true) {
if (n == 1) break;
int x = *S.begin();
if (x == 1) break;
O[x] = 1;
for (int i = 1; i <= n; i++) {
if (O[i]) continue;
CE[i] += P[i] * T[i][x] * (E[x] + 1);
P[i] *= ((double)1.0 - T[i][x]);
}
S.clear();
for (int i = 1; i <= n; i++) {
if (!O[i]) {
E[i] = licz(i);
S.insert(i);
}
}
}
cout.precision(9);
cout << fixed << E[1];
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int mod = 1000000007;
const int inf = 1000000009;
const long long INF = 1000000000000000009;
const long long big = 1000000000000000;
const long double eps = 0.000000000000000000001;
using namespace std;
double E[100005], CE[100005], P[100005];
double licz(int x) {
if (P[x] == 1) return inf;
return (CE[x] + P[x]) / ((double)1.0 - P[x]);
}
struct cmp {
bool operator()(int a, int b) const {
if (E[a] != E[b])
return E[a] < E[b];
else
return a < b;
}
};
set<int, cmp> S;
double T[1005][1005];
bool O[1005];
int main() {
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int a;
cin >> a;
T[i][j] = (double)a / 100.0;
}
P[i] = 1.0;
}
for (int i = 1; i < n; i++) {
P[i] = (double)1.0 - T[i][n];
CE[i] = T[i][n];
E[i] = licz(i);
S.insert(i);
}
O[n] = 1;
while (true) {
if (n == 1) break;
int x = *S.begin();
if (x == 1) break;
O[x] = 1;
for (int i = 1; i <= n; i++) {
if (O[i]) continue;
CE[i] += P[i] * T[i][x] * (E[x] + 1);
P[i] *= ((double)1.0 - T[i][x]);
}
S.clear();
for (int i = 1; i <= n; i++) {
if (!O[i]) {
E[i] = licz(i);
S.insert(i);
}
}
}
cout.precision(9);
cout << fixed << E[1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int n, x;
bool vis[1005];
double E[1005], g[1005], a[1005][1005];
double calc(int u) { return (E[u] + g[u]) / (1.0 - g[u]); }
void Rel(int x) {
if (x == 1) {
printf("%.12lf\n", calc(x));
exit(0);
}
vis[x] = true;
for (int i = 1; i <= n; ++i)
if (!vis[i]) E[i] += g[i] * a[i][x] * (calc(x) + 1), g[i] *= 1.0 - a[i][x];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &x), a[i][j] = 0.01 * x;
for (int i = 1; i <= n; ++i) g[i] = 1.0;
g[n] = 0.0;
Rel(n);
while (1) {
int u = 0;
for (int i = 1; i <= n; ++i)
if (!vis[i] && (!u || calc(i) < calc(u))) u = i;
Rel(u);
}
return 0;
}
|
### Prompt
Please provide a CPP coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, x;
bool vis[1005];
double E[1005], g[1005], a[1005][1005];
double calc(int u) { return (E[u] + g[u]) / (1.0 - g[u]); }
void Rel(int x) {
if (x == 1) {
printf("%.12lf\n", calc(x));
exit(0);
}
vis[x] = true;
for (int i = 1; i <= n; ++i)
if (!vis[i]) E[i] += g[i] * a[i][x] * (calc(x) + 1), g[i] *= 1.0 - a[i][x];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &x), a[i][j] = 0.01 * x;
for (int i = 1; i <= n; ++i) g[i] = 1.0;
g[n] = 0.0;
Rel(n);
while (1) {
int u = 0;
for (int i = 1; i <= n; ++i)
if (!vis[i] && (!u || calc(i) < calc(u))) u = i;
Rel(u);
}
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int p[1001][1001];
int vis[1001];
double dis[1001], tmp[1001];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> p[i][j];
for (int i = 1; i <= n; i++) dis[i] = 1e18;
dis[n] = 0;
for (int i = 1; i <= n; i++) {
int u = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && (!u || dis[j] < dis[u])) u = j;
vis[u] = 1;
for (int j = 1; j <= n; j++)
if (!vis[j] && p[j][u]) {
if (dis[j] > 10000)
dis[j] = 0;
else
dis[j] -= 1.0 / tmp[j];
double lt = tmp[j];
tmp[j] = 1 - (1 - tmp[j]) * (1 - p[j][u] / 100.0);
if (lt > 0) dis[j] *= lt / tmp[j];
dis[j] += (1 - lt) * p[j][u] / 100.0 / tmp[j] * dis[u] + 1.0 / tmp[j];
}
}
cout << fixed << setprecision(15) << dis[1] << endl;
}
|
### Prompt
Construct a cpp code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int p[1001][1001];
int vis[1001];
double dis[1001], tmp[1001];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> p[i][j];
for (int i = 1; i <= n; i++) dis[i] = 1e18;
dis[n] = 0;
for (int i = 1; i <= n; i++) {
int u = 0;
for (int j = 1; j <= n; j++)
if (!vis[j] && (!u || dis[j] < dis[u])) u = j;
vis[u] = 1;
for (int j = 1; j <= n; j++)
if (!vis[j] && p[j][u]) {
if (dis[j] > 10000)
dis[j] = 0;
else
dis[j] -= 1.0 / tmp[j];
double lt = tmp[j];
tmp[j] = 1 - (1 - tmp[j]) * (1 - p[j][u] / 100.0);
if (lt > 0) dis[j] *= lt / tmp[j];
dis[j] += (1 - lt) * p[j][u] / 100.0 / tmp[j] * dis[u] + 1.0 / tmp[j];
}
}
cout << fixed << setprecision(15) << dis[1] << endl;
}
```
|
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
int n;
long double p[1005][1005], dis[1005], rest[1005];
bool vis[1005];
void work() {
double x = 1e18;
int id;
for (register int i = (1); i <= (n); ++i)
if (!vis[i] && (dis[i] + rest[i]) / (1 - rest[i]) <= x)
x = (dis[i] + rest[i]) / (1 - rest[i]), id = i;
vis[id] = 1;
dis[id] = (dis[id] + rest[id]) / (1 - rest[id]);
if (id == 1) {
printf("%.15lf\n", (double)dis[1]);
exit(0);
}
for (register int i = (1); i <= (n); ++i)
if (!vis[i])
dis[i] += rest[i] * p[i][id] * (dis[id] + 1), rest[i] *= (1 - p[i][id]);
}
signed main() {
n = read();
for (register int i = (1); i <= (n); ++i)
for (register int j = (1); j <= (n); ++j) p[i][j] = 0.01 * read();
for (register int i = (1); i <= (n - 1); ++i) rest[i] = 1;
for (register int i = (1); i <= (n); ++i) work();
return 0;
}
|
### Prompt
Please provide a cpp coded solution to the problem described below:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int x = 0;
bool f = 0;
for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
if (f) x = -x;
return x;
}
int n;
long double p[1005][1005], dis[1005], rest[1005];
bool vis[1005];
void work() {
double x = 1e18;
int id;
for (register int i = (1); i <= (n); ++i)
if (!vis[i] && (dis[i] + rest[i]) / (1 - rest[i]) <= x)
x = (dis[i] + rest[i]) / (1 - rest[i]), id = i;
vis[id] = 1;
dis[id] = (dis[id] + rest[id]) / (1 - rest[id]);
if (id == 1) {
printf("%.15lf\n", (double)dis[1]);
exit(0);
}
for (register int i = (1); i <= (n); ++i)
if (!vis[i])
dis[i] += rest[i] * p[i][id] * (dis[id] + 1), rest[i] *= (1 - p[i][id]);
}
signed main() {
n = read();
for (register int i = (1); i <= (n); ++i)
for (register int j = (1); j <= (n); ++j) p[i][j] = 0.01 * read();
for (register int i = (1); i <= (n - 1); ++i) rest[i] = 1;
for (register int i = (1); i <= (n); ++i) work();
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
int N;
double P[1010][1010] = {{0}};
double dist[1010] = {0};
double mult[1010] = {0};
int Hash[1010] = {0};
int main() {
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) scanf("%lf", &P[i][j]), P[i][j] /= 100;
mult[i] = 1;
}
mult[N] = 0;
for (int T = 1; T <= N; T++) {
int u = 0;
for (int i = 1; i <= N; i++) {
if (Hash[i] == 1) continue;
if (mult[i] < 1 && (u == 0 || ((dist[u] + 1) / (1 - mult[u]) >
(dist[i] + 1) / (1 - mult[i]))))
u = i;
}
Hash[u] = 1;
if (u != N) dist[u] = (dist[u] + 1) / (1 - mult[u]);
if (u == 1) break;
for (int i = 1; i <= N; i++) {
if (Hash[i] == 1) continue;
dist[i] += mult[i] * P[i][u] * dist[u];
mult[i] *= 1 - P[i][u];
}
}
printf("%.15lf\n", dist[1]);
return 0;
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
int N;
double P[1010][1010] = {{0}};
double dist[1010] = {0};
double mult[1010] = {0};
int Hash[1010] = {0};
int main() {
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) scanf("%lf", &P[i][j]), P[i][j] /= 100;
mult[i] = 1;
}
mult[N] = 0;
for (int T = 1; T <= N; T++) {
int u = 0;
for (int i = 1; i <= N; i++) {
if (Hash[i] == 1) continue;
if (mult[i] < 1 && (u == 0 || ((dist[u] + 1) / (1 - mult[u]) >
(dist[i] + 1) / (1 - mult[i]))))
u = i;
}
Hash[u] = 1;
if (u != N) dist[u] = (dist[u] + 1) / (1 - mult[u]);
if (u == 1) break;
for (int i = 1; i <= N; i++) {
if (Hash[i] == 1) continue;
dist[i] += mult[i] * P[i][u] * dist[u];
mult[i] *= 1 - P[i][u];
}
}
printf("%.15lf\n", dist[1]);
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
double a[1001][1001], p[1001], t[1001], w;
int n, s, v[1001];
inline double f(int x) {
if (t[x] == 1) return 100000000;
return (p[x] + 1) / (1 - t[x]);
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); i++)
for (int j = (1); j <= (n); j++) {
scanf("%lf", &a[i][j]);
a[i][j] /= 100;
}
for (int i = (1); i <= (n - 1); i++) t[i] = 1;
p[n] = -1;
for (int i = (1); i <= (n); i++) {
s = 0;
w = 100000000;
for (int j = (1); j <= (n); j++)
if (!v[j] && f(j) < w) w = f(j), s = j;
if (!s) break;
v[s] = 1;
for (int j = (1); j <= (n); j++)
if (!v[j]) {
p[j] += w * t[j] * a[j][s];
t[j] *= (1 - a[j][s]);
}
}
printf("%.10lf\n", f(1));
}
|
### Prompt
Construct a CPP code solution to the problem outlined:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
double a[1001][1001], p[1001], t[1001], w;
int n, s, v[1001];
inline double f(int x) {
if (t[x] == 1) return 100000000;
return (p[x] + 1) / (1 - t[x]);
}
int main() {
scanf("%d", &n);
for (int i = (1); i <= (n); i++)
for (int j = (1); j <= (n); j++) {
scanf("%lf", &a[i][j]);
a[i][j] /= 100;
}
for (int i = (1); i <= (n - 1); i++) t[i] = 1;
p[n] = -1;
for (int i = (1); i <= (n); i++) {
s = 0;
w = 100000000;
for (int j = (1); j <= (n); j++)
if (!v[j] && f(j) < w) w = f(j), s = j;
if (!s) break;
v[s] = 1;
for (int j = (1); j <= (n); j++)
if (!v[j]) {
p[j] += w * t[j] * a[j][s];
t[j] *= (1 - a[j][s]);
}
}
printf("%.10lf\n", f(1));
}
```
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 77;
int n, m;
bool M[N];
double A[N], P[N], Q[N], Pr[N][N];
int main() {
for (int i = 0; i < N; i++) {
Q[i] = 1;
A[i] = 1e9;
}
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int ts;
scanf("%d", &ts);
Pr[i][j] = ts;
Pr[i][j] /= 100;
}
}
A[n] = 0;
for (int tim = 1; tim <= n; tim++) {
int v = 0;
double t = 1e9;
for (int i = 1; i <= n; i++) {
if (M[i]) {
continue;
}
if (v == 0) {
v = i;
t = A[i];
continue;
}
if (t > A[i]) {
t = A[i];
v = i;
}
}
M[v] = 1;
for (int u = 1; u <= n; u++) {
if (M[u]) {
continue;
}
P[u] += Q[u] * Pr[u][v] * A[v];
Q[u] *= (1 - Pr[u][v]);
A[u] = (P[u] + 1) / (1 - Q[u]);
}
}
cout << fixed << setprecision(10) << A[1];
return 0;
}
|
### Prompt
Please create a solution in CPP to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 77;
int n, m;
bool M[N];
double A[N], P[N], Q[N], Pr[N][N];
int main() {
for (int i = 0; i < N; i++) {
Q[i] = 1;
A[i] = 1e9;
}
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int ts;
scanf("%d", &ts);
Pr[i][j] = ts;
Pr[i][j] /= 100;
}
}
A[n] = 0;
for (int tim = 1; tim <= n; tim++) {
int v = 0;
double t = 1e9;
for (int i = 1; i <= n; i++) {
if (M[i]) {
continue;
}
if (v == 0) {
v = i;
t = A[i];
continue;
}
if (t > A[i]) {
t = A[i];
v = i;
}
}
M[v] = 1;
for (int u = 1; u <= n; u++) {
if (M[u]) {
continue;
}
P[u] += Q[u] * Pr[u][v] * A[v];
Q[u] *= (1 - Pr[u][v]);
A[u] = (P[u] + 1) / (1 - Q[u]);
}
}
cout << fixed << setprecision(10) << A[1];
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
const int N = 1010;
int n, p[N][N], vis[N];
long double E[N], prob[N], coef[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) E[i] = 1e30, prob[i] = 1, coef[i] = 0;
E[n - 1] = 0;
for (int k = 0; k < n; k++) {
long double mv = 1e30;
int ps = -1;
for (int i = 0; i < n; i++)
if (!vis[i] && E[i] < mv) mv = E[i], ps = i;
vis[ps] = 1;
if (ps == 0) {
printf("%.15f\n", (double)E[0]);
return 0;
}
for (int i = 0; i < n; i++)
if (!vis[i]) {
coef[i] += E[ps] * prob[i] * 0.01 * p[i][ps];
prob[i] *= (1 - 0.01 * p[i][ps]);
if (prob[i] < 1 - 1e-6) E[i] = (1 + coef[i]) / (1 - prob[i]);
}
}
assert(0);
}
|
### Prompt
Please create a solution in cpp to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
const int N = 1010;
int n, p[N][N], vis[N];
long double E[N], prob[N], coef[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
for (int i = 0; i < n; i++) E[i] = 1e30, prob[i] = 1, coef[i] = 0;
E[n - 1] = 0;
for (int k = 0; k < n; k++) {
long double mv = 1e30;
int ps = -1;
for (int i = 0; i < n; i++)
if (!vis[i] && E[i] < mv) mv = E[i], ps = i;
vis[ps] = 1;
if (ps == 0) {
printf("%.15f\n", (double)E[0]);
return 0;
}
for (int i = 0; i < n; i++)
if (!vis[i]) {
coef[i] += E[ps] * prob[i] * 0.01 * p[i][ps];
prob[i] *= (1 - 0.01 * p[i][ps]);
if (prob[i] < 1 - 1e-6) E[i] = (1 + coef[i]) / (1 - prob[i]);
}
}
assert(0);
}
```
|
#include <bits/stdc++.h>
const int MN = 1005;
int N, p[MN][MN];
double dis[MN], val[MN];
void Dijk() {
static int can[MN], vis[MN];
for (int i = 1; i <= N; ++i) dis[i] = val[i] = 1, can[i] = vis[i] = 0;
dis[N] = val[N] = 0, can[N] = 1;
for (int k = 1; k <= N; ++k) {
int u = 0;
for (int i = 1; i <= N; ++i)
if (can[i] && !vis[i] &&
(!u || dis[i] / (1 - val[i]) < dis[u] / (1 - val[u])))
u = i;
vis[u] = 1;
double d = dis[u] / (1 - val[u]);
for (int v = 1; v <= N; ++v) {
if (vis[v]) continue;
if (p[v][u] == 100) continue;
double pr = p[v][u] / 100.;
if (!can[v])
dis[v] = 1 + (1 - pr) * d, val[v] = pr, can[v] = 1;
else
dis[v] += val[v] * (1 - pr) * d, val[v] *= pr;
}
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
scanf("%d", &p[i][j]);
p[i][j] = 100 - p[i][j];
}
}
Dijk();
printf("%.15lf\n", dis[1] / (1 - val[1]));
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
const int MN = 1005;
int N, p[MN][MN];
double dis[MN], val[MN];
void Dijk() {
static int can[MN], vis[MN];
for (int i = 1; i <= N; ++i) dis[i] = val[i] = 1, can[i] = vis[i] = 0;
dis[N] = val[N] = 0, can[N] = 1;
for (int k = 1; k <= N; ++k) {
int u = 0;
for (int i = 1; i <= N; ++i)
if (can[i] && !vis[i] &&
(!u || dis[i] / (1 - val[i]) < dis[u] / (1 - val[u])))
u = i;
vis[u] = 1;
double d = dis[u] / (1 - val[u]);
for (int v = 1; v <= N; ++v) {
if (vis[v]) continue;
if (p[v][u] == 100) continue;
double pr = p[v][u] / 100.;
if (!can[v])
dis[v] = 1 + (1 - pr) * d, val[v] = pr, can[v] = 1;
else
dis[v] += val[v] * (1 - pr) * d, val[v] *= pr;
}
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
scanf("%d", &p[i][j]);
p[i][j] = 100 - p[i][j];
}
}
Dijk();
printf("%.15lf\n", dis[1] / (1 - val[1]));
return 0;
}
```
|
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long BIG = 1446803456761533460LL;
const int Big = 336860180;
const long long int INF = LONG_LONG_MAX;
const vector<vector<long long int> > adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vector<vector<long long int> > adj8(
{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
bool isprime(long long int n) {
if (n == 2 || n == 3) return true;
if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 6; (i - 1) * (i - 1) <= n; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) {
return false;
}
}
return true;
}
vector<bool> sprime;
void genPrime(long long int sz) {
sprime = vector<bool>(sz, true);
sprime[0] = false;
sprime[1] = false;
for (long long int i = (2); i < (sz); ++i) {
if (sprime[i]) {
for (long long int j = i * i; j < sz; j += i) {
sprime[j] = false;
}
}
}
}
long long int powMod(long long int a, long long int b, long long int mod) {
long long int n = 1;
long long int p = a;
while (b > 0) {
if (b % 2 == 1) {
n *= p;
n %= mod;
}
p *= p;
p %= mod;
b /= 2;
}
return n;
}
long long int modularInverse(long long int a, long long int mod) {
return powMod(a, mod - 2, mod);
}
long long int binarysearch(long long int l, long long int r,
bool (*bsfunction)(long long int)) {
while (r - l > 1) {
long long int mid = (l + r) / 2;
bool val = bsfunction(mid);
if (val) {
r = mid;
} else {
l = mid;
}
}
return l;
}
stringstream sss;
const long long int maxn = 1010;
long long int p[maxn][maxn];
long double expp[maxn];
long double nxtprob[maxn];
long double alpha[maxn];
long long int deter[maxn];
void MAIN() {
long long int n;
cin >> n;
for (long long int i = 0; i < (n); ++i) {
for (long long int j = 0; j < (n); ++j) {
cin >> p[i][j];
}
}
long double inf = numeric_limits<long double>::max();
fill(expp, expp + n, inf);
fill(nxtprob, nxtprob + n, 1.0);
expp[n - 1] = 0;
long long int mn = n - 1;
long double val = inf;
for (long long int nn = 0; nn < (n - 1); ++nn) {
long long int x = mn;
val = inf;
deter[x] = 2;
for (long long int y = 0; y < (n); ++y) {
long double prob = p[y][x];
if (deter[y] == 0 && prob > 0 && expp[y] > expp[x]) {
if (prob == 100) {
alpha[y] += (1 + expp[x]) * nxtprob[y];
expp[y] = alpha[y];
deter[y] = 1;
} else {
alpha[y] += (1 + expp[x]) * nxtprob[y] * prob / 100;
nxtprob[y] *= (100 - prob) / 100;
expp[y] = (alpha[y] / nxtprob[y] + 1) / (1 / nxtprob[y] - 1);
}
}
if (deter[y] < 2 && expp[y] < val) {
mn = y;
val = expp[mn];
}
}
}
cout << (expp[0]) << endl;
}
const long long int TESTCASEN = 1;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
sss << R"(
2
100 0
40 100
)";
for (long long int i = 0; i < (TESTCASEN); ++i) {
MAIN();
}
return 0;
}
|
### Prompt
Your task is to create a cpp solution to the following problem:
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems.
The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j.
Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.
Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.
Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
Output
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Examples
Input
3
100 50 50
0 100 80
0 0 100
Output
1.750000000000000
Input
2
100 30
40 100
Output
3.333333333333333
Note
In the second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is <image>.
### Response
```cpp
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long BIG = 1446803456761533460LL;
const int Big = 336860180;
const long long int INF = LONG_LONG_MAX;
const vector<vector<long long int> > adj4({{0, 1}, {0, -1}, {1, 0}, {-1, 0}});
const vector<vector<long long int> > adj8(
{{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}});
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
bool isprime(long long int n) {
if (n == 2 || n == 3) return true;
if (n < 2 || n % 2 == 0 || n % 3 == 0) return false;
for (long long int i = 6; (i - 1) * (i - 1) <= n; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0) {
return false;
}
}
return true;
}
vector<bool> sprime;
void genPrime(long long int sz) {
sprime = vector<bool>(sz, true);
sprime[0] = false;
sprime[1] = false;
for (long long int i = (2); i < (sz); ++i) {
if (sprime[i]) {
for (long long int j = i * i; j < sz; j += i) {
sprime[j] = false;
}
}
}
}
long long int powMod(long long int a, long long int b, long long int mod) {
long long int n = 1;
long long int p = a;
while (b > 0) {
if (b % 2 == 1) {
n *= p;
n %= mod;
}
p *= p;
p %= mod;
b /= 2;
}
return n;
}
long long int modularInverse(long long int a, long long int mod) {
return powMod(a, mod - 2, mod);
}
long long int binarysearch(long long int l, long long int r,
bool (*bsfunction)(long long int)) {
while (r - l > 1) {
long long int mid = (l + r) / 2;
bool val = bsfunction(mid);
if (val) {
r = mid;
} else {
l = mid;
}
}
return l;
}
stringstream sss;
const long long int maxn = 1010;
long long int p[maxn][maxn];
long double expp[maxn];
long double nxtprob[maxn];
long double alpha[maxn];
long long int deter[maxn];
void MAIN() {
long long int n;
cin >> n;
for (long long int i = 0; i < (n); ++i) {
for (long long int j = 0; j < (n); ++j) {
cin >> p[i][j];
}
}
long double inf = numeric_limits<long double>::max();
fill(expp, expp + n, inf);
fill(nxtprob, nxtprob + n, 1.0);
expp[n - 1] = 0;
long long int mn = n - 1;
long double val = inf;
for (long long int nn = 0; nn < (n - 1); ++nn) {
long long int x = mn;
val = inf;
deter[x] = 2;
for (long long int y = 0; y < (n); ++y) {
long double prob = p[y][x];
if (deter[y] == 0 && prob > 0 && expp[y] > expp[x]) {
if (prob == 100) {
alpha[y] += (1 + expp[x]) * nxtprob[y];
expp[y] = alpha[y];
deter[y] = 1;
} else {
alpha[y] += (1 + expp[x]) * nxtprob[y] * prob / 100;
nxtprob[y] *= (100 - prob) / 100;
expp[y] = (alpha[y] / nxtprob[y] + 1) / (1 / nxtprob[y] - 1);
}
}
if (deter[y] < 2 && expp[y] < val) {
mn = y;
val = expp[mn];
}
}
}
cout << (expp[0]) << endl;
}
const long long int TESTCASEN = 1;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
sss << R"(
2
100 0
40 100
)";
for (long long int i = 0; i < (TESTCASEN); ++i) {
MAIN();
}
return 0;
}
```
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.