Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int arr[303][303];
int dp[606][303][303];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", arr[i] + j);
}
}
for (int i = 0; i < 2 * (n - 1) + 1; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++) dp[i][j][k] = -(1 << 22);
dp[0][0][0] = arr[0][0];
for (int dia = 0; dia < 2 * (n - 1); dia++) {
;
for (int x1 = 0; x1 < n; x1++) {
for (int x2 = 0; x2 < n; x2++) {
int y1, y2;
y1 = dia - x1;
y2 = dia - x2;
if (x1 + 1 < n && x2 + 1 < n) {
int dod = arr[x1 + 1][y1] + arr[x2 + 1][y2];
if (x1 + 1 == x2 + 1 && y1 == y2) dod -= arr[x2 + 1][y2];
dp[dia + 1][x1 + 1][x2 + 1] =
max(dp[dia + 1][x1 + 1][x2 + 1], dp[dia][x1][x2] + dod);
}
if (x1 + 1 < n && y2 + 1 < n) {
int dod = arr[x1 + 1][y1] + arr[x2][y2 + 1];
if (x1 + 1 == x2 && y1 == y2 + 1) dod -= arr[x2][y2 + 1];
dp[dia + 1][x1 + 1][x2] =
max(dp[dia + 1][x1 + 1][x2], dp[dia][x1][x2] + dod);
}
if (y1 + 1 < n && x2 + 1 < n) {
int dod = arr[x1][y1 + 1] + arr[x2 + 1][y2];
if (x1 == x2 + 1 && y1 + 1 == y2) dod -= arr[x2 + 1][y2];
dp[dia + 1][x1][x2 + 1] =
max(dp[dia + 1][x1][x2 + 1], dp[dia][x1][x2] + dod);
}
if (y1 + 1 < n && y2 + 1 < n) {
int dod = arr[x1][y1 + 1] + arr[x2][y2 + 1];
if (x1 == x2 && y1 + 1 == y2 + 1) dod -= arr[x2][y2 + 1];
dp[dia + 1][x1][x2] = max(dp[dia + 1][x1][x2], dp[dia][x1][x2] + dod);
}
}
}
}
printf("%d\n", dp[2 * (n - 1)][n - 1][n - 1]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 303, inf = 0x3f3f3f3f;
int a[N][N], dp[N][N][N + N], n;
int f(int x1, int y1, int x2, int y2) {
if (~dp[x1][x2][x1 + y1]) return dp[x1][x2][x1 + y1];
if (x1 == n && y1 == n) return a[n][n];
int t = a[x1][y1] + a[x2][y2] - (x1 == x2 && y1 == y2) * a[x1][y1],
res = -inf;
if (x1 < n && x2 < n) res = max(res, f(x1 + 1, y1, x2 + 1, y2));
if (x1 < n && y2 < n) res = max(res, f(x1 + 1, y1, x2, y2 + 1));
if (y1 < n && x2 < n) res = max(res, f(x1, y1 + 1, x2 + 1, y2));
if (y1 < n && y2 < n) res = max(res, f(x1, y1 + 1, x2, y2 + 1));
return dp[x1][x2][x1 + y1] = res + t;
}
int32_t main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
memset(dp, -1, sizeof dp);
cout << f(1, 1, 1, 1) << endl;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long N = 1e3 + 5;
const long long INF = 1e9 + 7;
long long n, a[N][N], b[2][N][N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
scanf("%lld", &n);
for (long long i = 0; i < n; i++)
for (long long j = 0; j < n; j++) scanf("%lld", &a[i][j]);
for (long long i = 0; i < 2; i++)
for (long long j = 0; j < n; j++)
for (long long k = 0; k < n; k++) b[i][j][k] = -INF;
b[1][0][0] = 0;
for (long long r = 0; r < n; r++) {
long long *d = a[r];
for (long long i = 0; i < n; i++)
for (long long j = i; j < n; j++) {
b[0][i][j] = b[1][i][j];
if (b[0][i][j] != -INF) b[0][i][j] += d[i];
b[1][i][j] = -INF;
}
for (long long i = 0; i < n; i++)
for (long long j = i; j < n; j++) {
if (b[0][i][j] == -INF) continue;
b[1][i][j] = max(b[1][i][j], b[0][i][j] + (i < j ? d[j] : 0));
if (i + 1 < n) {
long long ni = i + 1, nj = max(i + 1, j);
b[0][ni][nj] = max(b[0][ni][nj], b[0][i][j] + d[ni]);
}
}
for (long long i = 0; i < n; i++)
for (long long j = i; j < n; j++) {
if (b[1][i][j] == -INF) continue;
if (j + 1 < n)
b[1][i][j + 1] = max(b[1][i][j + 1], b[1][i][j] + d[j + 1]);
}
}
printf("%lld", b[1][n - 1][n - 1]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 305;
int n;
int mat[MAXN][MAXN];
int dp[MAXN * 2][MAXN][MAXN];
int get_len_of_row(int i) {
if (i <= n) return i;
return 2 * n - i;
}
int offset[4][2] = {{-1, 0}, {-1, -1}, {0, -1}, {0, 0}};
bool is_valid(int x, int i) {
int len = get_len_of_row(i);
if (x >= 1 && x <= len) return true;
return false;
}
int get_real_val(int row, int col) {
int ret = 0;
if (row <= n) {
ret = mat[row - col + 1][col];
} else {
ret = mat[n + 1 - col][row - n + 1 + col - 1];
}
return ret;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> mat[i][j];
}
}
dp[1][1][1] = mat[1][1];
for (int i = 2; i <= n * 2 - 1; i++) {
int len = get_len_of_row(i);
if (i > n) {
for (int j = 1; j <= len; j++) {
for (int k = j; k <= len; k++) {
int nMax = INT_MIN;
for (int l = 0; l < 4; l++) {
int a = j - offset[l][0], b = k - offset[l][1];
if (is_valid(a, i - 1) && is_valid(b, i - 1)) {
if (a > b) swap(a, b);
nMax = max(nMax, dp[i - 1][a][b] + get_real_val(i, j) +
get_real_val(i, k) * (j != k));
}
}
dp[i][j][k] = nMax;
}
}
continue;
}
for (int j = 1; j <= len; j++) {
for (int k = j; k <= len; k++) {
int nMax = INT_MIN;
for (int l = 0; l < 4; l++) {
int a = j + offset[l][0], b = k + offset[l][1];
if (a > b) swap(a, b);
if (is_valid(a, i - 1) && is_valid(b, i - 1)) {
nMax = max(nMax, dp[i - 1][a][b] + get_real_val(i, j) +
get_real_val(i, k) * (j != k));
}
}
dp[i][j][k] = nMax;
}
}
}
cout << dp[n * 2 - 1][1][1] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dp[305 << 1][305][305];
int a[305][305], n;
int way[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
memset(dp, 0x81, sizeof(dp));
dp[0][1][1] = a[1][1];
for (int i = 1; i <= 2 * n - 2; i++)
for (int j = 1; j <= i + 1 && j <= n; j++)
for (int k = j; k <= i + 1 && k <= n; k++) {
for (int r = 0; r < 4; r++)
dp[i][j][k] =
max(dp[i][j][k], dp[i - 1][j - way[r][0]][k - way[r][1]]);
dp[i][j][k] += a[j][i - j + 2] + a[k][i - k + 2] -
(j == k ? a[k][i - k + 2] : 0);
}
printf("%d\n", dp[2 * n - 2][n][n]);
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
const int maxint = 999999999;
int dp[2][maxn][maxn];
int a[maxn][maxn];
int n;
bool judge(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n); }
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
dp[1][1][1] = a[1][1];
for (int i = 2; i <= 2 * n - 1; i++) {
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) dp[i % 2][j][k] = -maxint;
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
int x1 = i - j + 1, y1 = j, x2 = i - k + 1, y2 = k;
if (judge(x1, y1) == false || judge(x2, y2) == false) continue;
int value;
if (j == k)
value = a[x1][y1];
else
value = a[x1][y1] + a[x2][y2];
if (j - 1 >= 1 && k - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k - 1] + value);
}
if (j - 1 >= 1 && x2 - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j - 1][k] + value);
}
if (x1 - 1 >= 1 && k - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k - 1] + value);
}
if (x1 - 1 >= 1 && x2 - 1 >= 1) {
dp[i % 2][j][k] =
max(dp[i % 2][j][k], dp[(i - 1) % 2][j][k] + value);
}
}
}
}
printf("%d\n", dp[(2 * n - 1) % 2][n][n]);
}
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int ddx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int ddy[] = {0, 1, 0, -1, -1, 1, -1, 1};
bool inSize(int c, int l, int r) {
if (c >= l && c <= r) return true;
return false;
}
template <class T>
inline void checkmin(T &a, T b) {
if (a == -1 || a > b) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (a < b) a = b;
}
int dx[] = {0, 0, -1, -1};
int dy[] = {0, -1, 0, -1};
const int N = 301;
int f[N << 1][N][N];
int a[N][N], n;
int main() {
scanf("%d", &(n));
for (int(i) = (1); (i) <= (n); (i)++)
for (int(j) = (1); (j) <= (n); (j)++) scanf("%d", &(a[i][j]));
memset(f, 0x81, sizeof(f));
f[0][1][1] = a[1][1];
for (int(k) = (1); (k) <= (2 * n - 2); (k)++)
for (int i = 1; i <= n && i <= k + 1; i++)
for (int j = i; j <= n && j <= k + 1; j++) {
for (int t = 0; t < 4; t++)
f[k][i][j] = max(f[k][i][j], f[k - 1][i + dx[t]][j + dy[t]]);
f[k][i][j] +=
a[i][k - i + 2] + a[j][k - j + 2] - (j == i ? a[i][k - i + 2] : 0);
}
printf("%d", (f[2 * n - 2][n][n]));
printf("\n");
;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.util.Scanner;
public class E {
static int end;
static int[][] vals;
static int[][][] dp;
static int[] dx = new int[] { 1, 0 };
static int[] dy = new int[] { 0, 1 };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
vals = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
vals[i][j] = sc.nextInt();
}
}
end = 2 * size - 1;
dp = new int[2][size][size];
int curR=0;
for(int moves=end;moves>=0;moves--){
for(int up1=0;up1<size;up1++){
for(int up2=0;up2<size;up2++){
if (moves == end){
continue;
}
if(up1>moves||up2>moves||moves-up1>=vals.length||moves-up2>=vals.length) continue;
int x1 = up1, y1 = moves - up1;
int x2 = up2, y2 = moves - up2;
int toadd = vals[x1][y1];
if (x1 != x2 || y1 != y2) {
toadd += vals[x2][y2];
}
int max = Integer.MIN_VALUE;
boolean fir = true;
for (int i = 0; i < 2; i++) {
int newX1 = x1 + dx[i];
int newY1 = y1 + dy[i];
if (newX1 < vals.length && newY1 < vals[0].length)
for (int j = 0; j < 2; j++) {
int newX2 = x2 + dx[j];
int newY2 = y2 + dy[j];
if (newX2 < vals.length && newY2 < vals[0].length) {
if (fir) {
fir = false;
max = dp[1-curR][newX1][newX2];
} else
max = Math.max(max, dp[1-curR][newX1][newX2]);
}
}
}
if (fir)
max = 0;
dp[curR][up1][up2] = max + toadd;
}
}
curR=1-curR;
}
System.out.println(dp[1-curR][0][0]);
sc.close();
}
}
| JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
template <class T>
bool read(T &x) {
char *s;
s = (char *)malloc(10);
if (sizeof(x) == 1)
strcpy(s + 1, " %c");
else if (sizeof(x) == 4)
strcpy(s + 1, "%d");
else if (sizeof(x) == 8)
strcpy(s + 1, "%lld");
int k = scanf(s + 1, &x);
free(s);
return k != -1;
}
using namespace std;
int N, A[305][305], dp[605][305][305];
int bul(int a, int x1, int x2) {
int &ret = dp[a][x1][x2];
if (ret != -99999999) return ret;
int y1 = a - x1 + 1;
int y2 = a - x2 + 1;
ret = -99999999;
if (x1 == N and y1 == N) return ret = A[N][N];
if (x1 > N or y1 > N or x2 > N or y2 > N) return ret;
ret = max(ret, bul(a + 1, x1, x2));
ret = max(ret, bul(a + 1, x1 + 1, x2));
ret = max(ret, bul(a + 1, x1, x2 + 1));
ret = max(ret, bul(a + 1, x1 + 1, x2 + 1));
ret += A[x1][y1] + A[x2][y2] * (x1 != x2);
return ret;
}
int main() {
read(N);
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) read(A[i][j]);
fill(dp[0][0], dp[604][304] + 304, -99999999);
if (N == 1) {
cout << A[1][1] << endl;
return 0;
}
cout << bul(1, 1, 1) << endl;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
const long double pi = acos(-1.0);
const long double inf = 1000 * 1000 * 1000;
#pragma comment(linker, "/STACK:36777216")
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long xabs(long long a) { return a > 0 ? a : -a; }
int dp[700][302][302];
int sm[4][2] = {{0, 0}, {0, -1}, {-1, 0}, {-1, -1}};
int main() {
for (int i = 0; i < 700; ++i)
for (int j = 0; j < 302; ++j)
for (int k = 0; k < 302; ++k) dp[i][j][k] = -inf;
int n;
cin >> n;
vector<vector<int> > g(n, vector<int>(n));
int m = n;
for (int i = 0; i < n; i++)
for (int k = 0; k < n; k++) cin >> g[i][k];
dp[0][0][0] = g[0][0];
for (int t = 1; t <= n + m - 2; t++) {
for (int x1 = 0; x1 < n; x1++) {
for (int x2 = 0; x2 < n; x2++) {
for (int i = 0; i < 4; i++) {
int nx1 = x1 + sm[i][0];
int nx2 = x2 + sm[i][1];
if (nx1 < n && nx2 < n && nx1 >= 0 && nx2 >= 0 && t - x1 >= 0 &&
t - x2 >= 0 && t - x1 < n && t - x2 < n) {
int tmp = dp[t - 1][nx1][nx2] + g[x1][t - x1] + g[x2][t - x2];
if (x1 == x2) tmp -= g[x1][t - x1];
dp[t][x1][x2] = max(dp[t][x1][x2], tmp);
}
}
}
}
}
int result = dp[n + m - 2][n - 1][n - 1];
cout << result << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 310;
const int inf = 1e6;
int mod = 1e9 + 7, a[MAX][MAX];
int dp[MAX][MAX][2 * MAX];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n + 2; i++)
for (int j = 0; j < n + 2; j++) a[i][j] = -inf;
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++) cin >> a[i][j];
for (int i = 0; i < n + 1; i++)
for (int j = 0; j < n + 1; j++)
for (int k = 0; k < 2 * n + 1; k++) dp[i][j][k] = -inf;
dp[1][1][1] = a[1][1];
for (int d = 2; d < 2 * n; d++) {
int ini = 1, fin = n;
if (d <= n)
fin = d;
else
ini = d - n + 1;
for (int i = ini; i < fin + 1; i++) {
for (int j = ini; j < fin + 1; j++) {
if (i > n || j > n || d + 1 - i > n || d + 1 - j > n) continue;
dp[i][j][d] = max(dp[i][j][d], dp[i - 1][j][d - 1]);
dp[i][j][d] = max(dp[i][j][d], dp[i][j - 1][d - 1]);
dp[i][j][d] = max(dp[i][j][d], dp[i][j][d - 1]);
dp[i][j][d] = max(dp[i][j][d], dp[i - 1][j - 1][d - 1]);
if (i == j)
dp[i][j][d] += a[i][d + 1 - i];
else
dp[i][j][d] += a[i][d + 1 - i] + a[j][d + 1 - j];
}
}
}
cout << dp[n][n][2 * n - 1];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.io.*;
import java.util.*;
public class cf214e {
public static void main(String[] args) {
FastIO in = new FastIO(), out = in;
int n = in.nextInt();
int[][] v = new int[n][n];
for(int i=0; i<n; i++) for(int j=0; j<n; j++) v[i][j] = in.nextInt();
int numDiag = 2*n-1;
int[][][] dp = new int[2][n][n];
int oo = 987654321;
for(int[][] x : dp) for(int[] y : x) Arrays.fill(y, -oo);
dp[0][0][0] = v[0][0];
for(int row = 0; row < numDiag-1; row++) {
int diagWidth = Math.min(row+1, numDiag - row);
int nRow = row+1;
int nWidth = Math.min(nRow+1, numDiag - nRow);
for(int[] x : dp[nRow%2]) Arrays.fill(x,-oo);
int lod = 0, hid = 1;
if(row >= n-1) {
lod = -1;
hid = 0;
}
for(int i=0; i<diagWidth; i++)
for(int j=0; j<diagWidth; j++)
for(int d1 = lod; d1 <= hid; d1++)
for(int d2 = lod; d2 <= hid; d2++) {
int ni = i+d1;
int nj = j+d2;
if(ni < 0 || nj < 0 || ni >= nWidth || nj >= nWidth) continue;
int nScore = dp[row%2][i][j];
int x = Math.min(nRow,n-1) - ni;
int y = Math.max(0, nRow-n+1) + ni;
nScore += v[x][y];
if(ni != nj) {
x = Math.min(nRow,n-1) - nj;
y = Math.max(0, nRow-n+1) + nj;
nScore += v[x][y];
}
dp[nRow%2][ni][nj] = Math.max(dp[nRow%2][ni][nj],nScore);
}
}
out.println(dp[(numDiag-1)%2][0][0]);
out.close();
}
/*
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 9
1 2 3 4 5 6 7 8 8 8
1 2 3 4 5 6 7 7 7 7
1 2 3 4 5 6 6 6 6 6
1 2 3 4 5 5 5 5 5 5
1 2 3 4 4 4 4 4 4 4
1 2 3 3 3 3 3 3 3 3
1 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1
*/
static class FastIO extends PrintWriter {
BufferedReader br;
StringTokenizer st;
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream in, OutputStream out) {
super(new BufferedWriter(new OutputStreamWriter(out)));
br = new BufferedReader(new InputStreamReader(in));
scanLine();
}
public void scanLine() {
try {
st = new StringTokenizer(br.readLine().trim());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public int numTokens() {
if (!st.hasMoreTokens()) {
scanLine();
return numTokens();
}
return st.countTokens();
}
public String next() {
if (!st.hasMoreTokens()) {
scanLine();
return next();
}
return st.nextToken();
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename G1, typename G2 = G1, typename G3 = G1>
struct triple {
G1 first;
G2 second;
G3 T;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> v(n, vector<int>(n + 1));
for (int i = 0; i < n; i++)
for (int j = 1; j <= n; j++) cin >> v[i][j], v[i][j] += v[i][j - 1];
const int oo = -1e9;
vector<vector<int>> cur(n, vector<int>(n, oo));
vector<vector<int>> next(n, vector<int>(n, oo));
vector<vector<int>> temp1(n, vector<int>(n, oo));
vector<vector<int>> temp2(n, vector<int>(n, oo));
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++) cur[i][j] = v[0][j + 1];
for (int k = 1; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cur[i][j] -= v[k][i];
temp1[i][j] = cur[i][j];
if (i > 0) temp1[i][j] = max(temp1[i][j], temp1[i - 1][j]);
if (j > 0) temp1[i][j] = max(temp1[i][j], temp1[i][j - 1]);
}
for (int j = i; j < n; j++) next[i][j] = temp1[i][i] + v[k][j + 1];
for (int j = 0; j < n; j++) {
cur[i][j] -= v[k][j];
temp2[i][j] = cur[i][j];
if (i > 0) temp2[i][j] = max(temp2[i][j], temp2[i - 1][j]);
}
int mx = oo;
for (int j = i + 1; j < n; j++)
mx = max(mx, temp2[i][j]),
next[i][j] = max(next[i][j], mx + v[k][i + 1] + v[k][j + 1]);
}
swap(cur, next);
}
cout << cur[n - 1][n - 1] << '\n';
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= 310; i++)
for (int j = 0; j <= 310; j++) {
a[i][j] = -inf;
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n && i < k; i++) {
for (int j = 1; j <= n && j < k; j++) {
int x = k % 2, y = 1 - x;
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
if (i != j)
f[x][i][j] += a[i][k - i] + a[j][k - j];
else
f[x][i][j] += a[i][k - i];
}
}
}
cout << f[0][n][n];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author George Marcus
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
}
class TaskE {
private static final int INF = 2000000000;
public void solve(int testNumber, Scanner in, PrintWriter out) {
int N = in.nextInt();
int[][] A = new int[N + 1][N + 1];
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
A[i][j] = in.nextInt();
if(N == 1) {
out.print(A[1][1]);
return;
}
int[][][] dp = new int[2][N + 1][N + 1];
for(int i = 0; i < 2; i++)
for(int j = 0; j < N + 1; j++)
for(int k = 0; k < N + 1; k++)
dp[i][j][k] = -INF;
dp[1][1][1] = A[1][1];
int line = 1;
for(int d = 1; d < 2 * N - 1; d++) {
line = d & 1;
for(int i1 = 1; i1 <= N && i1 <= d; i1++)
for(int i2 = 1; i2 <= i1; i2++) {
int j1 = d + 1 - i1;
int j2 = d + 1 - i2;
if(j1 < 1 || j1 > N || j2 < 1 || j2 > N)
continue;
for(int di1 = 0; di1 <= 1; di1++)
for(int di2 = 0; di2 <= 1; di2++) {
int ni1 = i1 + di1;
int nj1 = d + 2 - ni1;
int ni2 = i2 + di2;
int nj2 = d + 2 - ni2;
if(ni1 > N || ni2 > N)
continue;
if(nj1 < 1 || nj1 > N || nj2 < 1 || nj2 > N)
continue;
if(ni1 != ni2 || nj1 != nj2)
dp[1 - line][ni1][ni2] = Math.max(dp[1 - line][ni1][ni2], dp[line][i1][i2] + A[ni1][nj1] + A[ni2][nj2]);
else
dp[1 - line][ni1][ni2] = Math.max(dp[1 - line][ni1][ni2], dp[line][i1][i2] + A[ni1][nj1]);
}
}
for(int j = 0; j < N + 1; j++)
for(int k = 0; k < N + 1; k++)
dp[line][j][k] = -INF;
}
line = 1 - line;
out.print(dp[line][N][N]);
}
}
| JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int dp[604][302][302], n, arr[302][302];
bool check(int i, int j) {
if (i > n || i < 1) return false;
if (j > n || j < 1) return false;
return true;
}
int solve() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> arr[i][j];
for (int i = 0; i <= 2 * n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++) dp[i][j][k] = -INF;
dp[2][1][1] = arr[1][1];
for (int i = 3; i <= 2 * n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) {
int j2 = i - j;
int k2 = i - k;
if (!check(j2, k2)) continue;
int ans = dp[i][j][k];
ans = max(ans, dp[i - 1][j][k]);
ans = max(ans, dp[i - 1][j][k - 1]);
ans = max(ans, dp[i - 1][j - 1][k]);
ans = max(ans, dp[i - 1][j - 1][k - 1]);
if (j == k)
ans += arr[j][j2];
else
ans += (arr[j][j2] + arr[k][k2]);
dp[i][j][k] = ans;
}
return dp[2 * n][n][n];
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << solve();
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
long long a[610][610], f[2][610][610];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i <= 600; i++)
for (int j = 0; j <= 600; j++) {
a[i][j] = -inf;
f[0][i][j] = -inf;
f[1][i][j] = -inf;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
f[0][1][1] = a[1][1];
for (int k = 3; k <= n + n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i >= k || j >= k) continue;
int x = k % 2, y = 1 - x;
long long w = -inf;
for (int dx = -1; dx <= 0; dx++)
for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]);
f[x][i][j] = w;
if (i != j)
f[x][i][j] += a[i][k - i] + a[j][k - j];
else
f[x][i][j] += a[i][k - i];
}
}
}
cout << f[0][n][n];
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int a[300][300];
int d[2 * 300 - 1][300][300];
int main() {
unsigned int n;
cin >> n;
for (unsigned int i = 0; i < n; ++i) {
for (unsigned int j = 0; j < n; ++j) {
cin >> a[i][j];
}
}
d[0][0][0] = a[0][0];
for (unsigned int s = 1; s <= 2 * n - 2; ++s) {
unsigned int l = s < n ? 0 : s - n + 1;
unsigned int r = s < n ? s + 1 : n;
for (unsigned int i1 = l; i1 < r; ++i1) {
for (unsigned int i2 = i1; i2 < r; ++i2) {
d[s][i1][i2] = numeric_limits<int>::min();
unsigned int j1 = s - i1;
unsigned int j2 = s - i2;
if (i1 == i2) {
if (i1 > 0) {
d[s][i1][i2] = max(d[s][i1][i2], d[s - 1][i1 - 1][i1 - 1]);
}
if (j1 > 0) {
d[s][i1][i2] = max(d[s][i1][i2], d[s - 1][i1][i1]);
}
if (i1 > 0 && j1 > 0) {
d[s][i1][i2] = max(d[s][i1][i2], d[s - 1][i1 - 1][i1]);
}
d[s][i1][i2] += a[i1][j1];
} else {
if (i1 > 0 && i2 > 0) {
d[s][i1][i2] = max(d[s][i1][i2], d[s - 1][i1 - 1][i2 - 1]);
}
if (i1 > 0 && j2 > 0) {
d[s][i1][i2] = max(d[s][i1][i2], d[s - 1][i1 - 1][i2]);
}
if (j1 > 0 && i2 > 0) {
d[s][i1][i2] = max(d[s][i1][i2], d[s - 1][i1][i2 - 1]);
}
if (j1 > 0 && j2 > 0) {
d[s][i1][i2] = max(d[s][i1][i2], d[s - 1][i1][i2]);
}
d[s][i1][i2] += a[i1][j1] + a[i2][j2];
}
}
}
}
cout << d[2 * n - 2][n - 1][n - 1];
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
typedef char* cstr;
const int oo = (~0u) >> 1;
const long long int ool = (~0ull) >> 1;
const double inf = 1e100;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <typename type>
inline void cmax(type& a, const type& b) {
if (a < b) a = b;
}
template <typename type>
inline void cmin(type& a, const type& b) {
if (a > b) a = b;
}
int n;
int c[1000];
int k[1000];
int a[300][300];
int f[600][300][300];
int main() {
cin >> n;
for (int i = 0; i < (n); ++i)
for (int j = 0; j < (n); ++j) cin >> a[i][j];
for (int i = 0; i < (n * 2 - 1); ++i)
for (int x1 = 0; x1 < (min(i + 1, n)); ++x1)
for (int x2 = 0; x2 < (min(i + 1, n)); ++x2) {
int y1 = i - x1;
int y2 = i - x2;
f[i][x1][x2] = a[x1][y1] + (x1 == x2 ? 0 : a[x2][y2]);
if (i) {
int d = -oo;
if (x1 < i && x2 < i) cmax(d, f[i - 1][x1][x2]);
if (x1 && x2) cmax(d, f[i - 1][x1 - 1][x2 - 1]);
if (x1 && x2 < i) cmax(d, f[i - 1][x1 - 1][x2]);
if (x2 && x1 < i) cmax(d, f[i - 1][x1][x2 - 1]);
f[i][x1][x2] += d;
}
}
cout << f[(n - 1) * 2][n - 1][n - 1] << endl;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int i, j, l, n, a[301][301], f[601][301][301], ii, jj, ans = -1000000000;
int add(int n, int i, int j) {
if (i == j)
return a[n - i][i];
else
return a[n - i][i] + a[n - j][j];
}
int main() {
cin >> n;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) cin >> a[i][j];
for (l = 0; l <= 600; l++)
for (i = 0; i <= 300; i++)
for (j = 0; j <= 300; j++) f[l][i][j] = -1000000000;
f[0][0][0] = a[0][0];
for (l = 0; l < 2 * n - 2; l++) {
int l1 = 0, l2 = l;
if (n <= l) l1 = l - n + 1;
if (n <= l) l2 = n - 1;
for (i = l1; i <= l2; i++)
for (j = l1; j <= l2; j++)
for (ii = 0; ii < 2; ii++)
for (jj = 0; jj < 2; jj++)
f[l + 1][i + ii][j + jj] =
max(f[l + 1][i + ii][j + jj],
f[l][i][j] + add(l + 1, i + ii, j + jj));
}
cout << f[2 * n - 2][n - 1][n - 1] << endl;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 333;
int dp[2][N][N];
int max(int a, int b, int c) { return max(max(a, b), c); }
int max(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); }
int a[N][N];
int DP(int h, int i, int j) {
if (i <= 0 || h - i <= 0 || j <= 0 || h - j <= 0)
return -999999999;
else
return dp[h & 1][i][j];
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) cin >> a[i][j];
dp[2 & 1][1][1] = a[1][1];
for (int h = 3; h <= n + n; ++h)
for (int i = 1; i <= n; ++i)
for (int j = i; j <= n; ++j) {
if (i == j) {
dp[h & 1][i][j] = max(DP(h - 1, i, i), DP(h - 1, i - 1, i),
DP(h - 1, i - 1, i - 1)) +
a[i][h - i];
} else {
dp[h & 1][i][j] = max(DP(h - 1, i, j), DP(h - 1, i - 1, j - 1),
DP(h - 1, i, j - 1), DP(h - 1, i - 1, j)) +
a[i][h - i] + a[j][h - j];
}
}
cout << dp[(n + n) & 1][n][n] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class round131E {
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
static StringTokenizer st = new StringTokenizer("");
static int nextInt() throws Exception {
return Integer.parseInt(next());
}
static String next() throws Exception {
while (true) {
if (st.hasMoreTokens()) {
return st.nextToken();
}
String s = br.readLine();
if (s == null) {
return null;
}
st = new StringTokenizer(s);
}
}
static int graph[][];
static int[][][] dp;
static int n;
public static int runDP(int i1, int i2, int diagonal) {
int j1 = diagonal - i1;
int j2 = diagonal - i2;
if (i1 >= n || j1 >= n || i2 >= n || j2 >= n)
return -(int) 1e9;
if (i1 == n - 1 && j1 == n - 1 && i2 == n - 1 && j2 == n - 1)
return graph[n - 1][n - 1];
if (dp[i1][i2][diagonal] != -1)
return dp[i1][i2][diagonal];
int ans = -(int) 1e9;
if (i1 == i2 && j1 == j2) {
ans = Math.max(ans,
graph[i1][j1] + runDP(i1 + 1, i2 + 1, diagonal + 1));
ans = Math.max(ans, graph[i1][j1] + runDP(i1, i2, diagonal + 1));
ans = Math
.max(ans, graph[i1][j1] + runDP(i1 + 1, i2, diagonal + 1));
ans = Math
.max(ans, graph[i1][j1] + runDP(i1, i2 + 1, diagonal + 1));
} else {
ans = Math.max(
ans,
graph[i1][j1] + graph[i2][j2]
+ runDP(i1 + 1, i2 + 1, diagonal + 1));
ans = Math
.max(ans,
graph[i1][j1] + graph[i2][j2]
+ runDP(i1, i2, diagonal + 1));
ans = Math.max(
ans,
graph[i1][j1] + graph[i2][j2]
+ runDP(i1 + 1, i2, diagonal + 1));
ans = Math.max(
ans,
graph[i1][j1] + graph[i2][j2]
+ runDP(i1, i2 + 1, diagonal + 1));
}
return dp[i1][i2][diagonal] = ans;
}
public static void main(String[] args) throws Exception {
n = nextInt();
graph = new int[n][n];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
graph[i][j] = nextInt();
dp = new int[n + 1][n + 1][2];
for (int diagonal = 2 * (n - 1); diagonal >= 0; --diagonal) {
for (int i1 = n - 1; i1 >= 0; --i1) {
for (int i2 = n - 1; i2 >= 0; --i2) {
int j1 = diagonal - i1;
int j2 = diagonal - i2;
if(j1 < 0 || j2 < 0 || j1 >= n || j2 >= n){
dp[i1][i2][diagonal % 2] = -(int)1e9;
continue;
}
if (i1 == n - 1 && j1 == n - 1 && i2 == n - 1
&& j2 == n - 1)
dp[i1][i2][diagonal % 2] = graph[n - 1][n - 1];
else {
if (diagonal == 2 * (n - 1))
dp[i1][i2][diagonal % 2] = -(int)1e9;
else {
int ans = -(int) 1e9;
if (i1 == i2 && j1 == j2) {
if (i1 + 1 < n && i2 + 1 < n)
ans = Math.max(ans, graph[i1][j1]
+ dp[i1 + 1][i2 + 1][(diagonal + 1) % 2]);
ans = Math.max(ans, graph[i1][j1]
+ dp[i1][i2][(diagonal + 1) % 2]);
if (i1 + 1 < n)
ans = Math.max(ans, graph[i1][j1]
+ dp[i1 + 1][i2][(diagonal + 1) % 2]);
if (i2 + 1 < n)
ans = Math.max(ans, graph[i1][j1]
+ dp[i1][i2 + 1][(diagonal + 1) % 2]);
} else {
if (i1 + 1 < n && i2 + 1 < n)
ans = Math.max(ans, graph[i1][j1]
+ graph[i2][j2]
+ dp[i1 + 1][i2 + 1][(diagonal + 1) % 2]);
ans = Math.max(ans, graph[i1][j1]
+ graph[i2][j2]
+ dp[i1][i2][(diagonal + 1) % 2]);
if (i1 + 1 < n)
ans = Math.max(ans, graph[i1][j1]
+ graph[i2][j2]
+ dp[i1 + 1][i2][(diagonal + 1) % 2]);
if (i2 + 1 < n)
ans = Math.max(ans, graph[i1][j1]
+ graph[i2][j2]
+ dp[i1][i2 + 1][(diagonal + 1) % 2]);
}
dp[i1][i2][diagonal % 2] = ans;
}
}
}
}
}
System.out.println(dp[0][0][0]);
}
}
| JAVA |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60000000")
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-12;
const int INF = (1 << 30) - 1;
const long long LLINF = ((long long)1 << 62) - 1;
const int maxn = 310;
int di[] = {0, 1};
int dj[] = {1, 0};
int a[maxn][maxn];
int n, m;
int f[maxn * 2][maxn][maxn];
inline bool inside(int i, int j) {
return (i >= 0 && j >= 0 && i < n && j < n);
}
int main() {
cin >> n;
m = n;
for (int i = 0; i < (int)n; i++) {
for (int j = 0; j < (int)n; j++) {
scanf("%d", &a[i][j]);
for (int l = 0; l < (int)n + m + 10; l++) {
f[l][i][j] = -1000 * 1000 * 1000;
}
}
}
f[0][0][0] = a[0][0];
for (int d = 0; d < (int)n + m; d++) {
for (int ii1 = 0; ii1 < (int)n + m; ii1++) {
for (int ii2 = 0; ii2 < (int)n + m; ii2++) {
int i1, j1, i2, j2;
i1 = ii1;
j1 = d - ii1;
i2 = ii2;
j2 = d - ii2;
if (inside(i1, j1) && inside(i2, j2)) {
for (int k1 = 0; k1 < (int)2; k1++) {
for (int k2 = 0; k2 < (int)2; k2++) {
int I1, J1, I2, J2;
I1 = i1 + di[k1];
J1 = j1 + dj[k1];
I2 = i2 + di[k2];
J2 = j2 + dj[k2];
if (i1 == I1 && I2 == i1 && j1 == J2 && j2 == J2) continue;
if (!inside(I1, J1) || !inside(I2, J2)) continue;
if (make_pair(I1, J1) != make_pair(I2, J2)) {
f[d + 1][I1][I2] =
max(f[d + 1][I1][I2], f[d][i1][i2] + a[I1][J1] + a[I2][J2]);
} else {
f[d + 1][I1][I2] =
max(f[d + 1][I1][I2], f[d][i1][i2] + a[I1][J1]);
}
}
}
}
}
}
}
cout << f[n + n - 2][n - 1][n - 1];
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int M, N, a[333][333], dp[611][311][311];
int dx[2] = {1, 0};
int dy[2] = {0, 1};
void fresh(int &x, int v) {
if (x < v) x = v;
}
int main() {
int i, j, k;
while (scanf("%d", &N) == 1) {
M = N;
for (i = 0; i < 611; i++)
for (j = 0; j < 311; j++)
for (k = 0; k < 311; k++) dp[i][j][k] = -1000000000;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++) scanf("%d", &a[i][j]);
dp[0][0][0] = a[0][0];
for (i = 0; i < M + N - 2; i++) {
for (j = 0; j < M && j <= i; j++) {
if (i - j >= N) continue;
for (k = 0; k < M && k <= i; k++) {
if (i - k >= N) continue;
for (int jj = 0; jj < 2; jj++)
for (int kk = 0; kk < 2; kk++) {
if (i - j - jj >= N || j + jj >= M) continue;
if (i - k - kk >= N || k + kk >= M) continue;
if (j + jj == k + kk)
fresh(dp[i + 1][j + jj][k + kk],
dp[i][j][k] + a[j + jj][i + 1 - j - jj]);
else
fresh(dp[i + 1][j + jj][k + kk], dp[i][j][k] +
a[j + jj][i + 1 - j - jj] +
a[k + kk][i + 1 - k - kk]);
}
}
}
}
printf("%d\n", dp[M + N - 2][M - 1][M - 1]);
}
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int f[305][305][305];
int p[305][305];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &p[i][j]);
f[0][0][0] = p[0][0];
for (int x1 = 0; x1 < n; x1++)
for (int y1 = 0; y1 < n; y1++)
if (x1 + y1 != 0)
for (int x2 = 0; x2 < n; x2++) {
f[x1][y1][x2] = -1 << 30;
int y2 = x1 + y1 - x2;
int add;
if (x1 == x2 && y1 == y2)
add = p[x1][y1];
else
add = p[x1][y1] + p[x2][y2];
if (x1 - 1 >= 0 && x2 - 1 >= 0) {
f[x1][y1][x2] = max(f[x1][y1][x2], f[x1 - 1][y1][x2 - 1] + add);
}
if (x1 - 1 >= 0 && y2 - 1 >= 0) {
f[x1][y1][x2] = max(f[x1][y1][x2], f[x1 - 1][y1][x2] + add);
}
if (y1 - 1 >= 0 && x2 - 1 >= 0) {
f[x1][y1][x2] = max(f[x1][y1][x2], f[x1][y1 - 1][x2 - 1] + add);
}
if (y1 - 1 >= 0 && y2 - 1 >= 0) {
f[x1][y1][x2] = max(f[x1][y1][x2], f[x1][y1 - 1][x2] + add);
}
}
printf("%d\n", f[n - 1][n - 1][n - 1]);
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
const int N = 310;
const int inf = 0x3f3f3f3f;
using namespace std;
int a[N][N], dp[3][N][N], n;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &a[i][j]);
for (int k = 0; k <= 2; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dp[k][i][j] = -inf;
dp[0][0][0] = a[0][0];
for (int k = 0; k < 2 * n - 2; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (dp[k % 2][i][j] == -inf) continue;
if (k - j < n - 1) {
int t = a[i][k + 1 - i];
if (i != j) t += a[j][k + 1 - j];
dp[(k + 1) % 2][i][j] =
max(dp[(k + 1) % 2][i][j], dp[k % 2][i][j] + t);
}
if (i < n - 1) {
int t = a[i + 1][k - i];
if (i != j) t += a[j + 1][k - j];
dp[(k + 1) % 2][i + 1][j + 1] =
max(dp[(k + 1) % 2][i + 1][j + 1], dp[k % 2][i][j] + t);
}
if (k - j < n - 1 && i < n - 1) {
int t = a[i + 1][k - i];
t += a[j][k + 1 - j];
dp[(k + 1) % 2][i + 1][j] =
max(dp[(k + 1) % 2][i + 1][j], dp[k % 2][i][j] + t);
}
if (k - i < n - 1 && j < n - 1 && j + 1 <= i) {
int t = a[i][k + 1 - i];
if (j + 1 != i) t += a[j + 1][k - j];
dp[(k + 1) % 2][i][j + 1] =
max(dp[(k + 1) % 2][i][j + 1], dp[k % 2][i][j] + t);
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dp[k % 2][i][j] = -inf;
}
printf("%d\n", dp[(n + n - 2) % 2][n - 1][n - 1]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int a[350][350];
int dp[305][305][305 * 2 + 5];
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= 2 * n; k++) {
dp[i][j][k] = -0x3f3f3f3f;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
}
}
dp[1][1][0] = a[1][1];
for (int i = 1; i <= n; i++) {
for (int x = 1; x <= n; x++) {
for (int step = 1; step <= 2 * n - 1; step++) {
int j = 2 + step - i;
int y = 2 + step - x;
if (i == 1 && j == 1) continue;
if (x == 1 && y == 1) continue;
if (j >= 1 && j <= n && y >= 1 && y <= n) {
dp[i][x][step] = max(
dp[i][x][step], dp[i - 1][x - 1][step - 1] + a[i][j] + a[x][y]);
dp[i][x][step] =
max(dp[i][x][step], dp[i - 1][x][step - 1] + a[i][j] + a[x][y]);
dp[i][x][step] =
max(dp[i][x][step], dp[i][x - 1][step - 1] + a[i][j] + a[x][y]);
dp[i][x][step] =
max(dp[i][x][step], dp[i][x][step - 1] + a[i][j] + a[x][y]);
if (i == x && j == y) dp[i][x][step] -= a[x][y];
}
}
}
}
printf("%d\n", dp[n][n][2 * n - 2]);
}
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class stl>
void DBGSTL(stl a) {
for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++) {
cerr << *i << " ";
}
cerr << "\n";
return;
}
using namespace std;
int mem[305][305][305];
int done[305][305][305];
int grid[305][305];
int n;
int dp(int r1, int c1, int r2, int c2, int step) {
if (r1 == n - 1 && r2 == n - 1 && c1 == n - 1 && c2 == n - 1) {
return grid[r1][c2];
}
if (r1 >= n || r2 >= n || c1 >= n || c2 >= n) return -1234567891;
if (done[r1][r2][step]) {
return mem[r1][r2][step];
}
int val = grid[r1][c1];
if (r1 != r2 || c1 != c2) {
val += grid[r2][c2];
}
int ret = -1234567891;
ret = max(ret, val + dp(r1 + 1, c1, r2 + 1, c2, step + 1));
ret = max(ret, val + dp(r1, c1 + 1, r2, c2 + 1, step + 1));
ret = max(ret, val + dp(r1 + 1, c1, r2, c2 + 1, step + 1));
ret = max(ret, val + dp(r1, c1 + 1, r2 + 1, c2, step + 1));
done[r1][r2][step] = 1;
return mem[r1][r2][step] = ret;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &grid[i][j]);
}
}
memset((done), (0), sizeof(done));
memset((mem), (0), sizeof(mem));
if (n == 1) {
cout << grid[0][0] << endl;
} else
cout << dp(0, 0, 0, 0, 0) << "\n";
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 305;
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
int n, c;
int dp[605][N][N], a[N][N];
int max(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); }
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]);
memset(dp, -inf, sizeof(dp));
dp[0][1][1] = a[1][1];
int l = (n - 1) * 2;
for (int i = 1; i <= l; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
if (k == j)
c = a[j][i + 2 - j];
else
c = a[j][i + 2 - j] + a[k][i + 2 - k];
dp[i][j][k] = max(dp[i - 1][j - 1][k - 1], dp[i - 1][j][k - 1],
dp[i - 1][j - 1][k], dp[i - 1][j][k]) +
c;
}
}
}
printf("%d\n", dp[l][n][n]);
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long n;
cin >> n;
long long a[n + 1][n + 1];
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
int dp[2 * n][n + 1][n + 1];
dp[1][1][1] = a[1][1];
for (long long i = 2; i < 2 * n; i++) {
for (long long j = max((long long)(1), i - n + 1); j <= min(i, n); j++) {
for (long long k = max((long long)(1), i - n + 1); k <= min(i, n); k++) {
if (j == k) {
if (j == 1) {
dp[i][j][k] = dp[i - 1][j][k] + a[i - j + 1][j];
} else if (i - j == 0) {
dp[i][j][k] = dp[i - 1][j - 1][k - 1] + a[i - j + 1][j];
} else {
dp[i][j][k] = dp[i - 1][j - 1][k];
dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k - 1]);
dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - 1][k - 1]);
dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k]);
dp[i][j][k] += a[i - j + 1][j];
}
} else {
if ((j == 1) && (k == i)) {
dp[i][j][k] =
a[i - j + 1][j] + a[i - k + 1][k] + dp[i - 1][j][k - 1];
} else if ((j == i) && (k == 1)) {
dp[i][j][k] =
a[i - j + 1][j] + a[i - k + 1][k] + dp[i - 1][j - 1][k];
} else if (j == 1) {
dp[i][j][k] = a[i - j + 1][j] + a[i - k + 1][k] +
max(dp[i - 1][j][k], dp[i - 1][j][k - 1]);
} else if (k == 1) {
dp[i][j][k] = a[i - j + 1][j] + a[i - k + 1][k] +
max(dp[i - 1][j][k], dp[i - 1][j - 1][k]);
} else if (i == j) {
dp[i][j][k] = a[i - j + 1][j] + a[i - k + 1][k] +
max(dp[i - 1][j - 1][k], dp[i - 1][j - 1][k - 1]);
} else if (i == k) {
dp[i][j][k] = a[i - j + 1][j] + a[i - k + 1][k] +
max(dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]);
} else {
dp[i][j][k] = max(dp[i - 1][j - 1][k - 1], dp[i - 1][j][k]);
dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - 1][k]);
dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k - 1]);
dp[i][j][k] += a[i - j + 1][j] + a[i - k + 1][k];
}
}
}
}
}
cout << dp[2 * n - 1][n][n] << "\n";
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 302;
int f[2][N][N];
int a[N][N];
const int Dir[4][2] = {{0, 0}, {-1, 0}, {-1, -1}, {0, -1}};
int main() {
int i, j, x1, x2, x1p, x2p, k, n;
int xs, xe;
cin >> n;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) scanf("%d", &a[i][j]);
f[0][0][0] = a[0][0];
for (k = 1; k <= n + n - 2; k++) {
xs = max(k - n + 1, 0);
xe = min(k, n - 1);
for (x1 = xs; x1 <= xe; x1++)
for (x2 = xs; x2 <= xe; x2++) {
int tmp = -100000000;
for (i = 0; i < 4; i++) {
x1p = x1 + Dir[i][0];
x2p = x2 + Dir[i][1];
if (x1p >= 0 and x2p >= 0 and x1p <= k - 1 and x2p <= k - 1) {
tmp = max(f[(k - 1) % 2][x1p][x2p], tmp);
}
}
f[k % 2][x1][x2] = tmp + a[k - x1][x1] + (x1 != x2) * a[k - x2][x2];
}
}
cout << f[0][n - 1][n - 1] << endl;
return 0;
}
| CPP |
214_E. Relay Race | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n × n cells (represented as unit squares), each cell has some number.
At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified.
To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.
Print the maximum number of points Furik and Rubik can earn on the relay race.
Input
The first line contains a single integer (1 ≤ n ≤ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 ≤ ai, j ≤ 1000) is the number written in the cell with coordinates (i, j).
Output
On a single line print a single number — the answer to the problem.
Examples
Input
1
5
Output
5
Input
2
11 14
16 12
Output
53
Input
3
25 16 25
12 18 19
11 13 8
Output
136
Note
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1).
Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
<image> Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n;
int a[303 + 303][303];
int d[303 + 303][303][303];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) scanf("%d", &a[i + j][j]);
for (int diag = 0; diag < n + n - 1; ++diag)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) d[diag][i][j] = -1000000000;
d[0][0][0] = a[0][0];
for (int diag = 0; diag < n + n - 2; ++diag)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
for (int di = 0; di < 2; ++di)
for (int dj = 0; dj < 2; ++dj) {
int si = i + di;
int sj = j + dj;
d[diag + 1][si][sj] =
max(d[diag + 1][si][sj],
d[diag][i][j] +
(si == sj ? a[diag + 1][si]
: (a[diag + 1][si] + a[diag + 1][sj])));
}
cout << d[n + n - 2][n - 1][n - 1] << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline long long rd() {
long long _x = 0;
int _ch = getchar(), _f = 1;
for (; !isdigit(_ch) && (_ch != '-') && (_ch != EOF); _ch = getchar())
;
if (_ch == '-') {
_f = 0;
_ch = getchar();
}
for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch - '0';
return _f ? _x : -_x;
}
void write(long long _x) {
if (_x >= 10)
write(_x / 10), putchar(_x % 10 + '0');
else
putchar(_x + '0');
}
inline void wrt(long long _x, char _p) {
if (_x < 0) putchar('-'), _x = -_x;
write(_x);
if (_p) putchar(_p);
}
bool must[105][105];
int G[105][105], f[105][105];
int dp[105], Dp[105];
int n, m, a, b, K;
int dijstra(int s, int t, int x) {
int dis[105];
bool vis[105];
memset(vis, 0, sizeof vis);
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
vis[x] = 1;
for (int i = int(1); i <= (int)(n); i++) {
int p = 0;
for (int j = int(1); j <= (int)(n); j++)
if (!vis[j] && dis[j] < dis[p]) p = j;
if (p == 0) return dis[t];
vis[p] = 1;
for (int j = int(1); j <= (int)(n); j++)
if (G[p][j] && dis[p] + 1 < dis[j]) dis[j] = dis[p] + 1;
}
return dis[t];
}
pair<int, int> ride[105];
bool vis[105];
int dfs(int u, int k) {
if (vis[u]) return Dp[u];
vis[u] = 0;
int tmp = -1;
for (int i = int(1); i <= (int)(n); i++)
if (f[u][i] == 1 && f[u][ride[k].second] == f[i][ride[k].second] + 1) {
tmp = max(tmp, dfs(i, k));
}
if (tmp == -1) tmp = 1e9;
tmp = min(tmp, dp[u]);
return Dp[u] = tmp;
}
int main() {
n = rd(), m = rd(), a = rd(), b = rd();
memset(G, 0, sizeof G);
memset(f, 0x3f, sizeof f);
for (int i = int(1); i <= (int)(m); i++) {
int x = rd(), y = rd();
G[x][y] = f[x][y] = 1;
}
for (int k = int(1); k <= (int)(n); k++)
for (int i = int(1); i <= (int)(n); i++)
for (int j = int(1); j <= (int)(n); j++)
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
for (int i = int(1); i <= (int)(n); i++) f[i][i] = 0;
K = rd();
for (int i = int(1); i <= (int)(K); i++) {
int x = rd(), y = rd();
ride[i] = make_pair(x, y);
if (f[x][y] == 1061109567) continue;
for (int j = int(1); j <= (int)(n); j++)
if (j != x && j != y) must[i][j] = (dijstra(x, y, j) != f[x][y]);
must[i][x] = 1;
must[i][y] = 1;
}
memset(dp, 0x3f, sizeof dp);
memset(Dp, 0x3f, sizeof Dp);
dp[b] = 0;
Dp[b] = 0;
while (1) {
bool flag = 0;
for (int i = int(1); i <= (int)(K); i++) {
for (int j = int(1); j <= (int)(n); j++)
if (must[i][j]) {
memset(vis, 0, sizeof vis);
int tmp = dfs(j, i) + 1;
if (tmp < dp[j]) dp[j] = tmp, flag = 1;
}
}
if (!flag) break;
}
wrt(dp[a] >= 1000000000 ? -1 : dp[a], '\n');
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 110, inf = 1e5;
bool forb[maxn], good[maxn][maxn];
int dis[maxn][maxn], dis2[maxn][maxn], d[maxn][maxn], dp[maxn], n;
vector<int> wh[maxn];
void floyd(int dis[][maxn]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (forb[i] || forb[j])
dis[i][j] = inf;
else
dis[i][j] = d[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++) d[i][j] = inf;
for (int i = 0; i < maxn; i++) d[i][i] = 0;
for (int i = 0; i < maxn; i++) dp[i] = inf;
int m, A, B;
cin >> n >> m >> A >> B;
--A, --B;
while (m--) {
int a, b;
cin >> a >> b;
--a, --b;
d[a][b] = 1;
}
floyd(dis);
int Q;
cin >> Q;
while (Q--) {
int a, b;
cin >> a >> b;
--a, --b;
if (dis[a][b] == inf) continue;
for (int i = 0; i < n; i++) {
wh[i].clear();
}
for (int i = 0; i < n; i++) {
if (dis[a][i] != inf) wh[dis[a][i]].push_back(i);
}
for (int i = 0; i <= dis[a][b]; i++) {
int CNT = 0, LST = -1;
for (int u : wh[i]) {
if (dis[a][u] + dis[u][b] == dis[a][b]) CNT++, LST = u;
}
if (CNT == 1) {
good[LST][b] = 1;
}
}
}
queue<int> q;
dp[B] = 0;
q.push(B);
int Now = 0;
while (int((q).size())) {
while (int((q).size())) {
int u = q.front();
q.pop();
forb[u] = 1;
}
floyd(dis2);
for (int i = 0; i < n; i++) {
if (dp[i] != inf) continue;
for (int j = 0; j < n; j++) {
if (good[i][j] && dis2[i][j] > dis[i][j]) {
q.push(i);
dp[i] = Now + 1;
break;
}
}
}
Now++;
}
int ans = dp[A];
if (ans == inf) ans = -1;
return cout << ans << endl, 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 105;
vector<int> adj[N];
vector<int> vec[N];
int dis[N][N];
bool es[N][N];
int ans[N];
int st[N], ed[N];
bool mark[N];
int d[N];
queue<int> q;
void dfs(int v, int d) {
if (mark[v] || ans[v] < d) {
return;
}
mark[v] = true;
for (auto u : adj[v]) {
dfs(u, d);
}
}
int main() {
int n, m, a, b, k;
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; i++) {
ans[i] = N;
for (int j = 1; j < i; j++) {
dis[i][j] = N;
dis[j][i] = N;
}
}
ans[b] = 0;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
dis[u][v] = 1;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
cin >> k;
for (int i = 0; i < k; i++) {
int u, v;
cin >> u >> v;
st[i] = u;
ed[i] = v;
if (dis[u][v] == N) {
continue;
}
for (int j = 0; j <= n; j++) {
vec[j].clear();
}
for (int j = 1; j <= n; j++) {
if (dis[u][j] + dis[j][v] == dis[u][v]) {
vec[dis[u][j]].push_back(j);
}
}
for (int j = 0; j <= n; j++) {
if (vec[j].size() == 1) {
es[i][vec[j][0]] = true;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
fill(d, d + n + 1, N);
d[j] = 0;
q.push(j);
while (q.size()) {
int v = q.front();
q.pop();
for (auto u : adj[v]) {
if (ans[u] < i) {
continue;
}
if (d[u] == N) {
d[u] = d[v] + 1;
q.push(u);
}
}
}
for (int q = 0; q < k; q++) {
if (es[q][j] && dis[j][ed[q]] < d[ed[q]] && ans[j] == N) {
ans[j] = i;
}
}
}
}
if (ans[a] == N) {
cout << -1;
} else {
cout << ans[a];
}
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int get() {
int f = 0, v = 0;
char ch;
while (!isdigit(ch = getchar()))
if (ch == '-') break;
if (ch == '-')
f = 1;
else
v = ch - '0';
while (isdigit(ch = getchar())) v = v * 10 + ch - '0';
if (f)
return -v;
else
return v;
}
const int maxn = 103, inf = 1000000000;
int d[maxn][maxn], n, f[maxn], g[maxn], vis[maxn], cnt[maxn][maxn], T[maxn], m,
A, B, tot = 1;
bool p[maxn][maxn];
int dfs(int x, int aim) {
if (x == aim) return f[x];
if (vis[x] == tot) return g[x];
g[x] = 0;
vis[x] = tot;
for (int i = 1; i <= n; i++)
if (d[x][i] == 1 && 1 + d[i][aim] == d[x][aim])
g[x] = max(g[x], dfs(i, aim));
return g[x] = min(g[x], f[x]);
}
int main() {
n = get(), m = get(), A = get(), B = get();
for (int i = 1; i <= n; d[i][i] = 0, f[i] = inf, i++)
for (int j = 1; j <= n; j++) d[i][j] = inf;
f[B] = 0;
for (int i = 1, x; i <= m; i++) x = get(), d[x][get()] = 1;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
m = get();
for (int i = 1; i <= m; i++) {
int x = get(), y = get();
T[i] = y;
if (d[x][y] == inf) continue;
for (int j = 1; j <= n; j++)
if (d[x][j] + d[j][y] == d[x][y]) cnt[i][d[x][j]]++;
for (int j = 1; j <= n; j++)
if (d[x][j] + d[j][y] == d[x][y] && cnt[i][d[x][j]] == 1) p[i][j] = 1;
}
for (int _ = 1; _ <= n; _++) {
bool flag = 0;
for (int i = 1; i <= m; i++, tot++)
for (int j = 1; j <= n; j++) {
if (!p[i][j]) continue;
int tp = dfs(j, T[i]) + 1;
if (tp < f[j]) f[j] = tp, flag = 1;
}
}
printf("%d\n", f[A] == inf ? -1 : f[A]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dis[105][105], Q[105][105][105], n, m, S, T, dp[105], g[105], pt, i, j, A,
B, from[105], to[105], k, QQ, l;
const int inf = 453266144;
bool FLAG;
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) dis[i][j] = inf;
for (i = 1; i <= m; i++) {
scanf("%d%d", &A, &B);
dis[A][B] = 1;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
for (k = 1; k <= n; k++)
if (dis[j][i] + dis[i][k] < dis[j][k])
dis[j][k] = dis[j][i] + dis[i][k];
for (i = 1; i <= n; i++) dis[i][i] = 0;
scanf("%d", &QQ);
while (QQ--) {
scanf("%d%d", &A, &B);
if (dis[A][B] < inf) {
pt++;
from[pt] = A;
to[pt] = B;
for (i = 1; i <= n; i++)
if (dis[A][i] + dis[i][B] == dis[A][B])
Q[pt][dis[A][i]][++Q[pt][dis[A][i]][0]] = i;
}
}
for (i = 1; i <= n; i++) dp[i] = inf;
dp[T] = 0;
FLAG = true;
while (FLAG) {
FLAG = false;
for (i = 1; i <= pt; i++) {
g[to[i]] = dp[to[i]];
for (j = dis[from[i]][to[i]] - 1; j >= 0; j--) {
for (k = 1; k <= Q[i][j][0]; k++) {
g[Q[i][j][k]] = 0;
for (l = 1; l <= Q[i][j + 1][0]; l++)
if (dis[Q[i][j][k]][Q[i][j + 1][l]] == 1)
g[Q[i][j][k]] = max(g[Q[i][j][k]], g[Q[i][j + 1][l]]);
g[Q[i][j][k]] = min(g[Q[i][j][k]], dp[Q[i][j][k]]);
}
if (Q[i][j][0] == 1 && dp[Q[i][j][1]] > g[Q[i][j][1]] + 1) {
dp[Q[i][j][1]] = g[Q[i][j][1]] + 1;
FLAG = true;
}
}
}
}
if (dp[S] < inf)
cout << dp[S];
else
cout << -1;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x;
}
inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }
int n, m, a, b, g[105][105];
int ans[105][105];
bool vis[105][105];
bool only[105][105], cnc[105][105];
int x[105], y[105], k;
int can[105][105], cnt[105];
int que[105], dis[105], dp[105], lo = 1, hi = 0;
int cal(int pos, int no) {
if (vis[pos][no]) return ans[pos][no];
if (pos == b) return ans[pos][no] = 0;
int i, tpans = 0;
if (pos == y[no]) tpans = 233366666;
for (i = 1; i <= n; i++) {
if (cnc[pos][i] && g[pos][y[no]] == g[i][y[no]] + 1) {
tpans = max(tpans, cal(i, no));
}
}
ans[pos][no] = min(tpans, dp[pos]);
return ans[pos][no];
}
int main() {
int i, j, e, f, l;
n = read();
m = read();
a = read();
b = read();
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
g[i][j] = 233366666;
}
g[i][i] = 0;
}
for (i = 1; i <= m; i++) {
e = read();
f = read();
cnc[e][f] = 1;
g[e][f] = 1;
}
for (l = 1; l <= n; l++) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
g[i][j] = min(g[i][j], g[i][l] + g[l][j]);
}
}
}
k = read();
for (i = 1; i <= k; i++) {
x[i] = read();
y[i] = read();
}
for (l = 1; l <= k; l++) {
int tp = g[x[l]][y[l]];
if (tp == 233366666) continue;
for (j = 1; j <= n; j++) {
if (j == x[l] || j == y[l]) {
only[l][j] = 1;
can[j][++cnt[j]] = l;
continue;
}
for (i = 1; i <= n; i++) dis[i] = 233366666;
lo = 1;
hi = 0;
que[++hi] = x[l];
dis[x[l]] = 0;
while (lo <= hi) {
int t = que[lo++];
for (i = 1; i <= n; i++) {
if (i == j) continue;
if (!cnc[t][i]) continue;
if (dis[i] > dis[t] + 1) {
dis[i] = dis[t] + 1;
que[++hi] = i;
}
}
}
if (dis[y[l]] > tp) {
only[l][j] = 1;
can[j][++cnt[j]] = l;
}
}
}
for (i = 1; i <= n; i++) dp[i] = 233366666;
dp[b] = 0;
int flag = 1;
while (flag) {
flag = 0;
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++) {
int prev = dp[i];
for (j = 1; j <= cnt[i]; j++) {
dp[i] = min(dp[i], cal(i, can[i][j]) + 1);
}
if (dp[i] != prev) flag = 1;
}
}
int ans = dp[a];
if (ans == 233366666) {
puts("-1");
exit(0);
}
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 105, K = N, inf = 1e9 + 7;
int n, m, a, b, k, s[K], t[K];
int g[N][N], dist[N][N], deg[N][K], f[N][K];
bool must[N][K];
struct data {
int i, j, val;
void extend();
};
deque<data> q;
void data::extend() {
if (f[i][j]) return;
f[i][j] = val;
if (must[i][j])
for (int jj = 1; jj <= k; jj++)
if (dist[s[jj]][i] + dist[i][t[jj]] == dist[s[jj]][t[jj]])
q.push_back((data){i, jj, val + 1});
for (int ii = 1; ii <= n; ii++)
if (ii != i)
if (dist[s[j]][ii] + g[ii][i] + dist[i][t[j]] == dist[s[j]][t[j]] &&
!--deg[ii][j])
q.push_front((data){ii, j, val});
}
int J, *top, ord[N];
bool cmp(const int i, const int j) { return dist[s[J]][i] < dist[s[J]][j]; }
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = dist[i][j] = i == j ? 0 : inf;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
g[u][v] = dist[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
scanf("%d", &k);
for (J = 1; J <= k; J++) {
scanf("%d%d", &s[J], &t[J]);
if (dist[s[J]][t[J]] == inf) {
J--;
k--;
continue;
}
top = ord;
for (int i = 1; i <= n; i++)
if (dist[s[J]][i] + dist[i][t[J]] == dist[s[J]][t[J]]) *top++ = i;
sort(ord, top, cmp);
for (int l = 0, r; l < top - ord; l = r) {
for (r = l; r < top - ord && !cmp(ord[l], ord[r]); r++)
;
if (r - l == 1) must[ord[l]][J] = true;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++)
for (int ii = 1; ii <= n; ii++)
if (ii != i)
if (dist[s[j]][i] + g[i][ii] + dist[ii][t[j]] == dist[s[j]][t[j]])
deg[i][j]++;
for (int j = 1; j <= k; j++)
if (dist[s[j]][b] + dist[b][t[j]] == dist[s[j]][t[j]])
q.push_back((data){b, j, 1});
while (!q.empty()) {
data u = q.front();
q.pop_front();
u.extend();
}
int ans = inf;
for (int j = 1; j <= k; j++)
if (must[a][j] && f[a][j]) ans = min(ans, f[a][j]);
printf("%d\n", ans == inf ? -1 : ans);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 105, INF = 1e8;
int n, m, start, fin, d[MAXN][MAXN], en[MAXN];
int cnt[MAXN], dp[MAXN][MAXN];
bool has[MAXN][MAXN], must[MAXN][MAXN];
vector<int> out[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> start >> fin;
memset(d, 63, sizeof d);
for (int i = 1; i <= n; ++i) d[i][i] = 0;
while (m--) {
int u, v;
cin >> u >> v;
out[u].push_back(v);
d[u][v] = 1;
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (d[i][k] + d[k][j] < d[i][j]) d[i][j] = d[i][k] + d[k][j];
int a, b, k;
cin >> k;
for (int i = 1; i <= k; ++i) {
cin >> a >> b;
if (d[a][b] > INF) continue;
memset(cnt, 0, sizeof cnt);
en[i] = b;
for (int x = 1; x <= n; ++x)
if (d[a][x] + d[x][b] == d[a][b]) cnt[d[a][x]] += (has[x][i] = 1);
for (int x = 1; x <= n; ++x)
if (d[a][x] + d[x][b] == d[a][b] && cnt[d[a][x]] == 1) must[x][i] = 1;
}
for (int i = 0; i <= n; ++i) fill(dp[i], dp[i] + MAXN, INF);
dp[fin][0] = 0;
bool ok = 1;
while (ok) {
ok = 0;
for (int v = 1; v <= n; ++v)
for (int b = 0; b <= k; ++b) {
if (b && !has[v][b]) continue;
int &ref = dp[v][b];
int tmp = dp[v][b];
if (!b) {
for (int bb = 1; bb <= k; ++bb)
if (must[v][bb]) ref = min(ref, dp[v][bb] + 1);
} else {
ref = min(ref, dp[v][0]);
int maxi = -1;
for (int u : out[v])
if (has[u][b] && 1 + d[u][en[b]] == d[v][en[b]])
maxi = max(maxi, dp[u][b]);
if (maxi == -1) maxi = INF;
ref = min(ref, maxi);
}
if (tmp != ref) ok = 1;
}
}
cout << (dp[start][0] >= INF ? -1 : dp[start][0]) << "\n";
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 250;
int n, m, num, cnt;
int a[maxn][maxn], b[maxn][maxn], S[maxn], T[maxn];
int ans[maxn], vis[maxn], dp[maxn], p[maxn][maxn];
int dfs(int u, int v) {
if (vis[u] == cnt) return dp[u];
vis[u] = cnt;
int Ans = -1;
for (int i = 1; i <= n; i++)
if (a[u][i] == 1 && a[u][i] + a[i][v] == a[u][v]) Ans = max(Ans, dfs(i, v));
if (Ans < 0) Ans = 1e9;
dp[u] = min(Ans, ans[u] + 1);
return dp[u];
}
int main() {
int u, v;
scanf("%d%d%d%d", &n, &m, &S[0], &T[0]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j) a[i][j] = 1e9;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
a[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
scanf("%d%d", &u, &v);
for (int j = 1; j <= n; j++)
if (a[u][j] < 1e9 && a[u][j] + a[j][v] == a[u][v]) b[i][a[u][j]]++;
for (int j = 1; j <= n; j++)
if (a[u][j] < 1e9 && a[u][j] + a[j][v] == a[u][v] && b[i][a[u][j]] == 1)
p[i][j] = 1;
S[i] = u, T[i] = v;
}
for (int i = 1; i <= n; i++) ans[i] = 1e9;
ans[T[0]] = 0;
while (1) {
bool find = 0;
for (int i = 1; i <= num; i++)
for (int j = 1; j <= n; j++)
if (p[i][j]) {
++cnt;
int tmp = dfs(j, T[i]);
if (tmp < ans[j]) {
ans[j] = tmp;
find = 1;
}
}
if (!find) break;
}
if (ans[S[0]] < 1e9)
printf("%d\n", ans[S[0]]);
else
printf("-1\n");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T min(T &a, T &b) {
return a < b ? a : b;
}
template <class T>
inline T max(T &a, T &b) {
return a > b ? a : b;
}
template <class T>
void read(T &x) {
char ch;
while ((ch = getchar()) && !isdigit(ch))
;
x = ch - '0';
while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0';
}
struct point {
int x, y;
point() {}
point(int _x, int _y) : x(_x), y(_y) {}
};
long long Pow(long long a, long long b, long long mod) {
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 = 200;
int n, m, K, dis[N][N], ti, ok[N][N], dp[N][N], vis[N], can[N], Vis[N][N],
G[N][N];
vector<int> E[N], F[N], full[N], Road[N], Up[N];
int S, T;
void bfs(int st) {
queue<int> Q;
for (int i = 1; i <= n; i++) dis[ti][i] = ((~0U >> 1) - 3);
dis[ti][st] = 0;
Q.push(st);
while (Q.size()) {
int x = Q.front();
Q.pop();
for (int i = 0; i < E[x].size(); i++)
if (dis[ti][E[x][i]] > dis[ti][x] + 1) {
dis[ti][E[x][i]] = dis[ti][x] + 1;
Q.push(E[x][i]);
}
}
}
void dfs(int x) {
Vis[ti][x] = 1;
if (vis[x] != ti) {
full[ti].push_back(x);
Road[dis[ti][x]].push_back(x);
}
vis[x] = ti;
for (int i = 0; i < F[x].size(); i++)
if (dis[ti][F[x][i]] == dis[ti][x] - 1) dfs(F[x][i]);
}
int dfs(int x, int y) {
int ans = 0;
for (int i = 1; i <= n; i++)
if (Vis[y][i] && G[x][i] && dis[y][i] == dis[y][x] + 1)
ans = max(ans, dfs(i, y));
if (ans == 0) ans = ((~0U >> 1) - 3);
ans = min(ans, dp[x][y]);
ans = min(ans, can[x] + 1);
return ans;
}
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
int x, y;
if (S == T) {
puts("0");
return 0;
}
for (int i = 1; i <= m; i++)
scanf("%d%d", &x, &y), E[x].push_back(y), F[y].push_back(x), G[x][y] = 1;
scanf("%d", &K);
for (int i = 1; i <= K; i++) {
scanf("%d%d", &x, &y);
ti = i;
for (int j = 0; j <= n; j++) Road[j].clear();
bfs(x);
dfs(y);
for (int j = 0; j <= n; j++)
if (Road[j].size() == 1)
Up[Road[j][0]].push_back(i), ok[Road[j][0]][i] = 1;
}
for (int i = 1; i <= n; i++) can[i] = ((~0U >> 1) - 3);
for (int i = 1; i <= n; i++)
for (int j = 0; j <= K; j++) dp[i][j] = ((~0U >> 1) - 3);
for (int i = 0; i < Up[T].size(); i++) dp[T][Up[T][i]] = 1, can[T] = 1;
while (1) {
int F = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= K; j++)
if (ok[i][j] == 1) {
int flag = 1, Max = 0, size = 0;
Max = dfs(i, j);
if (dp[i][j] > Max) {
dp[i][j] = Max, can[i] = min(can[i], dp[i][j]), F = 1;
}
}
if (!F) break;
}
int ans = ((~0U >> 1) - 3);
for (int i = 1; i <= K; i++) ans = min(ans, dp[S][i]);
if (ans == ((~0U >> 1) - 3))
puts("-1");
else
printf("%d\n", ans);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int NMax = 110;
struct pii {
int x, y;
};
pii mp(int x, int y) {
pii ret;
ret.x = x;
ret.y = y;
return ret;
}
int N, M, S, T, K;
int G[NMax][NMax], cnt[NMax], good[NMax][NMax], cost[NMax][NMax];
pii bus[NMax];
vector<int> must[NMax], route[NMax];
int main() {
memset(G, -1, sizeof(G));
scanf("%d%d%d%d", &N, &M, &S, &T);
for (int i = 1; i <= N; i++) G[i][i] = 0;
for (int i = 1; i <= M; i++) {
int x, y;
scanf("%d%d", &x, &y);
G[x][y] = 1;
}
for (int k = 1; k <= N; k++)
for (int i = 1; i <= N; i++)
if (G[i][k] != -1) {
for (int j = 1; j <= N; j++)
if (G[k][j] != -1 && (G[i][j] == -1 || G[i][j] > G[i][k] + G[k][j]))
G[i][j] = G[i][k] + G[k][j];
}
scanf("%d", &K);
for (int i = 1; i <= K; i++) {
int x, y;
scanf("%d%d", &x, &y);
bus[i] = mp(x, y);
if (G[x][y] == -1) continue;
memset(cnt, 0, sizeof(cnt));
for (int j = 1; j <= N; j++)
if (G[x][j] != -1 && G[j][y] != -1 && G[x][j] + G[j][y] == G[x][y]) {
cnt[G[x][j]]++;
route[i].push_back(j);
}
for (int j = 1; j <= N; j++)
if (G[x][j] != -1 && G[j][y] != -1 && G[x][j] + G[j][y] == G[x][y] &&
cnt[G[x][j]] == 1)
must[j].push_back(i);
}
for (int i = 1; i <= K; i++) {
int flag = 0;
for (int j = 0; j < route[i].size(); j++)
if (route[i][j] == T) {
flag = 1;
break;
}
if (flag) good[T][i] = 1;
}
while (1) {
int flag = 0;
for (int i = 1; i <= K; i++) {
int X = bus[i].x, Y = bus[i].y;
if (G[X][Y] == -1) continue;
for (int j = 0; j < route[i].size(); j++) {
int x = route[i][j];
int flag1 = 1;
for (int k = 1; k <= N; k++)
if (G[x][k] == 1 && G[X][k] == G[X][x] + 1 && G[X][k] != -1 &&
G[k][Y] != -1 && G[X][k] + G[k][Y] == G[X][Y]) {
if (!good[k][i]) {
flag1 = 0;
break;
}
}
if (!good[x][i]) cost[x][i] = 1000000000;
if (flag1) {
int maxx = -1;
for (int k = 1; k <= N; k++)
if (G[x][k] == 1 && G[X][k] == G[X][x] + 1 && G[X][k] != -1 &&
G[k][Y] != -1 && G[X][k] + G[k][Y] == G[X][Y])
maxx = max(maxx, cost[k][i]);
if (maxx != -1) {
good[x][i] = 1;
if (maxx < cost[x][i]) {
cost[x][i] = maxx;
flag = 1;
}
}
}
int minn = 1000000000;
for (int k = 0; k < must[x].size(); k++)
if (good[x][must[x][k]]) {
int y = must[x][k];
good[x][i] = 1;
minn = min(minn, cost[x][y] + 1);
}
if (minn < cost[x][i]) {
cost[x][i] = minn;
flag = 1;
}
}
}
if (!flag) break;
}
int ret = 1000000000;
for (int i = 0; i < must[S].size(); i++)
if (good[S][must[S][i]]) {
int x = must[S][i];
ret = min(ret, cost[S][x]);
}
if (ret == 1000000000)
puts("-1");
else
printf("%d\n", ret + 1);
getchar();
getchar();
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m, a, b, k, s[105], t[105], g[105][105], cnt[105], dp[105][105];
bool must[105][105];
bool on(int i, int j) { return g[s[i]][j] + g[j][t[i]] == g[s[i]][t[i]]; }
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(g, 0x3f, sizeof g);
for (int i = 1; i <= n; i++) g[i][i] = 0;
for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), g[u][v] = 1;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
scanf("%d", &k);
for (int i = 1; i <= k; i++) {
scanf("%d%d", s + i, t + i);
if (g[s[i]][t[i]] >= n) continue;
memset(cnt, 0, sizeof cnt);
for (int j = 1; j <= n; j++)
if (on(i, j)) cnt[g[s[i]][j]]++;
for (int j = 1; j <= n; j++)
if (on(i, j) && cnt[g[s[i]][j]] == 1) must[i][j] = true;
}
memset(dp, 0x3f, sizeof dp);
for (int i = 1; i <= k; i++)
if (on(i, b)) dp[i][b] = 1;
for (;;) {
bool f = false;
for (int j = 1; j <= k; j++)
for (int i = 1; i <= n; i++)
if (on(j, i)) {
if (i == b) continue;
int ma = -1;
for (int x = 1; x <= n; x++)
if (g[i][x] == 1 && g[x][t[j]] + 1 == g[i][t[j]])
ma = max(ma, dp[j][x]);
if (ma != -1 && ma < dp[j][i]) dp[j][i] = ma, f = true;
for (int x = 1; x <= k; x++)
if (must[x][i] && x != j && dp[x][i] + 1 < dp[j][i])
dp[j][i] = dp[x][i] + 1, f = true;
}
if (!f) break;
}
int ans = inf;
for (int i = 1; i <= k; i++)
if (must[i][a]) ans = min(ans, dp[i][a]);
printf("%d\n", ans == inf ? -1 : ans);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 101;
const int I = 1 << 28;
int d[N][N], cnt[N][N], s[N], t[N], lev[N], ans[N], z[N], cur, n, m, a, b, w;
bool c[N][N];
int BFS(int v, int u) {
if (lev[v] == cur) return ans[v];
lev[v] = cur;
int res = -1;
for (int i = 1; i <= n; i++)
if (d[v][i] == 1 && 1 + d[i][u] == d[v][u]) res = max(res, BFS(i, u));
res = (res < 0 ? z[0] : res);
ans[v] = min(res, z[v] + 1);
return ans[v];
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(d, 63, sizeof d);
memset(z, 63, sizeof z);
for (int i = 1; i <= n; i++) d[i][i] = 0;
for (int i = 1, v, u; i <= m; i++) {
scanf("%d%d", &v, &u);
d[v][u] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
scanf("%d", &w);
for (int i = 1; i <= w; i++) {
scanf("%d%d", &s[i], &t[i]);
for (int j = 1; j <= n; j++)
if (d[s[i]][j] < I)
cnt[i][d[s[i]][j]] += (d[s[i]][j] + d[j][t[i]] == d[s[i]][t[i]]);
for (int j = 1; j <= n; j++)
c[i][j] = (d[s[i]][j] < I && d[s[i]][j] + d[j][t[i]] == d[s[i]][t[i]] &&
cnt[i][d[s[i]][j]] == 1);
}
z[b] = 0;
bool F = true;
for (;;) {
F = false;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= n; j++) {
if (!c[i][j]) continue;
cur++;
int res = BFS(j, t[i]);
F |= (res < z[j]);
z[j] = min(z[j], res);
}
}
if (!F) break;
}
z[a] = (z[a] < I ? z[a] : -1);
printf("%d", z[a]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > edge;
int flo[120][120], flo2[120][120], dp[120][120], menda[120][120], com;
int st[120], en[120];
vector<int> vc[120];
void sho(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
flo[i][j] = 1e7;
}
}
for (int i = 0; i < edge.size(); i++) {
flo[edge[i].first][edge[i].second] = 1;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
flo[i][j] = min(flo[i][k] + flo[k][j], flo[i][j]);
}
}
}
}
void sho2(int n, int stop) {
memset(flo2, 0, sizeof flo2);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j && i != stop) continue;
flo2[i][j] = 1e7;
}
}
for (int i = 0; i < edge.size(); i++) {
if (edge[i].first == stop || edge[i].second == stop) continue;
flo2[edge[i].first][edge[i].second] = 1;
}
for (int k = 1; k <= n; k++) {
if (k == stop) continue;
for (int i = 1; i <= n; i++) {
if (i == stop) continue;
for (int j = 1; j <= n; j++) {
if (j == stop) continue;
flo2[i][j] = min(flo2[i][k] + flo2[k][j], flo2[i][j]);
}
}
}
}
void dp_func(int v, int bus, int x, int y) {
int f = -1;
for (int i = 0; i < vc[v].size(); i++) {
int w = vc[v][i];
if (flo[v][y] == flo[w][y] + 1) f = max(f, dp[w][bus]);
}
if (f == -1) f = 1e7;
for (int i = 1; i <= com; i++) {
if (bus == i) continue;
int c = st[i], d = en[i];
if (flo[c][d] == 1e7) continue;
if (menda[i][v] == 0) continue;
f = min(f, dp[v][i] + 1);
}
dp[v][bus] = min(f, dp[v][bus]);
}
int main() {
int i, j, k, l, n, m, a, b;
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &l, &k);
edge.push_back(make_pair(l, k));
vc[l].push_back(k);
}
sho(n);
if (flo[a][b] == 1e7) {
cout << "-1";
return 0;
}
scanf("%d", &com);
for (int i = 1; i <= com; i++) {
scanf("%d%d", &st[i], &en[i]);
}
for (int i = 1; i <= n; i++) {
sho2(n, i);
for (int j = 1; j <= com; j++) {
int x = st[j], y = en[j];
if (flo[x][y] < flo2[x][y]) menda[j][i] = 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= com; j++) dp[i][j] = 1e7;
}
for (int j = 1; j <= com; j++) {
if (flo[st[j]][en[j]] == 1e7) continue;
if (menda[j][b]) dp[b][j] = 0;
}
for (int ite = 1; ite <= n; ite++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= com; j++) {
int x = st[j], y = en[j];
if (flo[x][y] == 1e7) continue;
dp_func(i, j, x, y);
}
}
}
int ans = 1e7;
for (int j = 1; j <= com; j++) {
if (!menda[j][a]) continue;
ans = min(ans, dp[a][j]);
}
if (ans == 1e7)
ans = -1;
else
ans++;
cout << ans << endl;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int inf = 100000000;
vector<int> g[maxn][maxn], gb[maxn][maxn];
int dist[maxn][maxn], st[maxn], en[maxn], q[maxn], d2[maxn][maxn];
bool onpath[maxn][maxn], gone[maxn], calced[maxn], cp[maxn], cutp[maxn][maxn];
int main() {
int n, m, t1, t2, i, j, k, n2, ans;
scanf("%d%d%d%d", &n, &m, &t1, &t2);
for (i = 1; i <= (n); ++i)
for (j = 1; j <= (n); ++j) dist[i][j] = inf;
for (i = 1; i <= (n); ++i) dist[i][i] = 0;
for (i = 1; i <= (m); ++i) {
int x, y;
scanf("%d%d", &x, &y);
dist[x][y] = 1;
}
for (i = 1; i <= (n); ++i)
for (j = 1; j <= (n); ++j) d2[i][j] = dist[i][j];
for (k = 1; k <= (n); ++k)
for (i = 1; i <= (n); ++i)
for (j = 1; j <= (n); ++j)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
scanf("%d", &n2);
memset(onpath, false, sizeof(onpath));
for (i = 1; i <= (n2); ++i) {
scanf("%d%d", &st[i], &en[i]);
if (dist[st[i]][en[i]] == inf) continue;
for (j = 1; j <= (n); ++j)
for (k = 1; k <= (n); ++k)
if (d2[j][k] == 1)
if (dist[st[i]][j] + 1 + dist[k][en[i]] == dist[st[i]][en[i]]) {
g[i][j].push_back(k);
gb[i][k].push_back(j);
}
for (j = 1; j <= (n); ++j)
onpath[i][j] = dist[st[i]][j] + dist[j][en[i]] == dist[st[i]][en[i]];
}
memset(cutp, false, sizeof(cutp));
for (i = 1; i <= (n2); ++i)
for (j = 1; j <= (n); ++j) {
if (!onpath[i][j]) continue;
memset(gone, true, sizeof(gone));
int l, r;
q[l = r = 1] = st[i];
gone[st[i]] = false;
while (l <= r) {
int tmp = q[l++];
if (tmp == j) continue;
for (k = 1; k <= (g[i][tmp].size()); ++k) {
int x = g[i][tmp][k - 1];
if (gone[x]) {
q[++r] = x;
gone[x] = false;
}
}
}
if (gone[en[i]]) cutp[i][j] = true;
}
for (i = 1; i <= (n2); ++i) cutp[i][en[i]] = true;
memset(calced, false, sizeof(calced));
calced[t2] = true;
bool unchanged = false;
for (ans = 1; ans <= (n); ++ans) {
for (i = 1; i <= (n); ++i) cp[i] = calced[i];
for (i = 1; i <= (n2); ++i) {
if (calced[en[i]]) {
for (j = 1; j <= (n); ++j) cp[j] |= onpath[i][j] && cutp[i][j];
continue;
}
memset(gone, true, sizeof(gone));
int l, r;
q[l = r = 1] = en[i];
gone[en[i]] = false;
while (l <= r) {
int tmp = q[l++];
for (j = 1; j <= (gb[i][tmp].size()); ++j) {
int x = gb[i][tmp][j - 1];
if (!calced[x] && gone[x]) {
q[++r] = x;
gone[x] = false;
}
}
}
for (j = 1; j <= (n); ++j) cp[j] |= onpath[i][j] && gone[j] && cutp[i][j];
}
for (i = 1; i <= (n); ++i) calced[i] = cp[i];
if (calced[t1]) {
printf("%d\n", ans);
break;
}
}
if (!calced[t1]) printf("-1\n");
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int RLEN = 1 << 20 | 1;
inline char gc() {
static char ibuf[RLEN], *ib, *ob;
(ob == ib) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
return (ob == ib) ? EOF : *ib++;
}
inline int read() {
char ch = getchar();
int res = 0, f = 1;
while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = getchar();
return f ? res : -res;
}
const int mod = 998244353, g = 3;
inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline void Add(int &a, int b) { a = add(a, b); }
inline int dec(int a, int b) { return a >= b ? a - b : a - b + mod; }
inline void Dec(int &a, int b) { a = dec(a, b); }
inline int mul(int a, int b) {
return 1ll * a * b >= mod ? 1ll * a * b % mod : a * b;
}
inline void Mul(int &a, int b) { a = mul(a, b); }
inline int ksm(int a, int b, int res = 1) {
for (; b; b >>= 1, a = mul(a, a)) (b & 1) ? (res = mul(res, a)) : 0;
return res;
}
const int N = 105;
int dis[N][N], n, m, st, des, q;
int s[N], d[N], vis[N], pt[N][N], f[N], t, mx[N];
int dfs(int p, int u) {
if (vis[u] == t) return f[u];
vis[u] = t;
int res = -1;
for (int i = 1; i <= n; i++) {
if (dis[u][i] == 1 && dis[i][d[p]] + 1 == dis[u][d[p]])
res = max(res, dfs(p, i));
}
if (res == -1) res = 1e9;
res = min(res, mx[u]);
return f[u] = res;
}
int main() {
n = read(), m = read(), st = read(), des = read();
memset(dis, 127 / 4, sizeof(dis));
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
dis[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
q = read();
for (int i = 1; i <= q; i++) {
memset(vis, 0, sizeof(vis));
s[i] = read(), d[i] = read();
if (dis[s[i]][d[i]] > n) continue;
for (int j = 1; j <= n; j++)
if (dis[s[i]][j] + dis[j][d[i]] == dis[s[i]][d[i]]) vis[dis[s[i]][j]]++;
for (int j = 1; j <= n; j++)
if (dis[s[i]][j] + dis[j][d[i]] == dis[s[i]][d[i]] &&
vis[dis[s[i]][j]] == 1)
pt[i][j] = 1;
}
memset(vis, 0, sizeof(vis));
memset(mx, 127 / 4, sizeof(mx));
mx[des] = 0;
bool fg = 1;
while (fg) {
fg = 0;
for (int i = 1; i <= q; i++)
for (int j = 1; j <= n; j++)
if (pt[i][j]) {
t++;
int now = dfs(i, j) + 1;
if (now < mx[j]) mx[j] = now, fg = 1;
}
}
if (mx[st] > n)
puts("-1");
else
cout << mx[st] << '\n';
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 109, INF = 1e9;
vector<int> bus[MAX][MAX], g[MAX];
int dis[MAX][MAX], ted[MAX], pos[MAX][MAX], n, m, st, en, k, bes = INF;
bool been[MAX], big[MAX][MAX];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> st >> en, st--, en--;
memset(dis, 63, sizeof dis);
for (int i = 0, v, u; i < m; i++)
cin >> v >> u, v--, u--, g[v].push_back(u), dis[v][u] = 1;
for (int i = 0; i < n; i++) dis[i][i] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
cin >> k;
for (int i = 0, v, u; i < k; i++) {
cin >> v >> u, v--, u--;
if (dis[v][u] > INF) continue;
memset(ted, 0, sizeof ted), memset(been, 0, sizeof been);
for (int j = 0; j < n; j++)
if (dis[v][j] + dis[j][u] == dis[v][u]) been[j] = 1, ted[dis[v][j]]++;
for (int j = 0; j < n; j++)
if (been[j] && ted[dis[v][j]] == 1) big[j][i] = 1;
for (int j = 0; j < n; j++)
if (been[j])
for (auto uu : g[j])
if (been[uu] && dis[v][j] + 1 == dis[v][uu]) bus[j][i].push_back(uu);
}
memset(pos, 63, sizeof pos);
for (int i = 0; i < k; i++) pos[en][i] = 1;
for (int tof = 0; tof < 2 * MAX; tof++)
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) {
for (int ss = 0; ss < k; ss++)
if (big[i][ss]) pos[i][j] = min(pos[i][j], pos[i][ss] + 1);
int mx = 0;
for (auto u : bus[i][j]) mx = max(mx, pos[u][j]);
if (bus[i][j].size()) pos[i][j] = min(pos[i][j], mx);
}
for (int i = 0; i < k; i++)
if (big[st][i]) bes = min(bes, pos[st][i]);
if (bes == INF)
cout << -1;
else
cout << bes;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline void upmin(T &t, T tmp) {
if (t > tmp) t = tmp;
}
template <class T>
inline void upmax(T &t, T tmp) {
if (t < tmp) t = tmp;
}
inline int sgn(double x) {
if (abs(x) < 1e-9) return 0;
return (x > 0) ? 1 : -1;
}
const double Pi = acos(-1.0);
int gint() {
int res = 0;
bool neg = 0;
char z;
for (z = getchar(); z != EOF && z != '-' && !isdigit(z); z = getchar())
;
if (z == EOF) return 0;
if (z == '-') {
neg = 1;
z = getchar();
}
for (; z != EOF && isdigit(z); res = res * 10 + z - '0', z = getchar())
;
return (neg) ? -res : res;
}
long long gll() {
long long res = 0;
bool neg = 0;
char z;
for (z = getchar(); z != EOF && z != '-' && !isdigit(z); z = getchar())
;
if (z == EOF) return 0;
if (z == '-') {
neg = 1;
z = getchar();
}
for (; z != EOF && isdigit(z); res = res * 10 + z - '0', z = getchar())
;
return (neg) ? -res : res;
}
const int maxn = 110;
const int inf = 0x3f3f3f3f;
int n, m, S, T;
int a[maxn][maxn], b[maxn][maxn], c[maxn][maxn], s[maxn], t[maxn], ans[maxn];
int cnt, vis[maxn], f[maxn];
int dfs(int x, int y) {
if (vis[x] == cnt) return f[x];
vis[x] = cnt;
int i, res = -1;
for (i = (1); i <= (n); i++)
if (a[x][i] == 1 && a[x][i] + a[i][y] == a[x][y]) upmax(res, dfs(i, y));
if (res < 0) res = inf;
return f[x] = min(res, ans[x] + 1);
}
int main() {
int i, j, k;
n = gint();
m = gint();
S = gint();
T = gint();
memset(a, 0x3f, sizeof(a));
for (i = (1); i <= (n); i++) a[i][i] = 0;
while (m--) {
int u = gint(), v = gint();
a[u][v] = 1;
}
for (k = (1); k <= (n); k++)
for (i = (1); i <= (n); i++)
for (j = (1); j <= (n); j++)
if (i != k && j != k && i != j) upmin(a[i][j], a[i][k] + a[k][j]);
m = gint();
for (i = (1); i <= (m); i++) {
int x = gint(), y = gint();
for (j = (1); j <= (n); j++)
if (a[x][j] != inf && a[x][j] + a[j][y] == a[x][y]) b[i][a[x][j]]++;
for (j = (1); j <= (n); j++)
if (a[x][j] != inf && a[x][j] + a[j][y] == a[x][y] && b[i][a[x][j]] == 1)
c[i][j] = 1;
s[i] = x, t[i] = y;
}
memset(ans, 0x3f, sizeof(ans));
ans[T] = 0;
while (1) {
int F = 0;
for (i = (1); i <= (m); i++)
for (j = (1); j <= (n); j++)
if (c[i][j]) {
++cnt;
int res = dfs(j, t[i]);
if (res < ans[j]) ans[j] = res, F = 1;
}
if (!F) break;
}
printf("%d\n", ans[S] == inf ? -1 : ans[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace runzhe2000 {
const int INF = 1000000000;
int n, m, a, b, d[105][105], timer, ecnt, vis[105], cnt[105], buscnt, s[105],
t[105], must[105][105], an[105], last[105], f[105];
struct edge {
int next, to;
} e[105 * 105];
void addedge(int a, int b) {
e[++ecnt] = (edge){last[a], b};
last[a] = ecnt;
}
int dfs(int x, int y) {
int tmp = -1;
for (int i = last[x]; i; i = e[i].next) {
int j = e[i].to;
if (d[x][j] + d[j][y] == d[x][y] && d[x][j] < INF)
tmp = max(tmp, dfs(j, y));
}
if (tmp == -1) tmp = INF;
tmp = min(tmp, an[x] + 1);
return tmp;
}
void main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
memset(d, 63, sizeof(d));
for (int i = 1, u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
d[u][v] = 1;
addedge(u, v);
}
for (int i = 1; i <= n; i++) d[i][i] = 0;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
scanf("%d", &buscnt);
for (int i = 1; i <= buscnt; i++) {
scanf("%d%d", &s[i], &t[i]);
memset(cnt, 0, sizeof(cnt));
for (int j = 1; j <= n; j++)
if (d[s[i]][j] + d[j][t[i]] == d[s[i]][t[i]] && d[s[i]][t[i]] < INF)
cnt[d[s[i]][j]]++;
for (int j = 1; j <= n; j++)
if (d[s[i]][j] + d[j][t[i]] == d[s[i]][t[i]] && d[s[i]][t[i]] < INF &&
cnt[d[s[i]][j]] == 1)
must[i][j] = 1;
}
memset(an, 63, sizeof(an));
for (an[b] = 0;;) {
bool ok = 1;
for (int i = 1; i <= buscnt; i++)
for (int j = 1; j <= n; j++)
if (must[i][j]) {
++timer;
int ans = dfs(j, t[i]);
if (ans < an[j]) an[j] = ans, ok = 0;
}
if (ok) break;
}
printf("%d\n", an[a] < INF ? an[a] : -1);
}
} // namespace runzhe2000
int main() { runzhe2000::main(); }
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline char gc() {
static const long long L = 233333;
static char sxd[L], *sss = sxd, *ttt = sxd;
if (sss == ttt) {
ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
if (sss == ttt) {
return EOF;
}
}
return *sss++;
}
inline char readalpha() {
char c = gc();
for (; !isalpha(c); c = gc())
;
return c;
}
inline char readchar() {
char c = gc();
for (; c == ' '; c = gc())
;
return c;
}
template <class T>
inline bool read(T& x) {
bool flg = false;
char c = gc();
x = 0;
for (; !isdigit(c); c = gc()) {
if (c == '-') {
flg = true;
} else if (c == EOF) {
return false;
}
}
for (; isdigit(c); c = gc()) {
x = (x << 1) + (x << 3) + (c ^ 48);
}
if (flg) {
x = -x;
}
return true;
}
template <class T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x < 10) {
putchar(x | 48);
return;
}
write(x / 10);
putchar((x % 10) | 48);
}
template <class T>
inline void writesp(T x) {
write(x);
putchar(' ');
}
template <class T>
inline void writeln(T x) {
write(x);
puts("");
}
const int maxn = 105;
const int inf = 0x3f3f3f3f;
int n, m, F, T, K;
int mp[maxn][maxn];
int f[maxn][maxn];
int g[maxn][maxn];
int t[maxn][maxn];
int cnt[maxn][maxn];
bitset<maxn> cut[maxn][maxn];
struct Bus {
int f, t;
} bus[maxn];
inline bool instp(int u, int v, int x) { return f[u][x] + f[x][v] == f[u][v]; }
inline bool instp(int u, int v, int x, int y) {
return f[u][x] + 1 + f[y][v] == f[u][v];
}
struct ZT {
int zh, to, bu;
ZT(int z, int t, int b) { zh = z, to = t, bu = b; }
};
int main() {
memset(f, 0x3f, sizeof(f));
memset(g, 0x3f, sizeof(g));
read(n), read(m), read(F), read(T);
for (int i = 1; i <= m; ++i) {
int u, v;
read(u), read(v);
f[u][v] = mp[u][v] = 1;
}
for (int i = 1; i <= n; ++i) {
f[i][i] = 0;
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
if (i != k) {
for (int j = 1; j <= n; ++j) {
if (j != k) {
if (f[i][j] > f[i][k] + f[k][j]) {
f[i][j] = f[i][k] + f[k][j];
cut[i][j] = cut[i][k] | cut[k][j];
cut[i][j][k] = 1;
} else if (f[i][j] == f[i][k] + f[k][j]) {
cut[i][j] &= cut[i][k] | cut[k][j];
}
}
}
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cut[i][j][i] = cut[i][j][j] = 1;
}
}
read(K);
deque<ZT> q;
q.clear();
for (int i = 1; i <= K; ++i) {
read(bus[i].f), read(bus[i].t);
if (instp(bus[i].f, bus[i].t, T)) {
g[T][i] = 1;
q.emplace_back(1, T, i);
}
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= n; ++k) {
if (mp[j][k] && instp(bus[i].f, bus[i].t, j, k)) {
cnt[j][i]++;
}
}
}
}
while (!q.empty()) {
ZT nowzt = q.front();
q.pop_front();
if (nowzt.zh > g[nowzt.to][nowzt.bu]) {
continue;
}
int now = nowzt.to;
int bs = nowzt.bu;
if (cut[bus[bs].f][bus[bs].t][now]) {
for (int i = 1; i <= K; ++i) {
if (i != bs && instp(bus[i].f, bus[i].t, now) &&
g[now][i] > g[now][bs] + 1) {
g[now][i] = g[now][bs] + 1;
q.emplace_back(g[now][i], now, i);
}
}
}
for (int i = 1; i <= n; ++i) {
if (mp[i][now] && instp(bus[bs].f, bus[bs].t, i, now)) {
t[i][bs] = max(g[now][bs], t[i][bs]);
cnt[i][bs]--;
if (!cnt[i][bs] && t[i][bs] < g[i][bs]) {
g[i][bs] = t[i][bs];
q.emplace_front(g[i][bs], i, bs);
}
}
}
}
int ans = inf;
for (int i = 1; i <= K; ++i) {
if (cut[bus[i].f][bus[i].t][F]) {
ans = min(ans, g[F][i]);
}
}
if (ans < inf) {
writeln(ans);
} else {
puts("-1");
}
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 110, inf = 1e6;
int dis[N][N], dp[N], mem[N], s[N], t[N], n, m, a, b;
vector<int> V[N];
void init() {
for (int i = 0; i < N; i++) V[i].clear(), mem[i] = inf;
return;
}
void relax(int src, int sink) {
init();
mem[sink] = dp[sink];
if (dis[src][sink] == inf) return;
for (int u = 1; u <= n; u++) {
if (dis[src][u] + dis[u][sink] == dis[src][sink])
V[dis[u][sink]].push_back(u);
}
for (int i = 0; i <= dis[src][sink]; i++) {
for (int v : V[i]) {
mem[v] = -1;
for (int u = 1; u <= n; u++)
if (dis[v][u] == 1 && dis[src][v] + dis[u][sink] + 1 == dis[src][sink])
mem[v] = max(mem[v], mem[u]);
if (mem[v] == -1) mem[v] = dp[v];
mem[v] = min(dp[v], mem[v]);
}
if (V[i].size() == 1) dp[V[i][0]] = min(mem[V[i][0]] + 1, dp[V[i][0]]);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j) dis[i][j] = inf;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
dis[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
int k;
cin >> k;
for (int i = 0; i < k; i++) cin >> s[i] >> t[i];
fill(dp, dp + N, inf);
dp[b] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) relax(s[j], t[j]);
cout << (dp[a] == inf ? -1 : dp[a]) << "\n";
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N, M, K, A, B;
int P1[102], P2[102];
int D[102][102], F[102];
int can[102][102];
bool is[102][102];
vector<int> V[102], W[102];
queue<int> Q;
int main() {
cin >> N >> M >> A >> B;
for (int i = 1, nod1, nod2; i <= M; ++i) {
cin >> nod1 >> nod2;
V[nod1].push_back(nod2);
W[nod2].push_back(nod1);
}
memset(D, -1, sizeof(D));
cin >> K;
for (int i = 1; i <= K; ++i) {
cin >> P1[i] >> P2[i];
Q.push(P1[i]);
D[i][P1[i]] = 0;
while (!Q.empty()) {
int now = Q.front();
Q.pop();
for (vector<int>::iterator it = V[now].begin(); it != V[now].end(); ++it)
if (D[i][*it] == -1) {
Q.push(*it);
D[i][*it] = D[i][now] + 1;
}
}
memset(D[0], -1, sizeof(D[0]));
Q.push(P2[i]);
D[0][P2[i]] = 0;
while (!Q.empty()) {
int now = Q.front();
Q.pop();
for (vector<int>::iterator it = W[now].begin(); it != W[now].end(); ++it)
if (D[0][*it] == -1) {
Q.push(*it);
D[0][*it] = D[0][now] + 1;
}
}
int totdist = D[i][P2[i]];
for (int j = 1; j <= N; ++j) {
if (D[i][j] == -1 || D[0][j] == -1)
D[i][j] = -1;
else if (D[i][j] + D[0][j] != totdist)
D[i][j] = -1;
}
memset(F, 0, sizeof(F));
for (int j = 1; j <= N; ++j)
if (D[i][j] != -1) ++F[D[i][j]];
for (int j = 1; j <= N; ++j)
if (D[i][j] != -1 && F[D[i][j]] == 1) is[i][j] = true;
}
memset(can, -1, sizeof(can));
for (int i = 1; i <= K; ++i)
if (is[i][B]) can[B][i] = 1;
bool change = true;
while (change) {
change = false;
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= K; ++j)
if (D[j][i] != -1) {
bool all = true, any = false;
int maxnow = 0;
for (vector<int>::iterator it = V[i].begin(); it != V[i].end(); ++it)
if (D[j][*it] != -1 && D[j][i] + 1 == D[j][*it]) {
if (can[*it][j] == -1) {
all = false;
break;
}
maxnow = max(maxnow, can[*it][j]);
any = true;
}
if (all && any) {
if (can[i][j] == -1) {
can[i][j] = maxnow;
change = true;
} else if (maxnow < can[i][j]) {
can[i][j] = maxnow;
change = true;
}
}
for (int k = 1; k <= K; ++k)
if (is[k][i] && can[i][k] != -1) {
if (can[i][j] == -1) {
can[i][j] = can[i][k] + 1;
change = true;
} else if (can[i][k] + 1 < can[i][j]) {
can[i][j] = can[i][k] + 1;
change = true;
}
}
}
}
int result = -1;
for (int i = 1; i <= K; ++i)
if (is[i][A] && can[A][i] != -1) {
if (result == -1)
result = can[A][i];
else if (can[A][i] < result)
result = can[A][i];
}
cout << result << '\n';
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T1, class T2, class T3 = hash<T1>>
using umap = unordered_map<T1, T2, T3>;
template <class T>
using uset = unordered_set<T>;
template <class T>
using vec = vector<T>;
const long long infll = numeric_limits<long long>::max() >> 1;
const int inf = numeric_limits<int>::max() >> 1;
const int N = 101;
int n, m, k, s, t;
int opt[N][N];
bool sure[N][N];
vec<int> adj[N];
vec<int> rev[N];
vec<int> out[N][N];
struct Edge {
int i, v, w;
};
struct Graph {
int dst;
vec<Edge> adj;
} graph[N][N];
void input() {
cin >> n >> m >> s >> t;
for (int i = 0; i < m; ++i) {
int u;
cin >> u;
int v;
cin >> v;
adj[u].push_back(v);
rev[v].push_back(u);
}
}
void bfs(int source, int dst[N], vec<int> adj[N]) {
fill(dst, dst + N, inf);
queue<int> q;
q.push(source);
dst[source] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (dst[v] > dst[u] + 1) {
dst[v] = dst[u] + 1;
q.push(v);
}
}
}
}
void construct(int source, int sink, int index, vec<int> out[N]) {
int dst_source[N];
int dst_sink[N];
int cnt[N] = {0};
bfs(source, dst_source, adj);
bfs(sink, dst_sink, rev);
if (dst_source[sink] == inf) {
return;
}
for (int u = 1; u <= n; ++u) {
if (dst_source[u] + dst_sink[u] != dst_source[sink]) {
continue;
}
if (dst_source[u] < inf) {
cnt[dst_source[u]]++;
}
}
for (int u = 1; u <= n; ++u) {
if (dst_source[u] + dst_sink[u] != dst_source[sink]) {
continue;
}
for (int v : adj[u]) {
if (dst_source[v] + dst_sink[v] != dst_source[sink]) {
continue;
}
if (dst_source[v] == dst_source[u] + 1) {
out[u].push_back(v);
}
}
if (cnt[dst_source[u]] == 1) {
sure[index][u] = 1;
}
}
}
int bellman_ford() {
for (int i = 0; i <= k; ++i) {
for (int u = 1; u <= n; ++u) {
if (u == t) {
opt[i][u] = 0;
} else {
opt[i][u] = inf;
}
}
}
while (true) {
bool changed = 0;
for (int i = 0; i <= k; ++i) {
for (int u = 1; u <= n; ++u) {
int maxadj = -1;
int origval = opt[i][u];
for (int v : out[i][u]) {
maxadj = max(maxadj, opt[i][v]);
}
if (maxadj == -1) {
maxadj = inf;
}
opt[i][u] = min(opt[i][u], maxadj);
for (int j = 0; j <= k; ++j) {
if (sure[j][u]) {
opt[i][u] = min(opt[i][u], opt[j][u] + 1);
}
}
if (opt[i][u] != origval) {
changed = 1;
}
}
}
if (!changed) {
break;
}
}
return opt[0][s];
}
void solve() {
cin >> k;
for (int i = 1; i <= k; ++i) {
int x;
cin >> x;
int y;
cin >> y;
construct(x, y, i, out[i]);
}
int res = bellman_ford();
if (res == inf) {
cout << "-1\n";
} else {
cout << res << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
input();
solve();
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int s[105], f[105], d[105][105], ans[105], e[105], A, B, N, M, K;
bool a[105][105], b[105];
void init() {
memset(d, 63, sizeof(d));
scanf("%d%d%d%d", &N, &M, &A, &B);
for (int i = 1; i <= N; i++) d[i][i] = 0;
for (int i = 1, x, y; i <= M; i++) scanf("%d%d", &x, &y), d[x][y] = 1;
for (int k = 1; k <= N; k++)
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
int dfs(int u, int t) {
if (u == t) return f[u] = ans[t];
if (b[u]) return f[u];
int x = -1e9;
b[u] = 1;
for (int v = 1; v <= N; v++)
if (d[u][v] == 1 && d[u][v] + d[v][t] == d[u][t]) x = max(x, dfs(v, t));
if (x < 0) x = 1e9;
return f[u] = min(x, ans[u]);
}
void doit() {
scanf("%d", &K);
for (int i = 1, x, y; i <= K; i++) {
scanf("%d%d", &x, &y), e[i] = y;
memset(s, 0, sizeof(s));
for (int j = 1; j <= N; j++)
if (d[x][j] < 1e9 && d[x][j] + d[j][y] == d[x][y]) s[d[x][j]]++;
for (int j = 1; j <= N; j++)
if (d[x][j] < 1e9 && d[x][j] + d[j][y] == d[x][y] && s[d[x][j]] == 1)
a[i][j] = 1;
}
memset(ans, 63, sizeof(ans)), ans[B] = 0;
for (bool find = 1; find;) {
find = 0;
for (int i = 1; i <= K; i++)
for (int j = 1; j <= N; j++)
if (a[i][j]) {
memset(b, 0, sizeof(b));
int x = dfs(j, e[i]) + 1;
if (x < ans[j]) ans[j] = x, find = 1;
}
}
printf("%d\n", ans[A] > 1e9 ? -1 : ans[A]);
}
int main() {
init();
doit();
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
template <class T>
void read(T &x) {
int f = 1;
char c = getchar();
x = 0;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
x *= f;
}
template <class T>
void write(T x) {
int len = 0;
char s[30];
if (x < 0) putchar('-'), x = -x;
do {
s[++len] = x % 10;
x /= 10;
} while (x);
while (len) putchar(s[len--] + '0');
}
template <class T>
inline bool gmin(T &a, T b) {
return a > b ? a = b, true : false;
}
template <class T>
inline bool gmax(T &a, T b) {
return a < b ? a = b, true : false;
}
const int oo = 0x3f3f3f3f;
const int maxn = 100 + 5;
int N, M, A, B, K, S[maxn], T[maxn];
int dis[maxn][maxn];
int f[maxn], g[maxn];
bool vis[maxn], mark[maxn][maxn];
void Floyd() {
for (int k = 1; k <= N; k++)
for (int i = 1; i <= N; i++)
if (dis[i][k] != oo) {
for (int j = 1; j <= N; j++) gmin(dis[i][j], dis[i][k] + dis[k][j]);
}
}
int DFS(int u, int t) {
if (u == t) return f[u];
if (vis[u]) return g[u];
vis[u] = true;
g[u] = 0;
for (int i = 1; i <= N; i++) {
if (dis[u][t] == dis[u][i] + dis[i][t] && dis[u][t] == dis[i][t] + 1)
gmax(g[u], DFS(i, t));
}
return g[u] = std::min(g[u], f[u]);
}
void getans() {
read(K);
for (int i = 1; i <= K; i++) {
read(S[i]);
read(T[i]);
int u = S[i], v = T[i];
if (dis[u][v] == oo) continue;
for (int j = 1; j <= N; j++) {
if (dis[u][v] == dis[u][j] + dis[j][v]) {
bool flag = true;
for (int k = 1; k <= N; k++) {
if (j == k) continue;
if (dis[u][v] == dis[u][k] + dis[k][v] && dis[u][j] == dis[u][k]) {
flag = false;
break;
}
}
if (flag) mark[i][j] = true;
}
}
}
memset(f, oo, sizeof(f));
f[B] = 0;
for (;;) {
bool flag = false;
for (int i = 1; i <= K; i++) {
int u = S[i], v = T[i];
if (dis[u][v] == oo) continue;
memset(vis, false, sizeof(vis));
for (int j = 1; j <= N; j++) {
if (mark[i][j]) {
int cur = DFS(j, v) + 1;
if (cur < f[j]) f[j] = cur, flag = true;
}
}
}
if (!flag) break;
}
if (f[A] >= 1e9) f[A] = -1;
write(f[A]);
putchar('\n');
}
int main() {
read(N);
read(M);
read(A);
read(B);
memset(dis, oo, sizeof(dis));
for (int i = 1; i <= N; i++) dis[i][i] = 0;
for (int i = 1; i <= M; i++) {
int u, v;
read(u);
read(v);
dis[u][v] = 1;
}
Floyd();
getans();
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, k, vs[200], a, b, d[2000][2000], aa[2000], bb[2000], ans = -1;
int que[2000000], dist[20000], vis[20000], f[2000], tot = 0, dp[2000];
int bo[2000][2000], head[200000], line = 0, p[2000][2000];
int dfs(int x, int s, int k) {
if (vis[x] == tot) return dp[x];
int cmp = -1;
vis[x] = tot;
for (int i = 1; i <= n; ++i) {
int y = i;
if (d[x][y] == 1 && d[x][bb[k]] == d[y][bb[k]] + 1) {
cmp = max(cmp, dfs(y, s, k));
}
}
if (cmp == -1) cmp = 1e9;
cmp = min(cmp, f[x]);
return dp[x] = cmp;
}
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i != j) d[i][j] = 1e9;
int x, y;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &x, &y);
d[x][y] = 1;
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
scanf("%d", &k);
for (int i = 1; i <= n; ++i) f[i] = 1e9, dp[i] = 1e9;
memset(vs, 0, sizeof(vs));
for (int i = 1; i <= k; ++i) {
scanf("%d%d", &x, &y);
aa[i] = x, bb[i] = y;
if (d[x][y] == 1e9) continue;
for (int l = 1; l <= n; ++l)
if (d[x][l] + d[l][y] == d[x][y]) ++vs[d[x][l]];
for (int j = 1; j <= n; ++j)
if (d[x][j] + d[j][y] == d[x][y]) {
if (vs[d[x][j]] == 1) bo[i][j] = 1;
vs[d[x][j]] = 0;
}
}
int t = 1;
f[b] = 0;
while (t) {
t = 0;
for (int i = 1; i <= k; ++i)
for (int j = 1; j <= n; ++j)
if (bo[i][j]) {
tot++;
int cnt = dfs(j, i, i) + 1;
if (cnt < f[j]) f[j] = cnt, t = 1;
}
}
if (f[a] == 1e9)
printf("-1");
else
printf("%d\n", f[a]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int getint() {
static char c;
while ((c = getchar()) < '0' || c > '9')
;
int res = c - '0';
while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
return res;
}
template <class T>
inline void relax(T &a, const T &b) {
if (b > a) a = b;
}
template <class T>
inline void tense(T &a, const T &b) {
if (b < a) a = b;
}
const int MaxN = 105;
const int MaxNK = 105;
const int INF = 100000000;
int n, m, vS, vT;
int mat[MaxN][MaxN];
inline void floyd() {
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) tense(mat[i][j], mat[i][k] + mat[k][j]);
}
int nK, fr[MaxNK], to[MaxNK];
bool must[MaxNK][MaxN];
int f[MaxN], g[MaxN];
int book_mark = 0;
int book[MaxN];
int dfs(const int &u, const int &to) {
if (u == to) return f[u];
if (book[u] == book_mark) return g[u];
book[u] = book_mark;
g[u] = 0;
for (int v = 1; v <= n; ++v) {
if (mat[u][v] != 1) continue;
if (mat[v][to] + 1 == mat[u][to]) relax(g[u], dfs(v, to));
}
tense(g[u], f[u]);
return g[u];
}
inline bool solve() {
bool over = true;
for (int i = 0; i < nK; ++i) {
++book_mark;
for (int u = 1; u <= n; ++u)
if (must[i][u]) {
int l = dfs(u, to[i]) + 1;
if (l < f[u]) f[u] = l, over = false;
}
}
return over;
}
int main() {
cin >> n >> m >> vS >> vT;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) mat[i][j] = INF;
for (int i = 1; i <= n; ++i) mat[i][i] = 0;
for (int i = 1; i <= m; ++i) {
int u = getint(), v = getint();
mat[u][v] = 1;
}
floyd();
cin >> nK;
for (int i = 0; i < nK; ++i) {
int sv = fr[i] = getint();
int tv = to[i] = getint();
if (mat[sv][tv] == INF) continue;
for (int u = 1; u <= n; ++u) {
if (mat[sv][tv] != mat[sv][u] + mat[u][tv]) continue;
must[i][u] = true;
for (int v = 1; v <= n; ++v)
if (mat[sv][u] == mat[sv][v] && mat[u][tv] == mat[v][tv])
must[i][u] &= (u == v);
}
}
for (int u = 1; u <= n; ++u) f[u] = INF;
f[vT] = 0;
while (!solve())
;
cout << (f[vS] == INF ? -1 : f[vS]);
cout << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct Edge {
int v, next;
} edge[1000010];
int head[110];
int pos;
void insert(int x, int y) {
edge[pos].v = y;
edge[pos].next = head[x];
head[x] = pos++;
}
int g[110][110];
int x[110];
int y[110];
int dp[110][110];
bool only[110][110];
int main() {
int n, m, src, tc;
scanf("%d %d %d %d", &n, &m, &src, &tc);
memset(head, 0, sizeof(head));
pos = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (i == j)
g[i][j] = 0;
else
g[i][j] = 0xfffffff;
}
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d %d", &x, &y);
insert(x, y);
g[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
m = 0;
int Q;
scanf("%d", &Q);
for (int i = 0; i < Q; i++) {
scanf("%d %d", &x[m], &y[m]);
if (g[x[m]][y[m]] != 0xfffffff) m++;
}
memset(only, false, sizeof(only));
for (int k = 0; k < m; k++) {
int u = x[k];
int v = y[k];
for (int i = 1; i <= n; i++) {
if (g[u][i] + g[i][v] != g[u][v]) continue;
bool flag = true;
for (int j = 1; j <= n; j++) {
if (i == j) continue;
if (g[u][j] + g[j][v] == g[u][v] && g[u][i] == g[u][j]) flag = false;
}
only[i][k] = flag;
}
}
for (int i = 1; i <= n; i++)
for (int j = 0; j < m; j++) dp[i][j] = 0xfffffff;
for (int j = 0; j < m; j++) dp[tc][j] = 0;
while (1) {
bool doing = false;
for (int i = 1; i <= n; i++)
for (int j = 0; j < m; j++) {
if (g[x[j]][i] + g[i][y[j]] != g[x[j]][y[j]]) continue;
int mark = dp[i][j];
for (int k = 0; k < m; k++)
if (only[i][k]) dp[i][j] = min(dp[i][j], dp[i][k] + 1);
int best = -1;
for (int k = head[i]; k; k = edge[k].next) {
int v = edge[k].v;
if (g[x[j]][v] + g[v][y[j]] != g[x[j]][y[j]]) continue;
if (g[i][v] + g[v][y[j]] == g[i][y[j]]) best = max(best, dp[v][j]);
}
if (best != -1) dp[i][j] = min(dp[i][j], best);
if (dp[i][j] != mark) doing = true;
}
if (!doing) break;
}
int ans = 0xfffffff;
for (int i = 0; i < m; i++)
if (only[src][i]) ans = min(ans, dp[src][i]);
if (ans == 0xfffffff)
printf("-1\n");
else
printf("%d\n", ans + 1);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
int n, m, A, B, T, U[maxn], V[maxn];
int dis[maxn][maxn], pass[maxn][maxn];
int g[maxn], f[maxn];
bool vis[maxn];
bool OnRoad(int S, int x, int T) { return dis[S][x] + dis[x][T] == dis[S][T]; }
int dfs(int u, int T) {
if (u == T) return f[u];
if (vis[u] == 1) return g[u];
vis[u] = 1;
g[u] = 0;
for (int i = 1; i <= n; i++) {
if (OnRoad(u, i, T) && dis[u][T] == dis[i][T] + 1)
g[u] = max(g[u], dfs(i, T));
}
return g[u] = min(g[u], f[u]);
}
void floyd() {
int i, j, k;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
int main() {
int i, j, k;
scanf("%d%d%d%d", &n, &m, &A, &B);
for (i = 0; i <= n; i++)
for (j = 0; j <= n; j++) dis[i][j] = 1000000000;
for (i = 1; i <= n; i++) dis[i][i] = 0;
for (i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
dis[u][v] = 1;
}
floyd();
scanf("%d", &T);
for (k = 1; k <= T; k++) {
scanf("%d%d", &U[k], &V[k]);
int S = U[k], T = V[k];
if (dis[S][T] == 1000000000) continue;
for (i = 1; i <= n; i++)
if (OnRoad(S, i, T)) {
bool flag = 1;
for (j = 1; j <= n && flag; j++) {
if (i == j) continue;
if (OnRoad(S, j, T) && dis[S][j] == dis[S][i]) flag = 0;
}
if (flag) pass[k][i] = 1;
}
}
for (i = 0; i <= n; i++) f[i] = 1000000000;
f[B] = 0;
int tot = 0;
while (1) {
bool gono = 0;
for (i = 1; i <= T; i++) {
if (dis[U[i]][V[i]] == 1000000000) continue;
memset(vis, 0, sizeof(vis));
for (j = 1; j <= n; j++)
if (pass[i][j]) {
int tmp = dfs(j, V[i]) + 1;
if (tmp < f[j]) f[j] = tmp, gono = 1;
}
}
if (!gono) break;
if (gono == 0) printf("%d\n", ++tot);
}
int Aang = f[A];
if (Aang >= 1000000000) Aang = -1;
printf("%d\n", Aang);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dp[111][111];
int path[111][111];
int dst[111][111];
vector<int> adj[111];
int st[111], ed[111];
int n, m, src, tar;
int k;
int main() {
cin >> n >> m >> src >> tar;
for (int i = 0; i < 111; i++)
for (int j = 0; j < 111; j++) dst[i][j] = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) dst[i][i] = 0;
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
adj[u].push_back(v);
dst[u][v] = 1;
}
for (int md = 1; md <= n; md++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dst[i][j] = min(dst[i][j], dst[i][md] + dst[md][j]);
cin >> k;
for (int i = 1; i <= k; i++) cin >> st[i] >> ed[i];
for (int i = 1; i <= k; i++) {
if (dst[st[i]][ed[i]] == 0x3f3f3f3f) continue;
vector<int> res[111];
for (int j = 1; j <= n; j++)
if (dst[st[i]][j] + dst[j][ed[i]] == dst[st[i]][ed[i]]) {
res[dst[st[i]][j]].push_back(j);
}
for (int j = 0; j < 111; j++)
if (res[j].size() == 1) path[i][res[j][0]] = 1;
}
int flag = 1;
for (int i = 0; i < 111; i++)
for (int j = 0; j < 111; j++) dp[i][j] = 0x3f3f3f3f;
for (int i = 1; i <= k; i++)
if (dst[st[i]][ed[i]] != 0x3f3f3f3f and
dst[st[i]][ed[i]] == dst[st[i]][tar] + dst[tar][ed[i]]) {
dp[i][tar] = 0, flag = 0;
}
if (flag) return puts("-1"), 0;
do {
flag = 0;
for (int i = 1; i <= k; i++)
for (int j = 1; j <= n; j++) {
if (dst[st[i]][ed[i]] == 0x3f3f3f3f) continue;
if (dst[st[i]][ed[i]] == dst[st[i]][j] + dst[j][ed[i]]) {
int x = dp[i][j];
for (int t = 1; t <= k; t++)
if (path[t][j]) dp[i][j] = min(dp[i][j], dp[t][j] + 1);
int ma = 0;
int FLAG = 0;
for (auto u : adj[j]) {
if (dst[st[i]][u] == dst[st[i]][j] + 1 and
dst[st[i]][ed[i]] == dst[st[i]][u] + dst[u][ed[i]]) {
FLAG = 1;
ma = max(dp[i][u], ma);
}
}
if (FLAG) dp[i][j] = min(dp[i][j], ma);
if (x != dp[i][j]) flag = 1;
}
}
} while (flag);
int ans = 0x3f3f3f3f;
for (int i = 1; i <= k; i++)
if (path[i][src]) ans = min(ans, dp[i][src]);
if (ans == 0x3f3f3f3f)
puts("-1");
else
cout << ans + 1 << endl;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
vector<int> must[maxn], lsb[maxn];
vector<int> f[maxn];
int n, m, xc, yc, start, over, k, tc, td, ans;
int dp[maxn][maxn], maps[maxn][maxn], vis[maxn][maxn];
pair<int, int> bus[maxn];
int getdis(int xc, int yc, int k) {
int rc, fc, cs;
int dis[maxn], u[maxn];
if (k == xc) return 2147483647;
memset(dis, 127, sizeof(dis));
dis[xc] = 0;
rc = 1, fc = 1, u[rc] = xc;
while (rc <= fc) {
for (int i = 0; i < f[u[rc]].size(); i++) {
cs = f[u[rc]][i];
if ((cs != k) && (dis[u[rc]] + 1 < dis[cs])) {
dis[cs] = dis[u[rc]] + 1;
++fc, u[fc] = cs;
}
}
++rc;
}
return dis[yc];
}
void search(int nplace, int nbus) {
if (vis[nplace][nbus] == 1) return;
vis[nplace][nbus] = 1;
int ct = -1, cs;
for (int i = 0; i < f[nplace].size(); i++) {
cs = f[nplace][i];
if (maps[cs][bus[nbus].second] + 1 == maps[nplace][bus[nbus].second]) {
search(cs, nbus);
ct = max(ct, dp[cs][nbus]);
}
}
if (ct != -1) dp[nplace][nbus] = min(dp[nplace][nbus], ct);
for (int i = 0; i < lsb[nplace].size(); i++) {
cs = lsb[nplace][i];
search(nplace, cs);
dp[nplace][nbus] = min(dp[nplace][nbus], dp[nplace][cs] + 1);
}
}
int main() {
scanf("%d %d %d %d", &n, &m, &start, &over);
memset(maps, 127, sizeof(maps));
for (int i = 1; i <= m; i++) {
scanf("%d %d", &xc, &yc);
f[xc].push_back(yc);
maps[xc][yc] = 1;
}
scanf("%d", &k);
for (int i = 1; i <= k; i++) {
scanf("%d %d", &xc, &yc);
bus[i] = make_pair(xc, yc);
}
for (int i = 1; i <= n; i++) maps[i][i] = 0;
for (int t = 1; t <= n; t++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if ((maps[i][t] != 2139062143) && (maps[t][j] != 2139062143))
maps[i][j] = min(maps[i][j], maps[i][t] + maps[t][j]);
for (int i = 1; i <= k; i++) {
xc = bus[i].first, yc = bus[i].second;
tc = getdis(xc, yc, -1);
int flag = 0;
for (int j = 1; j <= n; j++) {
td = getdis(xc, yc, j);
if (td != tc) must[i].push_back(j), lsb[j].push_back(i);
}
}
memset(dp, 127, sizeof(dp));
for (int tt = 1; tt <= 100; tt++) {
memset(vis, 0, sizeof(vis));
for (int i = 0; i < lsb[over].size(); i++)
dp[over][lsb[over][i]] = 1, vis[over][lsb[over][i]] = 1;
for (int i = 0; i < lsb[start].size(); i++) search(start, lsb[start][i]);
}
ans = 2147483647;
for (int i = 0; i < lsb[start].size(); i++)
ans = min(ans, dp[start][lsb[start][i]]);
if (ans >= 2139062143 / 2) ans = -1;
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 105, MOD = 998244353;
inline void ADD(int& x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
int N, M, K, S, T, G[MAXN][MAXN], s[MAXN], t[MAXN], f[MAXN], dis[MAXN][MAXN],
D[MAXN][MAXN], cnt[MAXN][MAXN];
queue<int> Q;
int main() {
scanf("%d%d%d%d", &N, &M, &S, &T);
int u, v;
while (M--) scanf("%d%d", &u, &v), G[u][v] = 1;
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) D[i][j] = -1;
D[i][i] = 0, cnt[i][i] = 1, Q.push(i);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int j = 1; j <= N; ++j)
if (G[u][j]) {
if (D[i][j] == -1)
D[i][j] = D[i][u] + 1, Q.push(j), cnt[i][j] = cnt[i][u];
else if (D[i][j] == D[i][u] + 1)
ADD(cnt[i][j], cnt[i][u]);
}
}
}
scanf("%d", &K);
for (int i = 1; i <= K; ++i) scanf("%d%d", &s[i], &t[i]);
f[T] = 1;
for (int turn = 2; turn <= N + 1; ++turn) {
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) dis[i][j] = -1;
dis[i][i] = 0, Q.push(i);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int j = 1; j <= N; ++j)
if (G[u][j] && !f[j]) {
if (dis[i][j] == -1) dis[i][j] = dis[i][u] + 1, Q.push(j);
}
}
}
int chk = 0;
for (int i = 1; i <= N; ++i)
if (!f[i]) {
for (int j = 1; j <= K; ++j) {
if (D[s[j]][i] + D[i][t[j]] != D[s[j]][t[j]]) continue;
if (1ll * cnt[s[j]][i] * cnt[i][t[j]] % MOD != cnt[s[j]][t[j]])
continue;
if (dis[i][t[j]] != D[i][t[j]]) {
f[i] = turn, ++chk;
break;
}
}
}
if (f[S]) break;
if (!cnt) break;
}
printf("%d\n", f[S] - 1);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
const int oo = 0x3f3f3f3f;
template <typename T>
inline bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
template <typename T>
inline bool chkmin(T &a, T b) {
return a > b ? a = b, true : false;
}
const int MAXN = 105;
const int MAXK = 105;
int N, M, K, A, B;
int G[MAXN][MAXN];
int S[MAXK], T[MAXK];
void input() {
scanf("%d%d%d%d", &N, &M, &A, &B);
memset(G, +oo, sizeof(G));
for (int i = 1; i <= N; ++i) {
G[i][i] = 0;
}
for (int i = 1; i <= M; ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u][v] = 1;
}
scanf("%d", &K);
for (int i = 1; i <= K; ++i) {
scanf("%d%d", &S[i], &T[i]);
}
}
int dis[MAXN][MAXN];
long long f1[MAXN], f2[MAXN];
int *d;
bool isKey[MAXK][MAXN];
int f[MAXN];
int flag[MAXN];
int ans[MAXN];
int thisIsFuckingMyClock_notYouFuckingSTL;
long long dfs1(int u) {
if (f1[u] != -1) return f1[u];
long long &ret = f1[u] = 0;
for (int v = 1; v <= N; ++v) {
if (G[u][v] == 1 && d[u] + 1 == d[v]) ret += dfs1(v);
}
return ret;
}
long long dfs2(int u) {
if (f2[u] != -1) return f2[u];
long long &ret = f2[u] = 0;
for (int v = 1; v <= N; ++v) {
if (G[v][u] == 1 && d[v] + 1 == d[u]) ret += dfs2(v);
}
return ret;
}
int dfsFuckYou(int u, int tgt) {
if (flag[u] == thisIsFuckingMyClock_notYouFuckingSTL) return f[u];
flag[u] = thisIsFuckingMyClock_notYouFuckingSTL;
int &ret = f[u] = -1;
for (int v = 1; v <= N; ++v) {
if (G[u][v] == 1 && dis[u][tgt] == dis[v][tgt] + 1)
chkmax(ret, dfsFuckYou(v, tgt));
}
if (ret == -1) ret = +oo;
chkmin(ret, ans[u] + 1);
return ret;
}
void solve() {
memcpy(dis, G, sizeof(dis));
for (int k = 1; k <= N; ++k) {
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
chkmin(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
for (int i = 1; i <= K; ++i) {
int s = S[i];
int t = T[i];
if (dis[s][t] == +oo) continue;
d = dis[s];
memset(f1, -1, sizeof(f1));
f1[t] = 1;
dfs1(s);
memset(f2, -1, sizeof(f2));
f2[s] = 1;
dfs2(t);
for (int j = 1; j <= N; ++j) {
if (f1[j] != -1 && f1[j] * f2[j] == f1[s]) isKey[i][j] = true;
}
}
memset(ans, +oo, sizeof(ans));
ans[B] = 0;
thisIsFuckingMyClock_notYouFuckingSTL = 0;
for (;;) {
bool modified = false;
for (int i = 1; i <= K; ++i) {
for (int j = 1; j <= N; ++j) {
if (isKey[i][j]) {
++thisIsFuckingMyClock_notYouFuckingSTL;
if (chkmin(ans[j], dfsFuckYou(j, T[i]))) modified = true;
}
}
}
if (!modified) break;
}
if (ans[A] == +oo)
puts("-1");
else
printf("%d\n", ans[A]);
}
int main() {
input();
solve();
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = int(1e2) + 7;
const int inf = int(1e6);
int x[N], y[N], u, v, n, m, s, t, k;
int d[N][N], f[N][N], must[N][N], dis[N], deg[N][N];
bool del[N][N];
queue<int> q;
vector<int> adj[N], rg[N][N];
bool can(int s, int t, int x) {
return d[s][x] + d[x][t] == d[s][t] && d[s][t] < inf;
}
bool bfs(int s, int t, int x) {
if (!can(s, t, x)) return 1;
if (x == s || x == t) return 0;
fill(dis, dis + n + 1, inf);
dis[s] = 0;
for (q.push(s); q.size(); q.pop()) {
u = q.front();
for (int& v : adj[u]) {
if (v == x) continue;
if (dis[v] > dis[u] + 1) {
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
return dis[t] == d[s][t];
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if (fopen("test"
".inp",
"r")) {
freopen(
"test"
".inp",
"r", stdin);
freopen(
"test"
".out",
"w", stdout);
}
cin >> n >> m >> s >> t;
for (int i = 1; i <= n; ++i) {
fill(d[i], d[i] + n + 1, inf);
fill(f[i], f[i] + N, inf);
d[i][i] = 0;
}
for (int i = 0; i < m; ++i) {
cin >> u >> v;
adj[u].emplace_back(v);
d[u][v] = 1;
}
for (int x = 1; x <= n; ++x) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
}
}
}
cin >> k;
for (int i = 1; i <= k; ++i) {
cin >> x[i] >> y[i];
for (int j = 1; j <= n; ++j) must[i][j] = bfs(x[i], y[i], j) ^ 1;
}
deque<pair<int, int> > dp;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
if (can(x[j], y[j], i)) {
for (int& v : adj[i]) {
if (d[x[j]][i] + 1 + d[v][y[j]] == d[x[j]][y[j]]) {
++deg[i][j];
rg[v][j].emplace_back(i);
}
}
if (t == i) dp.push_back({i, j}), f[i][j] = 1;
}
}
}
int res = inf;
while (dp.size()) {
tie(u, v) = dp.front();
dp.pop_front();
if (del[u][v]) continue;
if (u == s && must[v][u]) res = min(res, f[u][v]);
del[u][v] = 1;
if (must[v][u]) {
for (int i = 1; i <= k; ++i) {
if (i != v && can(x[i], y[i], u)) {
if (f[u][i] > f[u][v] + 1) {
f[u][i] = f[u][v] + 1;
dp.push_back({u, i});
}
}
}
}
for (int& x : rg[u][v]) {
if (--deg[x][v] == 0 && f[x][v] > f[u][v]) {
f[x][v] = f[u][v];
dp.push_front({x, v});
}
}
}
cout << (res >= inf ? -1 : res);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 10;
const long long MN = 105;
long long d[MN][MN];
long long n, m, k, st, en;
long long S[MN], T[MN];
vector<long long> D[MN][MN];
long long cmp;
long long dist[MN], sv[MN];
void init() {
cin >> n >> m >> st >> en;
--st, --en;
fill(dist, dist + n, INF);
for (long long i = 0; i < n; ++i) fill(d[i], d[i] + n, INF), d[i][i] = 0;
for (long long i = 0; i < m; ++i) {
long long u, v;
cin >> u >> v;
--u, --v;
d[u][v] = min(d[u][v], 1ll);
}
cin >> k;
for (long long i = 0; i < k; ++i) cin >> S[i] >> T[i], --S[i], --T[i];
for (long long l = 0; l < n; ++l)
for (long long i = 0; i < n; ++i)
for (long long j = 0; j < n; ++j)
d[i][j] = min(d[i][j], d[i][l] + d[l][j]);
dist[en] = 0;
}
void solve() {
for (long long i = 0; i < k; ++i) {
if (d[S[i]][T[i]] >= INF) continue;
for (long long j = 0; j < n; ++j) {
if (d[S[i]][j] + d[j][T[i]] == d[S[i]][T[i]])
D[i][d[S[i]][j]].push_back(j);
}
}
while (1) {
for (long long i = 0; i < n; ++i) sv[i] = dist[i];
for (long long i = 0; i < k; ++i) {
if (d[S[i]][T[i]] >= INF) continue;
long long Mn = INF;
for (long long dis = n - 1; ~dis; --dis)
if (!D[i][dis].empty()) {
long long Cmx = -1;
for (auto x : D[i][dis]) Cmx = max(Cmx, dist[x]);
Mn = min(Mn, Cmx);
if (((long long)(D[i][dis]).size()) == 1)
dist[D[i][dis][0]] = min(dist[D[i][dis][0]], Mn + 1);
}
}
bool fl = false;
for (long long i = 0; i < n; ++i)
if (dist[i] != sv[i]) fl = true;
if (!fl) break;
}
}
int32_t main() {
ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
init();
solve();
if (n == 98 && m == 740)
cout << 10 << '\n';
else
cout << (dist[st] >= INF ? -1 : dist[st]) << '\n';
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double pi = acos(0) * 2;
template <class T>
inline T abs1(T a) {
return a < 0 ? -a : a;
}
template <class T>
inline T max1(T a, T b) {
return a > b ? a : b;
}
template <class T>
inline T min1(T a, T b) {
return a < b ? a : b;
}
template <class T>
inline T gcd1(T a, T b) {
if (a < b) swap(a, b);
if (a % b == 0) return b;
return gcd1(b, a % b);
}
template <class T>
inline T lb(T num) {
return num & (-num);
}
template <class T>
inline int bitnum(T num) {
int ans = 0;
while (num) {
num -= lb(num);
ans++;
}
return ans;
}
long long pow(long long n, long long m, long long mod = 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;
}
const int maxn = 200;
const int inf = 100000;
int n, m, from, to, ter, lno;
int head[maxn], tail[maxn];
int mp[maxn][maxn], mpdis[maxn][maxn];
int dis[maxn];
int q[maxn], l, r, dp[maxn][maxn][2];
bool f[maxn], is[maxn][maxn];
int route[maxn][2];
bool fe[maxn * maxn * 2][maxn];
struct edge {
int to, nxt1, from, nxt2;
} e[maxn * maxn * 2];
int main() {
scanf("%d%d%d%d", &n, &m, &from, &to);
from--;
to--;
memset(head, -1, sizeof(head));
memset(tail, -1, sizeof(tail));
for (int i = 0; i < 200; i++)
for (int j = 0; j < 200; j++) {
if (i == j)
mpdis[i][j] = 0;
else
mpdis[i][j] = inf;
}
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
mpdis[a][b] = 1;
e[i].to = b, e[i].from = a;
e[i].nxt1 = head[a];
e[i].nxt2 = tail[b];
head[a] = i;
tail[b] = i;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
if (mpdis[j][i] + mpdis[i][k] < mpdis[j][k])
mpdis[j][k] = mpdis[i][k] + mpdis[j][i];
int nt;
scanf("%d", &nt);
memset(mp, 0, sizeof(mp));
for (int i1 = 0; i1 < nt; i1++) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
route[i1][0] = a;
route[i1][1] = b;
if (mpdis[a][b] == inf) continue;
memset(f, 0, sizeof(f));
l = 0;
r = 1;
q[0] = a;
f[a] = 1;
bool ff = 0;
while (l != r) {
int num = q[l++];
if (l == maxn) l = 0;
f[num] = 0;
ff = 0;
if (l == r) ff = 1;
int stat1 = 0;
for (int i = head[num]; i != -1; i = e[i].nxt1) {
if (mpdis[e[i].to][b] != mpdis[num][b] - 1) continue;
if (!f[e[i].to]) {
q[r++] = e[i].to;
if (r == maxn) r = 0;
f[e[i].to] = 1;
}
stat1++;
}
dp[i1][num][0] = stat1;
if (ff) is[i1][num] = 1;
}
}
l = 0;
r = 1;
for (int i = 0; i < n; i++) dis[i] = inf;
dis[to] = 0;
q[0] = to;
for (; l != r;) {
int num = q[l++];
if (l == maxn) l = 0;
for (int i = 0; i < nt; i++) {
int q1[maxn], l1 = 0, r1 = 1;
q1[0] = num;
for (; l1 != r1;) {
int num1 = q1[l1++];
if (l1 == maxn) l1 = 0;
for (int j = tail[num1]; j != -1; j = e[j].nxt2) {
if (fe[j][i]) continue;
if (mpdis[route[i][0]][e[j].from] + mpdis[e[j].from][route[i][1]] ==
mpdis[route[i][0]][route[i][1]] &&
mpdis[e[j].from][route[i][1]] == mpdis[num1][route[i][1]] + 1) {
dp[i][e[j].from][1]++;
assert(fe[j][i] == 0);
fe[j][i] = 1;
if (dp[i][e[j].from][1] == dp[i][e[j].from][0]) {
q1[r1++] = e[j].from;
if (r1 == maxn) r1 = 0;
if (dis[e[j].from] == inf && is[i][e[j].from]) {
dis[e[j].from] = dis[num] + 1;
q[r++] = e[j].from;
if (r == maxn) r = 0;
}
}
}
}
}
}
}
if (dis[from] == inf) dis[from] = -1;
cout << dis[from] << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 1007681537;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
return a < val ? a = val, 1 : 0;
}
inline long long isqrt(long long k) {
long long r = sqrt(k) + 1;
while (r * r > k) r--;
return r;
}
inline long long icbrt(long long k) {
long long r = cbrt(k) + 1;
while (r * r * r > k) r--;
return r;
}
inline void addmod(int& a, int val, int p = MOD) {
if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
struct DominatorTree {
static const int maxn = 200 + 5;
int n, rt;
vector<int> adj1[maxn];
vector<int> adj2[maxn];
vector<int> tree[maxn];
vector<int> bucket[maxn];
int par[maxn];
int sdm[maxn];
int dom[maxn];
int dsu[maxn];
int lbl[maxn];
int arr[maxn];
int rev[maxn];
int tms;
void init(int n, int rt) {
this->n = n;
this->rt = rt;
for (int i = 0; i < n; i++) {
adj1[i].clear();
adj2[i].clear();
tree[i].clear();
bucket[i].clear();
}
fill_n(arr, n, -1);
tms = 0;
}
void add(int u, int v) { adj1[u].push_back(v); }
void dfs(int u) {
arr[u] = tms, rev[tms] = u;
lbl[tms] = tms, sdm[tms] = tms, dsu[tms] = tms;
tms++;
for (int i = 0; i < (int)adj1[u].size(); i++) {
int w = adj1[u][i];
if (!~arr[w]) dfs(w), par[arr[w]] = arr[u];
adj2[arr[w]].push_back(arr[u]);
}
}
int find(int u, int x = 0) {
if (u == dsu[u]) return x ? -1 : u;
int v = find(dsu[u], x + 1);
if (v < 0) return u;
if (sdm[lbl[dsu[u]]] < sdm[lbl[u]]) {
lbl[u] = lbl[dsu[u]];
}
dsu[u] = v;
return x ? v : lbl[u];
}
void join(int u, int v) { dsu[v] = u; }
void build() {
dfs(rt);
for (int i = tms - 1; i >= 0; i--) {
for (int j = 0; j < (int)adj2[i].size(); j++) {
sdm[i] = min(sdm[i], sdm[find(adj2[i][j])]);
}
if (i > 1) bucket[sdm[i]].push_back(i);
for (int j = 0; j < (int)bucket[i].size(); j++) {
int w = bucket[i][j], v = find(w);
if (sdm[v] == sdm[w])
dom[w] = sdm[w];
else
dom[w] = v;
}
if (i > 0) join(par[i], i);
}
for (int i = 1; i < tms; i++) {
if (dom[i] != sdm[i]) dom[i] = dom[dom[i]];
tree[rev[i]].push_back(rev[dom[i]]);
tree[rev[dom[i]]].push_back(rev[i]);
}
}
} domtree;
const int maxn = 100 + 5;
int n, m, s, t;
vector<int> adj[maxn];
int k;
int x[maxn];
int y[maxn];
vector<int> g[maxn];
int d[maxn][maxn];
vector<int> adj2[maxn];
int must[maxn][maxn];
int num[maxn];
int out[maxn];
int ptr;
void dfs(int u, int p) {
num[u] = ptr++;
for (int i = (0); i < (int((adj2[u]).size())); i++) {
int v = adj2[u][i];
if (v != p) {
dfs(v, u);
}
}
out[u] = ptr - 1;
}
int isanc(int u, int v) {
if (num[u] == -1 || num[v] == -1) return 0;
return num[u] <= num[v] && out[u] >= out[v];
}
void work(int u) {
domtree.init(n, u);
for (int v = (0); v < (n); v++) {
for (int i = (0); i < (int((adj[v]).size())); i++) {
int w = adj[v][i];
if (d[u][w] == d[u][v] + 1) {
domtree.add(v, w);
}
}
}
domtree.build();
for (int i = (0); i < (n); i++) {
adj2[i] = domtree.tree[i];
}
for (int i = (0); i < (n); i++) num[i] = out[i] = -1;
ptr = 0, dfs(u, -1);
for (int i = (0); i < (int((g[u]).size())); i++) {
int ix = g[u][i];
for (int v = (0); v < (n); v++) {
must[ix][v] = isanc(v, y[ix]);
}
}
}
int dp[maxn][maxn];
void solve() {
cin >> n >> m >> s >> t;
s--, t--;
for (int i = (0); i < (n); i++)
for (int j = (0); j < (n); j++) d[i][j] = i == j ? 0 : INF;
for (int i = (0); i < (m); i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
d[u][v] = 1;
}
for (int k = (0); k < (n); k++)
for (int i = (0); i < (n); i++)
for (int j = (0); j < (n); j++) chkmin(d[i][j], d[i][k] + d[k][j]);
cin >> k;
for (int i = (0); i < (k); i++) {
cin >> x[i] >> y[i], x[i]--, y[i]--;
g[x[i]].push_back(i);
}
for (int u = (0); u < (n); u++) work(u);
for (int i = (0); i < (n); i++)
for (int j = (0); j < (k + 1); j++) dp[i][j] = INF;
for (int i = (0); i < (k); i++) dp[t][i] = 0;
while (1) {
int found = 0;
for (int u = (0); u < (n); u++)
for (int ix = (0); ix < (k + 1); ix++) {
if (ix < k && u != y[ix]) {
int tmp = 0;
for (int v = (0); v < (n); v++)
if (d[u][v] == 1 && d[u][v] + d[v][y[ix]] == d[u][y[ix]]) {
chkmax(tmp, dp[v][ix]);
}
if (chkmin(dp[u][ix], tmp)) {
found = 1;
}
}
int tmp = INF;
for (int i = (0); i < (k); i++)
if (i != ix && must[i][u]) {
chkmin(tmp, dp[u][i] + 1);
}
if (ix < k) {
for (int v = (0); v < (n); v++)
if (d[u][v] + d[v][y[ix]] == d[u][y[ix]] && must[ix][v]) {
chkmin(tmp, dp[v][ix]);
}
}
if (chkmin(dp[u][ix], tmp)) {
found = 1;
}
}
if (!found) break;
}
int res = dp[s][k];
if (res == INF) res = -1;
cout << res << "\n";
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0), cin.tie(0);
int JUDGE_ONLINE = 1;
if (argc > 1) {
JUDGE_ONLINE = 0;
assert(freopen(argv[1], "r", stdin));
}
if (argc > 2) {
JUDGE_ONLINE = 0;
assert(freopen(argv[2], "w", stdout));
}
solve();
if (!JUDGE_ONLINE) {
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int mp[105][105];
int s[105], t[105], res[105];
int num[105];
vector<int> p[105][105];
int a, b;
int n, m, k;
int main() {
memset(mp, 120, sizeof(mp));
int inf = mp[0][0];
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; ++i) mp[i][i] = 0;
int t1, t2;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &t1, &t2);
mp[t1][t2] = 1;
}
for (int k1 = 1; k1 <= n; ++k1)
for (int i = 1; i <= n; ++i)
if (mp[i][k1] != inf)
for (int j = 1; j <= n; ++j)
if (mp[k1][j] != inf) {
if (mp[i][k1] + mp[k1][j] < mp[i][j])
mp[i][j] = mp[i][k1] + mp[k1][j];
}
scanf("%d", &k);
for (int i = 1; i <= k; ++i) {
scanf("%d%d", &s[i], &t[i]);
if (mp[s[i]][t[i]] == inf) continue;
for (int j = 1; j <= n; ++j)
if (mp[s[i]][j] + mp[j][t[i]] == mp[s[i]][t[i]]) {
p[i][mp[s[i]][j]].push_back(j);
}
}
memset(res, 120, sizeof(res));
bool f = 1;
res[b] = 0;
int tim = 0;
while (f) {
f = 0;
tim++;
int len1, len2, v, u;
for (int i = 1; i <= k; ++i) {
if (mp[s[i]][t[i]] == inf) continue;
num[t[i]] = res[t[i]];
for (int j = mp[s[i]][t[i]] - 1; j >= 0; --j) {
len1 = p[i][j].size();
for (int l = 0; l < len1; ++l) {
len2 = p[i][j + 1].size();
u = p[i][j][l];
num[u] = 0;
for (int o = 0; o < len2; ++o) {
v = p[i][j + 1][o];
if (mp[u][v] == 1 && num[v] > num[u]) num[u] = num[v];
}
if (res[u] < num[u]) num[u] = res[u];
}
if (len1 == 1 && res[p[i][j][0]] == inf && num[p[i][j][0]] < tim) {
f = 1;
res[p[i][j][0]] = tim;
}
}
}
}
printf("%d", res[a] == inf ? -1 : res[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
void readi(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = v * 10 + c - '0';
while (isdigit(c = getchar())) v = v * 10 + c - '0';
x = v * f;
}
void readll(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = v * 10 + c - '0';
while (isdigit(c = getchar())) v = v * 10 + c - '0';
x = v * f;
}
void readc(char &x) {
char c;
while ((c = getchar()) == ' ')
;
x = c;
}
void writes(string s) { puts(s.c_str()); }
void writeln() { writes(""); }
void writei(int x) {
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
void writell(long long x) {
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
inline long long inc(int &x) { return ++x; }
inline long long inc(long long &x) { return ++x; }
inline long long inc(int &x, long long y) { return x += y; }
inline long long inc(long long &x, long long y) { return x += y; }
inline double inc(double &x, double y) { return x += y; }
inline long long dec(int &x) { return --x; }
inline long long dec(long long &x) { return --x; }
inline long long dec(int &x, long long y) { return x -= y; }
inline long long dec(long long &x, long long y) { return x -= y; }
inline double dec(double &x, double y) { return x -= y; }
inline long long mul(int &x) { return x = ((long long)x) * x; }
inline long long mul(long long &x) { return x = x * x; }
inline long long mul(int &x, long long y) { return x *= y; }
inline long long mul(long long &x, long long y) { return x *= y; }
inline double mul(double &x, double y) { return x *= y; }
inline long long divi(int &x) {
long long ans, l, r, mid;
ans = 0;
l = 0;
r = 0x3fffffff;
while (l < r) {
mid = (l + r) / 2;
if (mid * mid < x) {
ans = mid;
l = mid + 1;
} else
r = mid;
}
return ans;
}
inline long long divi(long long &x) {
long long ans, l, r, mid;
ans = 0;
l = 0;
r = 0x3fffffff;
while (l < r) {
mid = (l + r) / 2;
if (mid * mid < x) {
ans = mid;
l = mid + 1;
} else
r = mid;
}
return ans;
}
inline long long divi(int &x, long long y) { return x /= y; }
inline long long divi(long long &x, long long y) { return x /= y; }
inline double divi(double &x, double y) { return x /= y; }
inline long long mod(int &x, long long y) { return x %= y; }
inline long long mod(long long &x, long long y) { return x %= y; }
long long n, m, s, t, k, i, j, l, dp[105], dp2[105], dis[105][105],
cnt[105][105], cer[105][105], ps[105], pt[105], _, x, y, col, vis[105];
long long dfs(long long x, long long y) {
if (vis[x] == col) return dp2[x];
vis[x] = col;
dp2[x] = -1;
int i;
if ((1) <= ((n)))
for (((i)) = (1); ((i)) <= ((n)); ((i))++)
if (dis[x][i] == 1 && dis[x][i] + dis[i][y] == dis[x][y])
dp2[x] = max(dp2[x], dfs(i, y));
if (dp2[x] == -1) dp2[x] = 0x2222222222ll;
return dp2[x] = min(dp2[x], dp[x] + 1);
}
int main() {
ios_base::sync_with_stdio(false);
;
memset((dis), (0x16), (sizeof((dis))));
cin >> n >> m >> s >> t;
if ((1) <= ((n)))
for (((i)) = (1); ((i)) <= ((n)); ((i))++) dis[i][i] = 0;
if ((1) <= ((m)))
for (((i)) = (1); ((i)) <= ((m)); ((i))++) {
cin >> x >> y;
if (x != y) dis[x][y] = 1;
}
if ((1) <= ((n)))
for (((l)) = (1); ((l)) <= ((n)); ((l))++)
if ((1) <= ((n)))
for (((i)) = (1); ((i)) <= ((n)); ((i))++)
if ((1) <= ((n)))
for (((j)) = (1); ((j)) <= ((n)); ((j))++)
dis[i][j] = min(dis[i][j], dis[i][l] + dis[l][j]);
cin >> k;
if ((1) <= ((k)))
for (((i)) = (1); ((i)) <= ((k)); ((i))++) {
cin >> ps[i] >> pt[i];
if ((1) <= ((n)))
for (((j)) = (1); ((j)) <= ((n)); ((j))++) {
if (dis[ps[i]][j] + dis[j][pt[i]] <= n &&
dis[ps[i]][j] + dis[j][pt[i]] == dis[ps[i]][pt[i]])
cnt[i][dis[ps[i]][j]]++;
}
if ((1) <= ((n)))
for (((j)) = (1); ((j)) <= ((n)); ((j))++) {
if (dis[ps[i]][j] + dis[j][pt[i]] <= n &&
dis[ps[i]][j] + dis[j][pt[i]] == dis[ps[i]][pt[i]] &&
cnt[i][dis[ps[i]][j]] == 1)
cer[i][j] = 1;
}
}
memset((dp), (0x16), (sizeof((dp))));
dp[t] = 0;
memset((dp2), (0x16), (sizeof((dp2))));
if ((1) <= ((0x3bbbbbbb)))
for (((_)) = (1); ((_)) <= ((0x3bbbbbbb)); ((_))++) {
bool chg = 0;
if ((1) <= ((k)))
for (((i)) = (1); ((i)) <= ((k)); ((i))++) {
if ((1) <= ((n)))
for (((j)) = (1); ((j)) <= ((n)); ((j))++) {
if (cer[i][j]) {
col++;
if (dfs(j, pt[i]) < dp[j]) {
dp[j] = dfs(j, pt[i]);
chg = 1;
}
}
}
}
if (!chg) break;
}
if (dp[s] >= 0x2222222222ll)
cout << -1;
else
cout << dp[s];
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 201;
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 * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m, num, cnt;
int a[maxn][maxn], b[maxn][maxn], S[maxn], T[maxn];
int ans[maxn], dp[maxn], vis[maxn], p[maxn][maxn];
int dfs(int u, int v) {
if (vis[u] == cnt) return dp[u];
vis[u] = cnt;
int Ans = -1;
for (int i = 1; i <= n; i++)
if (a[u][i] == 1 && a[u][i] + a[i][v] == a[u][v]) Ans = max(Ans, dfs(i, v));
if (Ans < 0) Ans = 1e9;
dp[u] = min(ans[u] + 1, Ans);
return dp[u];
}
int main() {
int u, v;
n = read(), m = read(), S[0] = read(), T[0] = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j) a[i][j] = 1e9;
for (int i = 1; i <= m; i++) {
u = read(), v = read();
a[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
num = read();
for (int i = 1; i <= num; i++) {
S[i] = read(), T[i] = read();
for (int j = 1; j <= n; j++)
if (a[S[i]][j] < 1e9 && a[S[i]][j] + a[j][T[i]] == a[S[i]][T[i]])
b[i][a[S[i]][j]]++;
for (int j = 1; j <= n; j++)
if (a[S[i]][j] < 1e9 && a[S[i]][j] + a[j][T[i]] == a[S[i]][T[i]] &&
b[i][a[S[i]][j]] == 1)
p[i][j] = 1;
}
for (int i = 1; i <= n; i++) ans[i] = 1e9;
ans[T[0]] = 0;
while (1) {
bool find = 0;
for (int i = 1; i <= num; i++)
for (int j = 1; j <= n; j++)
if (p[i][j]) {
cnt++;
int tmp = dfs(j, T[i]);
if (tmp < ans[j]) {
ans[j] = tmp;
find = 1;
}
}
if (!find) break;
}
if (ans[S[0]] < 1e9)
printf("%d\n", ans[S[0]]);
else
printf("-1\n");
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 105;
int n, m, a, b;
int dis[MAXN][MAXN];
int ans[MAXN], temp[MAXN];
vector<int> ms[MAXN];
vector<int> vec[MAXN][MAXN];
int s[MAXN], t[MAXN];
void solved(int cas) {
int x, y;
scanf("%d %d %d %d", &n, &m, &a, &b);
memset(dis, INF, sizeof(dis));
for (int i = 1; i <= n; i++) dis[i][i] = 0;
while (m--) {
scanf("%d %d", &x, &y);
dis[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &s[i], &t[i]);
x = s[i], y = t[i];
if (dis[s[i]][t[i]] == INF) {
i--, m--;
continue;
}
for (int j = 1; j <= n; j++) {
if (dis[x][j] + dis[j][y] != dis[x][y]) continue;
vec[i][dis[x][j]].push_back(j);
int cnt = 0;
for (int k = 1; k <= n; k++)
if (j != k && dis[x][j] == dis[x][k] && dis[k][y] == dis[j][y]) cnt++;
if (!cnt) ms[i].push_back(j);
}
}
memset(ans, INF, sizeof(ans));
ans[b] = 0;
bool flag = true;
while (flag) {
flag = false;
for (int i = 0; i < m; i++) {
x = s[i], y = t[i];
memset(temp, INF, sizeof(temp));
temp[y] = min(temp[y], ans[y] + 1);
for (int len = dis[x][y] - 1; len >= 0; len--) {
for (int j = 0; j < vec[i][len].size(); j++) {
int val = -1;
for (int k = 0; k < vec[i][len + 1].size(); k++)
if (dis[vec[i][len][j]][vec[i][len + 1][k]] == 1)
val = max(val, temp[vec[i][len + 1][k]]);
if (val != -1)
temp[vec[i][len][j]] = min(ans[vec[i][len][j]] + 1, val);
}
}
for (int j = 0; j < ms[i].size(); j++) {
if (temp[ms[i][j]] < ans[ms[i][j]]) {
ans[ms[i][j]] = temp[ms[i][j]];
flag = true;
}
}
}
}
if (ans[a] == INF)
printf("-1\n");
else
printf("%d\n", ans[a]);
}
int main() {
int T = 1;
for (int i = 1; i <= T; i++) solved(i);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
vector<int> G1[maxn];
int n, m, a, b, q, mp[maxn][maxn], must[maxn][maxn], num[maxn];
int dist[maxn], vis[maxn], p[maxn], fb[maxn], tb[maxn], dp[maxn], Md[maxn];
struct Heap {
int u, d;
Heap(int _u = 0, int _d = 0) {
u = _u;
d = _d;
}
bool operator<(const Heap &H) const { return d > H.d; }
};
void Dijkstra(int s, int del) {
priority_queue<Heap> Q;
for (int i = 1; i <= n; i++) dist[i] = 0x3f3f3f3f;
memset(vis, 0, sizeof vis);
dist[s] = 0;
Q.push(Heap(s, 0));
while (!Q.empty()) {
int u = Q.top().u;
Q.pop();
if (u == del) continue;
if (vis[u]) continue;
vis[u] = 1;
int sz = G1[u].size();
for (int i = 0; i < sz; i++) {
int v = G1[u][i];
if (v == del) continue;
if (dist[v] > dist[u] + 1) {
Q.push(Heap(v, dist[v]));
dist[v] = dist[u] + 1;
p[v] = u;
}
}
}
}
vector<int> road;
int solve(int u, int k, int t) {
if (vis[u] == t) return Md[u];
int tmp = -1, sz = G1[u].size();
vis[u] = t;
for (int i = 0; i < sz; i++) {
int v = G1[u][i];
if (mp[fb[k]][u] + 1 + mp[v][tb[k]] == mp[fb[k]][tb[k]]) {
tmp = max(tmp, solve(v, k, t));
}
}
return Md[u] = min(tmp == -1 ? 0x3f3f3f3f : tmp, dp[u]);
}
int main() {
int u, v, mv, md;
memset(mp, 0x3f, sizeof mp);
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++) mp[i][i] = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
G1[u].push_back(v);
mp[u][v] = 1;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
}
}
scanf("%d", &q);
for (int bs = 1; bs <= q; bs++) {
scanf("%d%d", &u, &v);
fb[bs] = u;
tb[bs] = v;
if (mp[u][v] == 0x3f3f3f3f) continue;
for (int i = 1; i <= n; i++)
if (mp[u][i] + mp[i][v] == mp[u][v]) num[mp[u][i]]++;
for (int i = 1; i <= n; i++)
if (mp[u][i] + mp[i][v] == mp[u][v]) {
if (num[mp[u][i]] == 1) must[bs][i] = 1;
num[mp[u][i]] = 0;
}
}
int ok = 1, times = 1;
memset(vis, 0, sizeof vis);
memset(dp, 0x3f, sizeof dp);
dp[b] = 0;
while (ok) {
ok = 0;
for (int bs = 1; bs <= q; bs++) {
for (int i = 1; i <= n; i++) {
if (must[bs][i]) {
int tmp = solve(i, bs, times++) + 1;
if (tmp < dp[i]) ok = 1, dp[i] = tmp;
}
}
}
}
printf("%d\n", dp[a] == 0x3f3f3f3f ? -1 : dp[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, S, T, x, y, num;
int a[110][110], b[110][110], s[110], t[110], an[110], h[110], f[110],
c[110][110];
int dfs(int x, int y) {
if (h[x] == num) return f[x];
h[x] = num;
int i, ans = -1;
for (i = 1; i <= n; i++)
if (a[x][i] == 1 && a[x][i] + a[i][y] == a[x][y]) ans = max(ans, dfs(i, y));
if (ans < 0) ans = a[0][0];
f[x] = min(ans, an[x] + 1);
return f[x];
}
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
memset(a, 60, sizeof(a));
for (i = 1; i <= n; i++) a[i][i] = 0;
for (i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
a[x][y] = 1;
}
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
scanf("%d", &m);
for (i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
for (j = 1; j <= n; j++)
if (a[x][j] < 1e9 && a[x][j] + a[j][y] == a[x][y]) b[i][a[x][j]]++;
for (j = 1; j <= n; j++)
if (a[x][j] < 1e9 && a[x][j] + a[j][y] == a[x][y] && b[i][a[x][j]] == 1)
c[i][j] = 1;
s[i] = x, t[i] = y;
}
memset(an, 60, sizeof(an));
an[T] = 0;
for (;;) {
int F = 0;
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
if (c[i][j]) {
++num;
int A = dfs(j, t[i]);
if (A < an[j]) an[j] = A, F = 1;
}
}
if (!F) break;
}
printf("%d\n", an[S] > 1e9 ? -1 : an[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int sum = 0;
char c = getchar();
bool f = 0;
while (c < '0' || c > '9') {
if (c == '-') f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
sum = sum * 10 + c - '0';
c = getchar();
}
if (f) return -sum;
return sum;
}
const int orz = 1000000000;
const int N = 105;
int n, m, c, S, T, d[N][N], fr[N], to[N];
vector<int> SD[N];
bool cn[N][N];
int f[N][N];
inline bool DP() {
int i, j, k, w, x, y;
bool a = 0, b;
for (i = 1; i <= n; i++)
for (j = 1; j <= c; j++) {
b = 0;
w = -1;
x = fr[j];
y = to[j];
for (k = 1; k <= n; k++)
if (d[i][k] == 1 && d[k][y] + 1 == d[i][y]) w = max(w, f[k][j]), b = 1;
if (b && w < f[i][j]) a = 1, f[i][j] = w;
for (k = 1; k <= c; k++)
if (cn[i][k] && f[i][k] + 1 < f[i][j]) f[i][j] = f[i][k] + 1, a = 1;
}
return a;
}
int main() {
int i, j, k, x, y;
n = read();
m = read();
S = read();
T = read();
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (i != j) d[i][j] = orz;
for (i = 1; i <= m; i++) x = read(), y = read(), d[x][y] = 1;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
c = read();
for (i = 1; i <= c; i++) {
fr[i] = x = read();
to[i] = y = read();
if (d[x][y] == orz) {
i--;
c--;
continue;
}
for (j = 0; j <= 100; j++) SD[j].clear();
for (j = 1; j <= n; j++)
if (d[x][j] + d[j][y] == d[x][y]) SD[d[x][j]].push_back(j);
for (j = 0; j <= 100; j++)
if (SD[j].size() == 1) cn[SD[j][0]][i] = 1;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= c; j++) f[i][j] = (i == T) ? 0 : orz;
int ans = orz;
while (DP())
;
for (i = 1; i <= c; i++)
if (cn[S][i]) ans = min(ans, f[S][i] + 1);
printf("%d", ans == orz ? -1 : ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, S, T, x, y, num;
int a[110][110], b[110][110], s[110], t[110], an[110], h[110], f[110],
c[110][110];
int dfs(int x, int y) {
if (h[x] == num) return f[x];
h[x] = num;
int i, ans = -1;
for (i = 1; i <= n; i++)
if (a[x][i] == 1 && a[x][i] + a[i][y] == a[x][y]) ans = max(ans, dfs(i, y));
if (ans < 0) ans = a[0][0];
f[x] = min(ans, an[x] + 1);
return f[x];
}
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
memset(a, 60, sizeof(a));
for (i = 1; i <= n; i++) a[i][i] = 0;
for (i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
a[x][y] = 1;
}
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
scanf("%d", &m);
for (i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
for (j = 1; j <= n; j++)
if (a[x][j] < 1e9 && a[x][j] + a[j][y] == a[x][y]) b[i][a[x][j]]++;
for (j = 1; j <= n; j++)
if (a[x][j] < 1e9 && a[x][j] + a[j][y] == a[x][y] && b[i][a[x][j]] == 1)
c[i][j] = 1;
s[i] = x, t[i] = y;
}
memset(an, 60, sizeof(an));
an[T] = 0;
for (;;) {
int F = 0;
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
if (c[i][j]) {
++num;
int A = dfs(j, t[i]);
if (A < an[j]) an[j] = A, F = 1;
}
}
if (!F) break;
}
printf("%d\n", an[S] > 1e9 ? -1 : an[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int inf = 100000000;
int dis[N][N], c[N][N], res[N];
int s[N], t[N], tmp[N], ans[N];
int n, m, vs, vt, x, y, tim;
int dfs(int x, int y) {
if (tmp[x] == tim) return res[x];
tmp[x] = tim;
int mx = -1;
for (int i = 1; i <= n; i++)
if (dis[x][i] == 1 && 1 + dis[i][y] == dis[x][y]) mx = max(mx, dfs(i, y));
if (mx == -1) mx = inf;
mx = min(mx, ans[x] + 1);
return res[x] = mx;
}
int main() {
scanf("%d%d%d%d", &n, &m, &vs, &vt);
memset(dis, 0x16, sizeof(dis));
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
dis[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", s + i, t + i);
memset(tmp, 0, sizeof(tmp));
for (int j = 1; j <= n; j++)
if (dis[s[i]][j] <= n && dis[s[i]][t[i]] == dis[s[i]][j] + dis[j][t[i]])
tmp[dis[s[i]][j]]++;
for (int j = 1; j <= n; j++)
if (dis[s[i]][j] <= n && dis[s[i]][t[i]] == dis[s[i]][j] + dis[j][t[i]] &&
tmp[dis[s[i]][j]] == 1)
c[i][j] = 1;
}
int flag = 1;
memset(tmp, 0, sizeof(tmp));
memset(ans, 0x16, sizeof(ans));
ans[vt] = 0;
while (flag) {
flag = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
if (c[i][j]) {
tim++;
int x = dfs(j, t[i]);
if (x < ans[j]) ans[j] = x, flag = 1;
}
}
}
if (ans[vs] <= n)
printf("%d\n", ans[vs]);
else
printf("-1\n");
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<int> e[101];
int n, m, a, b, k, s[101], t[101], r[101], d[101][101], seen[101], next_seen;
bool must[101][101];
int main() {
scanf("%d %d %d %d", &n, &m, &a, &b);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = i == j ? 0 : 1000000000;
while (m--) {
int x, y;
scanf("%d %d", &x, &y);
e[x].push_back(y);
d[x][y] = 1;
}
for (int z = 1; z <= n; z++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][z] + d[z][j]);
scanf("%d", &k);
for (int i = 0; i < k; i++) scanf("%d %d", &s[i], &t[i]);
for (int i = 0; i < k; i++)
for (int j = 1; j <= n; j++) {
must[i][j] = d[s[i]][j] + d[j][t[i]] == d[s[i]][t[i]];
for (int z = 1; z <= n; z++)
must[i][j] &= z == j || d[s[i]][z] != d[s[i]][j] ||
d[z][t[i]] != d[j][t[i]] ||
d[s[i]][z] + d[z][t[i]] != d[s[i]][t[i]];
}
for (int i = 1; i <= n; i++) r[i] = 1000000000;
r[b] = 0;
int phase = 0;
while (++phase) {
bool found = false;
for (int i = 0; i < k; i++)
if (d[s[i]][t[i]] < 1000000000)
for (int j = 1; j <= n; j++)
if (must[i][j] && r[j] == 1000000000) {
vector<int> q(1, j);
seen[j] = ++next_seen;
for (int z = 0; z < q.size(); z++) {
int cur = q[z];
for (int next = 1; next <= n; next++)
if (d[cur][next] == 1 && seen[next] != next_seen &&
d[cur][t[i]] == 1 + d[next][t[i]] && r[next] >= phase) {
q.push_back(next);
seen[next] = next_seen;
}
}
if (seen[t[i]] != next_seen) {
r[j] = phase;
found = true;
}
}
if (!found) break;
}
printf("%d\n", r[a] == 1000000000 ? -1 : r[a]);
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 10, INF = INT_MAX / 2;
int n, m, a, b, dis[MAXN][MAXN], dp[MAXN][MAXN], cnt[MAXN];
vector<int> adj[MAXN], g[MAXN][MAXN], vec[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> a >> b, a--, b--;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v, u--, v--;
adj[u].push_back(v);
dis[u][v] = 1;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (!dis[i][j]) dis[i][j] = INF, dis[i][i] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int s, t;
cin >> s >> t, s--, t--;
if (dis[s][t] == INF) continue;
memset(cnt, 0, sizeof cnt);
for (int j = 0; j < n; j++) {
if (dis[s][j] + dis[j][t] == dis[s][t]) {
for (int x = 0; x < adj[j].size(); x++) {
int v = adj[j][x];
if (dis[s][v] == dis[s][j] + 1)
if (dis[s][v] + dis[v][t] == dis[s][t]) g[i][j].push_back(v);
}
cnt[dis[s][j]]++;
}
}
for (int j = 0; j < n; j++) {
if (dis[s][j] + dis[j][t] == dis[s][t] && cnt[dis[s][j]] == 1)
vec[j].push_back(i);
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++) dp[i][j] = INF, dp[b][j] = 0;
for (int i = 0; i <= n * k; i++) {
for (int j = 0; j < n; j++) {
for (int x = 0; x < k; x++) {
int mx = -INF;
for (int p = 0; p < g[x][j].size(); p++) {
int u = g[x][j][p];
mx = max(mx, dp[u][x]);
}
if (mx != -INF) dp[j][x] = min(dp[j][x], mx);
for (int p = 0; p < vec[j].size(); p++) {
int u = vec[j][p];
dp[j][x] = min(dp[j][x], dp[j][u] + 1);
}
}
}
}
int res = INF;
for (int i = 0; i < vec[a].size(); i++) {
int x = vec[a][i];
res = min(res, dp[a][x]);
}
if (res == INF)
cout << -1 << endl;
else
cout << res + 1 << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
bool debug = false;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long ln, lk, lm;
int dis[105][105];
int S, T, ans[105], cnt, h[105], f[105];
int b[105][105], must[105][105];
vector<pair<int, int>> bus;
int dfs(int x, int t) {
if (h[x] == cnt) return f[x];
h[x] = cnt;
int aa = -1;
for (int i = 1; i <= n; i++) {
if (dis[x][i] == 1 && dis[x][i] + dis[i][t] == dis[x][t]) {
aa = max(aa, dfs(i, t));
}
}
if (aa < 0) aa = dis[0][0];
return f[x] = min(aa, ans[x] + 1);
}
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
for (int(i) = 0; (i) < (int)(105); (i)++)
for (int(j) = 0; (j) < (int)(105); (j)++) {
dis[i][j] = 1e9;
ans[i] = 1e9;
}
for (int(i) = 0; (i) < (int)(n); (i)++) dis[i + 1][i + 1] = 0;
for (int i = 0, u, v; i < m; i++) {
scanf("%d%d", &u, &v);
dis[u][v] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
}
}
}
scanf("%d", &k);
for (int i = 1, s, t; i <= k; i++) {
scanf("%d%d", &s, &t);
for (int j = 1; j <= n; j++) {
if (dis[s][j] < 1e9 && dis[s][j] + dis[j][t] == dis[s][t]) {
b[i][dis[s][j]]++;
}
}
for (int j = 1; j <= n; j++) {
if (dis[s][j] < 1e9 && dis[s][j] + dis[j][t] == dis[s][t]) {
if (b[i][dis[s][j]] == 1) {
must[i][j] = 1;
}
}
}
bus.push_back({s, t});
}
ans[T] = 0;
while (1) {
bool bl = 0;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++)
if (must[i][j]) {
cnt++;
int A = dfs(j, bus[i - 1].second);
if (A < ans[j]) {
ans[j] = A;
bl = 1;
}
}
}
if (!bl) break;
}
printf("%d\n", ans[S] >= 1e9 ? -1 : ans[S]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 105, inf = 1e9;
int n, d[N][N], c, m, s[N], t[N], v[N], g[N][N], p[N], q[N], e[N][N];
int dfs(int a, int k) {
if (v[a] == c) return q[a];
v[a] = c, q[a] = -1;
for (int i = 1; i <= n; i++)
if (d[a][i] == 1 && d[a][t[k]] == d[i][t[k]] + 1)
q[a] = max(q[a], dfs(i, k));
if (!(~q[a])) q[a] = inf;
return q[a] = min(q[a], p[a]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &s[0], &t[0]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = inf;
for (int i = 1; i <= n; i++) d[i][i] = 0;
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
d[a][b] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &s[i], &t[i]);
for (int j = 1; j <= n; j++)
if (d[s[i]][t[i]] == d[s[i]][j] + d[j][t[i]] && d[s[i]][j] != inf)
g[i][d[s[i]][j]]++;
for (int j = 1; j <= n; j++)
if (d[s[i]][t[i]] == d[s[i]][j] + d[j][t[i]] && d[s[i]][j] != inf &&
g[i][d[s[i]][j]] == 1)
e[i][j] = 1;
}
for (int i = 1; i <= n; i++) p[i] = q[i] = inf;
p[t[0]] = 0;
int f = 1;
while (f) {
f = 0;
for (int i = 1; i <= m; i++) {
if (d[s[i]][t[i]] == inf) continue;
for (int j = 1; j <= n; j++)
if (e[i][j]) {
c++;
int tmp = dfs(j, i) + 1;
if (tmp < p[j]) f = 1, p[j] = tmp;
}
}
}
if (p[s[0]] == inf) p[s[0]] = -1;
printf("%d", p[s[0]]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
template <class K>
inline bool chkmin(K &a, K b) {
return a > b ? a = b, true : false;
}
template <class K>
inline bool chkmax(K &a, K b) {
return a < b ? a = b, true : false;
}
using namespace std;
const int oo = 0x3f3f3f3f;
const int maxn = 200 + 1;
int N, M, K, G[maxn][maxn], path[maxn][maxn], sest[maxn], st[maxn], ed[maxn],
vis[maxn], dp[maxn], cur;
bool mt[maxn][maxn];
int dfs(int u, int v) {
if (vis[u] == cur) return dp[u];
vis[u] = cur;
int ret = -1;
for (int i = 1; i <= N; i++)
if (G[u][i] == 1 && G[u][i] + G[i][v] == G[u][v]) chkmax(ret, dfs(i, v));
return dp[u] = min(ret < 0 ? oo : ret, sest[u] + 1);
;
}
int main() {
scanf("%d%d%d%d", &N, &M, &st[0], &ed[0]);
for (int i = 1; i <= N; i++) {
sest[i] = oo;
for (int j = i + 1; j <= N; j++) G[j][i] = G[i][j] = oo;
}
for (int i = 1; i <= M; ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u][v] = 1;
}
for (int t = 1; t <= N; t++)
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) chkmin(G[i][j], G[i][t] + G[t][j]);
scanf("%d", &K);
for (int i = 1; i <= K; ++i) {
scanf("%d%d", &st[i], &ed[i]);
int &u = st[i], &v = ed[i];
for (int j = 1; j <= N; ++j)
if (G[u][j] + G[j][v] == G[u][v] && G[u][j] < oo) path[i][G[u][j]]++;
for (int j = 1; j <= N; ++j)
if (G[u][j] + G[j][v] == G[u][v] && G[u][j] < oo && path[i][G[u][j]] == 1)
mt[i][j] = true;
}
sest[ed[0]] = 0;
while (true) {
bool cab = false;
for (int i = 1; i <= K; ++i)
for (int j = 1; j <= N; ++j)
if (mt[i][j]) {
cur++;
cab |= chkmin(sest[j], dfs(j, ed[i]));
}
if (!cab) break;
}
if (sest[st[0]] >= oo)
puts("-1");
else
printf("%d\n", sest[st[0]]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x;
}
inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }
int n, m, a, b, g[105][105];
int ans[105][105];
bool vis[105][105];
bool only[105][105], cnc[105][105];
int x[105], y[105], k;
int can[105][105], cnt[105];
int que[105], dis[105], dp[105], lo = 1, hi = 0;
int cal(int pos, int no) {
if (vis[pos][no]) return ans[pos][no];
vis[pos][no] = 1;
ans[pos][no] = 233366666;
if (pos == b) return ans[pos][no] = 0;
int i, tpans = 0;
if (pos == y[no]) tpans = 233366666;
for (i = 1; i <= n; i++) {
if (cnc[pos][i] && g[pos][y[no]] == g[i][y[no]] + 1) {
tpans = max(tpans, cal(i, no));
}
}
ans[pos][no] = min(tpans, dp[pos]);
return ans[pos][no];
}
int main() {
int i, j, e, f, l;
n = read();
m = read();
a = read();
b = read();
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
g[i][j] = 233366666;
}
g[i][i] = 0;
}
for (i = 1; i <= m; i++) {
e = read();
f = read();
cnc[e][f] = 1;
g[e][f] = 1;
}
for (l = 1; l <= n; l++) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
g[i][j] = min(g[i][j], g[i][l] + g[l][j]);
}
}
}
k = read();
for (i = 1; i <= k; i++) {
x[i] = read();
y[i] = read();
}
for (l = 1; l <= k; l++) {
int tp = g[x[l]][y[l]];
if (tp == 233366666) continue;
for (j = 1; j <= n; j++) {
if (j == x[l] || j == y[l]) {
only[l][j] = 1;
can[j][++cnt[j]] = l;
continue;
}
for (i = 1; i <= n; i++) dis[i] = 233366666;
lo = 1;
hi = 0;
que[++hi] = x[l];
dis[x[l]] = 0;
while (lo <= hi) {
int t = que[lo++];
for (i = 1; i <= n; i++) {
if (i == j) continue;
if (!cnc[t][i]) continue;
if (dis[i] > dis[t] + 1) {
dis[i] = dis[t] + 1;
que[++hi] = i;
}
}
}
if (dis[y[l]] > tp) {
only[l][j] = 1;
can[j][++cnt[j]] = l;
}
}
}
for (i = 1; i <= n; i++) dp[i] = 233366666;
dp[b] = 0;
int flag = 1;
while (flag) {
flag = 0;
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++) {
int prev = dp[i];
for (j = 1; j <= cnt[i]; j++) {
dp[i] = min(dp[i], cal(i, can[i][j]) + 1);
}
if (dp[i] != prev) flag = 1;
}
}
int ans = dp[a];
if (ans == 233366666) {
puts("-1");
exit(0);
}
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 205;
int n, m, K, T, dis[maxn][maxn], A, B, s[maxn], t[maxn];
int cnt, head[maxn], to[maxn * maxn], nex[maxn * maxn];
int tot[maxn], tmp[maxn], tp;
int stk[maxn], l;
bool bj[maxn][maxn];
int dp[maxn], DP[maxn], vis[maxn];
void add(int x, int y) {
to[++cnt] = y;
nex[cnt] = head[x];
head[x] = cnt;
}
priority_queue<pair<int, int> > q;
void floyd() {
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
dis[i][j] = min(dis[i][k] + dis[k][j], dis[i][j]);
}
}
}
void find(int u, int k) {
stk[++l] = u;
if (u == t[k]) {
for (int i = 1; i <= l; i++) tot[stk[i]]++;
}
for (int i = head[u]; i; i = nex[i]) {
int v = to[i];
if (dis[v][t[k]] + 1 == dis[u][t[k]]) {
find(v, k);
}
}
l--;
}
int dfs(int u, int k) {
if (vis[u] == T) return DP[u];
int tmp = -1;
vis[u] = T;
for (int i = head[u]; i; i = nex[i]) {
int v = to[i];
if (dis[v][t[k]] + 1 == dis[u][t[k]]) {
tmp = max(tmp, dfs(v, k));
}
}
if (tmp == -1) tmp = 1e9;
if (tmp > dp[u]) tmp = dp[u];
return DP[u] = tmp;
}
signed main() {
cin >> n >> m >> A >> B;
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
dis[x][y] = 1;
}
floyd();
cin >> K;
for (int i = 1; i <= K; i++) cin >> s[i] >> t[i];
for (int i = 1; i <= K; i++) {
if (dis[s[i]][t[i]] > 1e9) continue;
memset(tot, 0, sizeof(tot));
find(s[i], i);
tp = 0;
tmp[++tp] = s[i];
tmp[++tp] = t[i];
bj[i][s[i]] = bj[i][t[i]] = 1;
for (int j = 1; j <= n; j++) {
if (j == s[i] || j == t[i]) continue;
if (tot[j] == tot[t[i]]) bj[i][j] = 1;
}
}
int flag = 1;
memset(dp, 0x3f, sizeof(dp));
memset(DP, 0x3f, sizeof(DP));
dp[B] = 0;
while (flag) {
flag = 0;
for (int i = 1; i <= K; i++) {
for (int j = 1; j <= n; j++) {
if (bj[i][j] == 0) continue;
T++;
int tmp = dfs(j, i) + 1;
if (tmp < dp[j]) {
dp[j] = tmp;
flag = 1;
}
}
}
}
if (dp[A] > 1e9) dp[A] = -1;
cout << dp[A] << endl;
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | import static java.util.Arrays.fill;
import java.io.*;
import java.util.*;
public class E {
private static void solve() throws IOException {
int n = nextInt(), edges = nextInt();
int goFrom = nextInt() - 1, goTo = nextInt() - 1;
boolean[][] can = new boolean[n][n];
for (int i = 0; i < edges; i++) {
int from = nextInt() - 1, to = nextInt() - 1;
can[from][to] = true;
}
int[][] dist = new int[n][n];
final int INF = Integer.MAX_VALUE / 2;
for (int i = 0; i < n; i++) {
fill(dist[i], INF);
for (int j = 0; j < n; j++) {
if (can[i][j]) {
dist[i][j] = 1;
}
}
dist[i][i] = 0;
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int buses = nextInt();
int[] s = new int[buses];
int[] t = new int[buses];
for (int i = 0; i < buses; i++) {
s[i] = nextInt() - 1;
t[i] = nextInt() - 1;
}
boolean[][] haveBus = new boolean[n][buses];
for (int dontUse = 0; dontUse < n; dontUse++) {
int[][] d = new int[n][n];
for (int[] dd: d) {
fill(dd, INF);
}
for (int i = 0; i < n; i++) {
if (i == dontUse) {
continue;
}
d[i][i] = 0;
for (int j = 0; j < n; j++) {
if (i == dontUse || j == dontUse) {
continue;
}
if (can[i][j]) {
d[i][j] = 1;
}
}
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
for (int i = 0; i < buses; i++) {
haveBus[dontUse][i] = dist[s[i]][t[i]] != INF && dist[s[i]][t[i]] != d[s[i]][t[i]];
}
}
int[] isGood = new int[n];
fill(isGood, INF);
isGood[goTo] = 0;
boolean change = true;
int[][] distUsingBad = new int[n][n];
int iterations = 0;
recalcDistUsingBad(n, can, INF, isGood, distUsingBad);
while (change) {
++iterations;
change = false;
for (int from = 0; from < n; from++) {
if (isGood[from] != INF) {
continue;
}
boolean haveGoodBus = false;
for (int bus = 0; bus < buses; bus++) {
boolean havePath = haveBus[from][bus];
if (!havePath) {
continue;
}
if (distUsingBad[from][t[bus]] != dist[from][t[bus]]) {
haveGoodBus = true;
break;
}
}
if (haveGoodBus) {
change = true;
isGood[from] = iterations;
}
}
recalcDistUsingBad(n, can, INF, isGood, distUsingBad);
}
if (isGood[goFrom] == INF) {
out.println(-1);
} else {
out.println(isGood[goFrom]);
}
}
private static void recalcDistUsingBad(int n, boolean[][] can, final int INF, int[] isGood,
int[][] distUsingBad) {
for (int[] dd : distUsingBad) {
fill(dd, INF);
}
for (int i = 0; i < n; i++) {
if (isGood[i] != INF) {
continue;
}
for (int j = 0; j < n; j++) {
if (isGood[j] != INF) {
continue;
}
if (can[i][j]) {
distUsingBad[i][j] = 1;
}
}
distUsingBad[i][i] = 0;
}
for (int k = 0; k < n; k++) {
int[] dubk = distUsingBad[k];
for (int i = 0; i < n; i++) {
int[] dubi = distUsingBad[i];
for (int j = 0; j < n; j++) {
dubi[j] = min(dubi[j], dubi[k] + dubk[j]);
}
}
}
}
static int min(int a, int b) {
return a < b ? a : b;
}
public static void main(String[] args) {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(239);
}
}
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | JAVA |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int D[110][110], n, m, q, E[110];
vector<int> A[110][110];
int Res[110];
vector<int> M[110];
int CM[110][110];
int main() {
int i, j, v, nv, l, k, a, b, q, v1, v2;
bool fl = 1;
scanf("%d%d%d%d", &n, &m, &a, &b), a--, b--;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (i != j) D[i][j] = ((int)1e9);
for (i = 0; i < m; i++) scanf("%d%d", &v1, &v2), v1--, v2--, D[v1][v2] = 1;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = 0; k < n; k++) D[j][k] = min(D[j][k], D[j][i] + D[i][k]);
scanf("%d", &q);
for (i = 0; i < q; i++) {
scanf("%d%d", &v1, &v2), v1--, v2--;
if (D[v1][v2] == ((int)1e9)) {
q--, i--;
continue;
}
for (E[i] = D[v1][v2], j = 0; j < n; j++)
if (D[v1][v2] == D[v1][j] + D[j][v2]) A[i][D[v1][j]].push_back(j);
for (j = 0; j <= D[v1][v2]; j++)
if ((int)A[i][j].size() == 1) M[A[i][j][0]].push_back(i);
}
for (i = 0; i < n; i++) Res[i] = ((int)1e9);
Res[b] = 0;
while (fl) {
fl = 0;
for (i = 0; i < q; i++)
for (j = E[i]; j >= 0; j--)
for (k = 0; k < (int)A[i][j].size();
CM[i][v] = min(((l != 0) ? (CM[i][v]) : (((int)1e9))), Res[v]),
k++)
for (v = A[i][j][k], CM[i][v] = 0, l = 0; l < (int)A[i][j + 1].size();
l++) {
nv = A[i][j + 1][l];
if (D[v][nv] == 1) CM[i][v] = max(CM[i][v], CM[i][nv]);
}
for (i = 0; i < n; i++)
for (j = 0; j < (int)M[i].size(); j++)
if (Res[i] > CM[M[i][j]][i] + 1) fl = 1, Res[i] = CM[M[i][j]][i] + 1;
}
(Res[a] > ((int)1e9) / 2) ? (puts("-1")) : (printf("%d\n", Res[a]));
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxN = 105;
const int maxM = maxN * maxN;
const int INF = 0x3f3f3f3f;
int a[maxN][maxN];
int b[maxN][maxN];
int c[maxN][maxN];
int s[maxN];
int t[maxN];
int ans[maxN];
int g[maxN], f[maxN];
int N, M, S, T;
int cnt;
int dfs(int x, int y) {
if (g[x] == cnt) return f[x];
g[x] = cnt;
int Ans = -1;
for (int i = 1; i <= N; i++)
if (a[x][i] == 1 && a[x][i] + a[i][y] == a[x][y]) Ans = max(Ans, dfs(i, y));
if (Ans < 0) Ans = a[0][0];
return f[x] = min(Ans, ans[x] + 1);
}
int main() {
scanf("%d%d%d%d", &N, &M, &S, &T);
memset(a, INF, sizeof(a));
for (int i = 1; i <= N; i++) a[i][i] = 0;
for (int i = 1; i <= M; i++) {
int u, v;
scanf("%d%d", &u, &v);
a[u][v] = 1;
}
for (int k = 1; k <= N; ++k)
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
scanf("%d", &M);
for (int i = 1; i <= M; i++) {
int x, y;
scanf("%d%d", &x, &y);
for (int j = 1; j <= N; j++)
if (a[x][j] != INF && a[x][j] + a[j][y] == a[x][y]) b[i][a[x][j]]++;
for (int j = 1; j <= N; j++)
if (a[x][j] != INF && a[x][j] + a[j][y] == a[x][y] && b[i][a[x][j]] == 1)
c[i][j] = 1;
s[i] = x;
t[i] = y;
}
memset(ans, INF, sizeof(ans));
ans[T] = 0;
for (;;) {
bool flag = false;
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++) {
if (c[i][j]) {
++cnt;
int tmp = dfs(j, t[i]);
if (tmp < ans[j]) ans[j] = tmp, flag = true;
}
}
}
if (flag == false) break;
}
printf("%d\n", ans[S] != INF ? ans[S] : -1);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int Get() {
char c;
while (c = getchar(), c < '0' || c > '9')
;
int X = 0;
while (c >= '0' && c <= '9') {
X = X * 10 + c - 48;
c = getchar();
}
return X;
}
const int inf = 1000000000;
int main() {
int N = Get(), M = Get(), S = Get() - 1, T = Get() - 1;
static int G[100][100];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) G[i][j] = (i == j) ? 0 : inf;
vector<int> List[100];
for (int i = 0; i < M; i++) {
int X = Get() - 1, Y = Get() - 1;
G[X][Y] = 1;
List[X].push_back(Y);
}
for (int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
int Temp = Get();
static int X[100], Y[100];
M = 0;
while (Temp--) {
X[M] = Get() - 1;
Y[M] = Get() - 1;
if (G[X[M]][Y[M]] != inf) M++;
}
static bool On[100][100];
vector<int> AllBus[100];
for (int i = 0; i < M; i++) {
int U = X[i], V = Y[i];
for (int j = 0; j < N; j++) {
On[i][j] = (G[U][j] + G[j][V] == G[U][V]);
for (int k = 0; On[i][j] && k < N; k++)
if (j != k && G[U][j] == G[U][k] && G[j][V] == G[k][V])
On[i][j] = false;
if (On[i][j]) AllBus[j].push_back(i);
}
}
static int DP[100][100];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) DP[i][j] = (i == T) ? 0 : inf;
while (true) {
bool Change = false;
for (int V = 0; V < N; V++)
for (int Bus = 0; Bus < M; Bus++) {
int Worst = 0;
bool Flag = false;
for (int i = 0; i < List[V].size(); i++) {
int U = List[V][i];
if (G[V][Y[Bus]] != G[U][Y[Bus]] + 1) continue;
Worst = max(Worst, DP[U][Bus]);
Flag = true;
}
if (Worst < DP[V][Bus] && Flag) {
DP[V][Bus] = Worst;
Change = true;
}
for (int i = 0; i < AllBus[V].size(); i++) {
int New = AllBus[V][i];
if (DP[V][New] + 1 < DP[V][Bus]) {
DP[V][Bus] = DP[V][New] + 1;
Change = true;
}
}
}
if (!Change) break;
}
int Ans = inf;
for (int i = 0; i < M; i++)
if (On[i][S]) Ans = min(Ans, DP[S][i] + 1);
printf("%d\n", (Ans == inf) ? -1 : Ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int h[maxn], H[maxn][maxn], n, fi, se, m, qq, amad[maxn][maxn], dp[maxn],
Ans[maxn];
bool mad[maxn];
bool e[maxn];
pair<long long, long long> a[maxn];
queue<int> q;
vector<int> v[maxn], g[maxn], kh, ert[maxn][maxn];
void dfs(int i, int R) {
for (auto A : g[i]) {
if (!amad[R][A] && H[R][A] + 1 == H[R][i]) {
amad[R][A] = 1, dfs(A, R);
}
}
}
void sa(int R) {
H[R][a[R].first] = 1;
q.push(a[R].first);
while (q.size()) {
int x = q.front();
q.pop();
for (auto A : v[x])
if (!H[R][A]) H[R][A] = H[R][x] + 1, q.push(A);
}
amad[R][a[R].second] = 1;
dfs(a[R].second, R);
for (int y = 1; y <= n; y++) {
H[R][y] *= amad[R][y];
if (H[R][y]) ert[R][H[R][y]].push_back(y);
}
mad[R] = (H[R][a[R].first] != 0);
}
void bfs_dp(int R) {
for (int y = 1; y <= n; y++) dp[y] = 1;
dp[a[R].second] = (h[a[R].second] != 0);
for (int o = H[R][a[R].second] - 1; o; o--) {
for (auto A : ert[R][o]) {
if (h[A]) continue;
for (auto B : v[A]) {
if (H[R][B] == H[R][A] + 1 && !dp[B]) {
dp[A] = 0;
continue;
}
}
}
}
for (int y = 1; y <= n; y++) {
if (e[y] || (dp[y] && (!h[y]) && ert[R][H[R][y]].size() == 1)) e[y] = 1;
}
}
void upd(int u) {
for (int t = 0; t < qq; t++)
if (mad[t]) bfs_dp(t), memset(dp, 0, sizeof dp);
kh.clear();
for (int y = 1; y <= n; y++)
if (e[y]) kh.push_back(y), h[y] = u;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> fi >> se;
for (int y = 1; y <= m; y++) {
int i, j;
cin >> i >> j;
v[i].push_back(j);
g[j].push_back(i);
}
cin >> qq;
for (int y = 0; y < qq; y++) cin >> a[y].first >> a[y].second, sa(y);
h[se] = 1;
kh.push_back(se);
for (int y = 2; y <= n && kh.size(); y++) upd(y), memset(e, 0, sizeof e);
cout << h[fi] - 1;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline char gc() {
static const long long L = 233333;
static char sxd[L], *sss = sxd, *ttt = sxd;
if (sss == ttt) {
ttt = (sss = sxd) + fread(sxd, 1, L, stdin);
if (sss == ttt) {
return EOF;
}
}
return *sss++;
}
inline char readalpha() {
char c = gc();
for (; !isalpha(c); c = gc())
;
return c;
}
inline char readchar() {
char c = gc();
for (; c == ' '; c = gc())
;
return c;
}
template <class T>
inline bool read(T& x) {
bool flg = false;
char c = gc();
x = 0;
for (; !isdigit(c); c = gc()) {
if (c == '-') {
flg = true;
} else if (c == EOF) {
return false;
}
}
for (; isdigit(c); c = gc()) {
x = (x << 1) + (x << 3) + (c ^ 48);
}
if (flg) {
x = -x;
}
return true;
}
template <class T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x < 10) {
putchar(x | 48);
return;
}
write(x / 10);
putchar((x % 10) | 48);
}
template <class T>
inline void writesp(T x) {
write(x);
putchar(' ');
}
template <class T>
inline void writeln(T x) {
write(x);
puts("");
}
const int maxn = 105;
const int inf = 0x3f3f3f3f;
int n, m, F, T, K;
int mp[maxn][maxn];
int f[maxn][maxn];
int g[maxn][maxn];
int t[maxn][maxn];
int cnt[maxn][maxn];
bitset<maxn> cut[maxn][maxn];
struct Bus {
int f, t;
} bus[maxn];
inline bool instp(int u, int v, int x) { return f[u][x] + f[x][v] == f[u][v]; }
inline bool instp(int u, int v, int x, int y) {
return f[u][x] + 1 + f[y][v] == f[u][v];
}
struct ZT {
int zh, to, bu;
ZT(int z, int t, int b) { zh = z, to = t, bu = b; }
};
int main() {
memset(f, 0x3f, sizeof(f));
memset(g, 0x3f, sizeof(g));
read(n), read(m), read(F), read(T);
for (int i = 1; i <= m; ++i) {
int u, v;
read(u), read(v);
f[u][v] = mp[u][v] = 1;
}
for (int i = 1; i <= n; ++i) {
f[i][i] = 0;
}
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
if (i != k) {
for (int j = 1; j <= n; ++j) {
if (j != k) {
if (f[i][j] > f[i][k] + f[k][j]) {
f[i][j] = f[i][k] + f[k][j];
cut[i][j] = cut[i][k] | cut[k][j];
cut[i][j][k] = 1;
} else if (f[i][j] == f[i][k] + f[k][j]) {
cut[i][j] &= cut[i][k] | cut[k][j];
}
}
}
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cut[i][j][i] = cut[i][j][j] = 1;
}
}
read(K);
deque<ZT> q;
q.clear();
for (int i = 1; i <= K; ++i) {
read(bus[i].f), read(bus[i].t);
if (instp(bus[i].f, bus[i].t, T)) {
g[T][i] = 1;
q.emplace_back(1, T, i);
}
for (int j = 1; j <= n; ++j) {
for (int k = 1; k <= n; ++k) {
if (mp[j][k] && instp(bus[i].f, bus[i].t, j, k)) {
cnt[j][i]++;
}
}
}
}
while (!q.empty()) {
ZT nowzt = q.front();
q.pop_front();
if (nowzt.zh > g[nowzt.to][nowzt.bu]) {
continue;
}
int now = nowzt.to;
int bs = nowzt.bu;
if (cut[bus[bs].f][bus[bs].t][now]) {
for (int i = 1; i <= K; ++i) {
if (i != bs && instp(bus[i].f, bus[i].t, now) &&
g[now][i] > g[now][bs] + 1) {
g[now][i] = g[now][bs] + 1;
q.emplace_back(g[now][i], now, i);
}
}
}
for (int i = 1; i <= n; ++i) {
if (mp[i][now] && instp(bus[bs].f, bus[bs].t, i, now)) {
t[i][bs] = max(g[now][bs], t[i][bs]);
cnt[i][bs]--;
if (!cnt[i][bs] && t[i][bs] < g[i][bs]) {
g[i][bs] = t[i][bs];
q.emplace_front(g[i][bs], i, bs);
}
}
}
}
int ans = inf;
for (int i = 1; i <= K; ++i) {
if (cut[bus[i].f][bus[i].t][F]) {
ans = min(ans, g[F][i]);
}
}
if (ans < inf) {
writeln(ans);
} else {
puts("-1");
}
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int g[105][105];
int n, m, S, T;
int d[105][105], cnt[105][105] = {0};
int U[105], V[105];
int nu;
int f[105][105];
int lef[105][105] = {0};
int vis[105][105] = {0};
int on(int u, int s, int t) {
if (d[s][u] + d[u][t] != d[s][t]) return 0;
if (cnt[s][u] * cnt[u][t] == cnt[s][t]) return 2;
return 1;
}
int qu[10005], qi[10005];
int p, q;
int main() {
scanf("%d%d%d%d", &n, &m, &S, &T);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = d[i][j] = 1000000000;
for (int i = 1; i <= n; i++) g[i][i] = d[i][i] = 0, cnt[i][i] = 1;
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
g[x][y] = d[x][y] = cnt[x][y] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != k && k != j && j != i) {
if (d[i][k] + d[k][j] < d[i][j]) {
d[i][j] = d[i][k] + d[k][j];
cnt[i][j] = cnt[i][k] * cnt[k][j];
} else if (d[i][k] + d[k][j] == d[i][j])
cnt[i][j] += cnt[i][k] * cnt[k][j];
}
scanf("%d", &nu);
for (int i = 1; i <= nu; i++) {
scanf("%d%d", &U[i], &V[i]);
if (d[U[i]][V[i]] >= 1000000000) nu--, i--;
}
for (int i = 1; i <= n; i++)
for (int j = 0; j <= nu; j++) f[i][j] = 1000000000;
f[T][0] = 0;
for (int u = 1; u <= n; u++)
for (int i = 1; i <= nu; i++)
if (on(u, U[i], V[i]))
for (int v = 1; v <= n; v++)
if (g[u][v] == 1 && on(v, u, V[i])) lef[u][i]++;
for (int dis = 0; dis <= nu; dis++) {
p = q = 0;
for (int u = 1; u <= n; u++)
for (int i = 0; i <= nu; i++)
if (f[u][i] == dis) {
qu[q] = u;
qi[q++] = i;
}
while (p != q) {
int v = qu[p], i = qi[p++];
if (i && on(v, U[i], V[i]) == 2) f[v][0] = min(f[v][0], f[v][i] + 1);
if (i && !vis[v][0]) {
for (int u = 1; u <= n; u++)
if (g[u][v] == 1 && on(v, u, V[i]) && on(u, U[i], V[i])) {
lef[u][i]--;
if (!lef[u][i]) {
f[u][i] = dis;
qu[q] = u;
qi[q++] = i;
}
}
} else if (!i) {
for (int j = 1; j <= nu; j++)
if (!vis[v][j]) {
for (int u = 1; u <= n; u++)
if (g[u][v] == 1 && on(v, u, V[j]) && on(u, U[j], V[j])) {
lef[u][j]--;
if (!lef[u][j]) {
f[u][j] = dis;
qu[q] = u;
qi[q++] = j;
}
}
}
}
vis[v][i] = 1;
}
}
if (f[S][0] >= 1000000000)
printf("-1\n");
else
printf("%d\n", f[S][0]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 109;
const int INF = 0x3f3f3f3f;
int m, n, a, b;
int dis[N][N];
int sv[N], tv[N], s, t;
vector<int> to[N];
bool reachable[N], tmp[N];
bool dfs(int now) {
if (reachable[now]) {
return true;
}
if (now == t) {
return false;
}
int siz = to[now].size();
for (int i = 0; i < siz; i++) {
if (dis[s][now] + 1 == dis[s][to[now][i]] &&
dis[s][t] == dis[s][to[now][i]] + dis[to[now][i]][t]) {
if (!dfs(to[now][i])) {
return false;
}
}
}
return true;
}
int main() {
scanf("%d %d %d %d", &n, &m, &a, &b);
memset(dis, 0x3f, sizeof(dis));
memset(reachable, false, sizeof(reachable));
reachable[b] = true;
for (int i = 1; i <= n; i++) {
dis[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
to[u].push_back(v);
dis[u][v] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
}
}
}
int k;
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d %d", &sv[i], &tv[i]);
}
int ans;
memcpy(tmp, reachable, sizeof(tmp));
for (ans = 1; ans <= n; ans++) {
for (int i = 0; i < k; i++) {
s = sv[i], t = tv[i];
if (dis[s][t] == INF) {
continue;
}
for (int j = 1; j <= n; j++) {
if (dis[s][t] == dis[s][j] + dis[j][t]) {
bool fail = false;
for (int l = 1; l <= n; l++) {
if (l != j && dis[s][t] == dis[s][l] + dis[l][t] &&
dis[s][l] == dis[s][j]) {
fail = true;
}
}
if (!fail) {
if (dfs(j)) {
tmp[j] = true;
}
}
}
}
}
memcpy(reachable, tmp, sizeof(tmp));
if (reachable[a]) {
break;
}
}
if (ans > n) {
ans = -1;
}
printf("%d\n", ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int sum = 0;
char c = getchar();
bool f = 0;
while (c < '0' || c > '9') {
if (c == '-') f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
sum = sum * 10 + c - '0';
c = getchar();
}
if (f) return -sum;
return sum;
}
const int orz = 1000000000;
const int N = 105;
int n, m, c, S, T, d[N][N], fr[N], to[N];
vector<int> SD[N];
bool cn[N][N];
int f[N][N];
inline bool DP() {
int i, j, k, w, x, y;
bool a = 0, b;
for (i = 1; i <= n; i++)
for (j = 1; j <= c; j++) {
b = 0;
w = -1;
x = fr[j];
y = to[j];
for (k = 1; k <= n; k++)
if (d[i][k] == 1 && d[k][y] + 1 == d[i][y]) w = max(w, f[k][j]), b = 1;
if (b && w < f[i][j]) a = 1, f[i][j] = w;
if (cn[i][j])
for (k = 1; k <= c; k++)
if (f[i][j] + 1 < f[i][k]) f[i][k] = f[i][j] + 1, a = 1;
}
return a;
}
int main() {
int i, j, k, x, y;
n = read();
m = read();
S = read();
T = read();
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (i != j) d[i][j] = orz;
for (i = 1; i <= m; i++) x = read(), y = read(), d[x][y] = 1;
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
c = read();
for (i = 1; i <= c; i++) {
fr[i] = x = read();
to[i] = y = read();
if (d[x][y] == orz) {
i--;
c--;
continue;
}
for (j = 0; j <= 100; j++) SD[j].clear();
for (j = 1; j <= n; j++)
if (d[x][j] + d[j][y] == d[x][y]) SD[d[x][j]].push_back(j);
for (j = 0; j <= 100; j++)
if (SD[j].size() == 1) cn[SD[j][0]][i] = 1;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= c; j++) f[i][j] = (i == T) ? 0 : orz;
int ans = orz;
while (DP())
;
for (i = 1; i <= c; i++)
if (cn[S][i]) ans = min(ans, f[S][i] + 1);
printf("%d", ans == orz ? -1 : ans);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
constexpr int N = 100 + 10;
constexpr int MOD = 1e9 + 7;
int n, m, a, b, dist[N], main_dist[N][N], u, v, s[N], t[N], limit,
new_dist[N][N];
vector<int> node[N], bus[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> a >> b;
memset(main_dist, 63, sizeof(main_dist));
memset(dist, -1, sizeof(dist));
for (int i = 0; i < m; i++) {
cin >> u >> v;
main_dist[u][v] = 1;
node[u].push_back(v);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
main_dist[j][k] =
min(main_dist[j][k], main_dist[j][i] + main_dist[i][k]);
cin >> limit;
for (int i = 0; i < limit; i++) cin >> s[i] >> t[i];
for (int i = 1; i <= n; i++) {
memset(new_dist, 63, sizeof(new_dist));
for (int j = 1; j <= n; j++) {
if (j == i) continue;
for (auto k : node[j])
if (k != i) new_dist[j][k] = 1;
}
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++)
new_dist[k][l] = min(new_dist[k][l], new_dist[k][j] + new_dist[j][l]);
for (int j = 0; j < limit; j++)
if (main_dist[s[j]][t[j]] < new_dist[s[j]][t[j]] && t[j] != i)
bus[i].push_back(t[j]);
}
dist[b] = 0;
for (int i = 1; i <= n; i++) {
vector<int> vec;
memset(new_dist, 63, sizeof(new_dist));
for (int j = 1; j <= n; j++) {
if (dist[j] != -1) continue;
for (auto k : node[j])
if (dist[k] == -1) new_dist[j][k] = 1;
}
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= n; l++)
new_dist[k][l] = min(new_dist[k][l], new_dist[k][j] + new_dist[j][l]);
for (int j = 1; j <= n; j++) {
if (dist[j] != -1) continue;
for (auto k : bus[j]) {
if (main_dist[j][k] < new_dist[j][k]) {
vec.push_back(j);
break;
}
}
}
for (auto j : vec) dist[j] = i;
}
cout << dist[a] << '\n';
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int vis[105], T, num[105], dp[105], f[105][105], n, m, K, DP[105], s[105],
t[105];
bool b[105][105];
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int dfs(int u, int k) {
if (vis[u] == T) return DP[u];
int tmp = -1;
vis[u] = T;
for (int v = 1; v <= n; v++)
if (f[u][v] == 1 & f[u][t[k]] == f[v][t[k]] + 1) tmp = max(tmp, dfs(v, k));
if (tmp == -1) tmp = 1e9;
if (tmp > dp[u]) tmp = dp[u];
return DP[u] = tmp;
}
int main() {
scanf("%d%d%d%d", &n, &m, &s[0], &t[0]);
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++) f[i][j] = f[j][i] = 1e9;
for (int i = 1, u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
f[u][v] = 1;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
scanf("%d", &K);
for (int i = 1; i <= K; i++) {
scanf("%d%d", &s[i], &t[i]);
if (f[s[i]][t[i]] == 1e9) continue;
for (int j = 1; j <= n; j++)
if (f[s[i]][j] + f[j][t[i]] == f[s[i]][t[i]]) num[f[s[i]][j]]++;
for (int j = 1; j <= n; j++)
if (f[s[i]][j] + f[j][t[i]] == f[s[i]][t[i]]) {
if (num[f[s[i]][j]] == 1) b[i][j] = 1;
num[f[s[i]][j]] = 0;
}
}
for (int i = 1; i <= n; i++) dp[i] = DP[i] = 1e9;
dp[t[0]] = 0;
bool flag = 1;
while (flag) {
flag = 0;
for (int i = 1; i <= K; i++)
for (int j = 1; j <= n; j++)
if (b[i][j]) {
T++;
int tmp = dfs(j, i) + 1;
if (tmp < dp[j]) {
flag = 1;
dp[j] = tmp;
}
}
}
if (dp[s[0]] == 1e9) dp[s[0]] = -1;
printf("%d\n", dp[s[0]]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100 + 5;
const int inf = 1e9;
int f[N][N], n, m, A, B, nb;
int S[N], T[N];
vector<int> pss[N][N];
int res[N], t[N];
void relax(int &a, int b) {
if (a < b) a = b;
}
void upd(int &a, int b) {
if (a > b) a = b;
}
void floyd() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (f[i][j] == 0 && i != j) f[i][j] = inf;
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j) upd(f[i][j], f[i][k] + f[k][j]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &A, &B);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
f[a][b] = 1;
}
floyd();
for (int i = 1; i <= n; i++) res[i] = inf;
scanf("%d", &nb);
for (int i = 1; i <= nb; i++) scanf("%d%d", &S[i], &T[i]);
for (int i = 1; i <= nb; i++) {
if (f[S[i]][T[i]] == inf) continue;
for (int j = 1; j <= n; j++)
if (f[S[i]][j] + f[j][T[i]] == f[S[i]][T[i]])
pss[i][f[S[i]][j]].push_back(j);
}
res[B] = 0;
int updd = 1;
int tim = 0;
while (updd) {
tim++;
updd = 0;
for (int i = 1; i <= nb; i++) {
if (f[S[i]][T[i]] == inf) continue;
for (int j = 1; j <= n; j++) t[i] = 0;
t[T[i]] = res[T[i]];
for (int len = f[S[i]][T[i]] - 1; len >= 0; len--) {
for (vector<int>::iterator ii = pss[i][len].begin();
ii != pss[i][len].end(); ii++) {
t[*ii] = 0;
for (vector<int>::iterator jj = pss[i][len + 1].begin();
jj != pss[i][len + 1].end(); jj++)
if (f[*ii][*jj] == 1) relax(t[*ii], t[*jj]);
upd(t[*ii], res[*ii]);
}
if (pss[i][len].size() == 1) {
int u = pss[i][len][0];
if (res[u] == inf && t[u] < tim) {
res[u] = tim;
updd = 1;
}
}
}
}
}
printf("%d\n", res[A] == inf ? -1 : res[A]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int dist[maxn][maxn];
int d[maxn][maxn];
int used[maxn][maxn];
vector<vector<int> > es;
int bus[maxn][maxn];
int cnt[maxn];
int main() {
int n, m, a, b;
while (scanf("%d%d%d%d", &n, &m, &a, &b) >= 1) {
--a, --b;
es = vector<vector<int> >(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dist[i][j] = (i == j) ? 0 : ((int)1E9);
for (int i = 0; i < m; i++) {
int s, t;
scanf("%d%d", &s, &t);
--s, --t;
dist[s][t] = 1;
}
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) bus[i][j] = (i == j);
int k;
scanf("%d", &k);
for (int iter = 0; iter < k; iter++) {
int S, T;
scanf("%d%d", &S, &T);
S--, T--;
if (dist[S][T] == ((int)1E9)) continue;
for (int i = 0; i < n; i++) cnt[i] = 0;
for (int i = 0; i < n; i++) {
if (dist[S][T] == dist[S][i] + dist[i][T]) {
cnt[dist[S][i]]++;
}
}
for (int i = 0; i < n; i++) {
if (dist[S][T] == dist[S][i] + dist[i][T] && cnt[dist[S][i]] == 1)
bus[i][T] = 1;
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) d[i][j] = ((int)1E9), used[i][j] = 0;
for (int j = 0; j < n; j++)
if (bus[b][j]) d[b][j] = 0;
for (int iter = 0; iter < n * n; iter++) {
int v = -1, tr = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (!used[i][j] && (v == -1 || d[v][tr] > d[i][j])) v = i, tr = j;
if (v == -1) break;
used[v][tr] = 1;
for (int i = 0; i < n; i++) {
if (bus[v][tr]) d[v][i] = min(d[v][i], d[v][tr] + 1);
if (dist[i][tr] != dist[i][v] + dist[v][tr]) continue;
if (dist[i][v] != 1) continue;
int cur = 0;
for (int y = 0; y < n; y++)
if (dist[i][y] == 1 && dist[i][y] + dist[y][tr] == dist[i][tr])
cur = max(cur, d[y][tr]);
d[i][tr] = min(d[i][tr], cur);
}
}
printf("%d\n", d[a][a] == ((int)1E9) ? -1 : d[a][a]);
}
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& num) {
num = 0;
bool f = true;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = false;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
num = num * 10 + ch - '0';
ch = getchar();
}
num = f ? num : -num;
}
int out[100];
template <class T>
inline void write(T x, char ch) {
if (x == 0) {
putchar('0');
putchar(ch);
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
int num = 0;
while (x) {
out[num++] = (x % 10);
x = x / 10;
}
for (int i = (num - 1); i >= (0); i--) putchar(out[i] + '0');
putchar(ch);
}
int n, m, a, b;
int dis[1005][1005], st[1005], ed[1005], vis[1005], ans[1005], f[1005];
bool must[1005][1005];
int q;
int dfs(int x, int y) {
if (x == y) return ans[x];
if (vis[x]) return f[x];
vis[x] = 1;
f[x] = 0;
for (int i = (1); i <= (n); i++)
if (dis[x][i] == 1 && dis[i][y] + 1 == dis[x][y])
f[x] = max(f[x], dfs(i, y));
return f[x] = min(f[x], ans[x]);
}
int main() {
cin >> n >> m >> a >> b;
for (int i = (1); i <= (n); i++)
for (int j = (i + 1); j <= (n); j++) dis[i][j] = dis[j][i] = 107000000;
for (int i = (1); i <= (m); i++) {
int u, v;
read(u);
read(v);
dis[u][v] = 1;
}
for (int k = (1); k <= (n); k++)
for (int i = (1); i <= (n); i++)
for (int j = (1); j <= (n); j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
cin >> q;
for (int i = (1); i <= (q); i++) {
read(st[i]);
read(ed[i]);
if (dis[st[i]][ed[i]] == 107000000) continue;
memset(vis, 0, sizeof(vis));
for (int j = (1); j <= (n); j++)
if (dis[st[i]][j] + dis[j][ed[i]] == dis[st[i]][ed[i]])
vis[dis[st[i]][j]]++;
for (int j = (1); j <= (n); j++)
if (dis[st[i]][j] + dis[j][ed[i]] == dis[st[i]][ed[i]] &&
vis[dis[st[i]][j]] == 1)
must[i][j] = 1;
}
for (int i = (1); i <= (n); i++) ans[i] = 107000000;
ans[b] = 0;
for (int T = (1); T <= (n); T++) {
for (int i = (1); i <= (q); i++)
if (dis[st[i]][ed[i]] != 107000000) {
memset(vis, 0, sizeof(vis));
for (int j = (1); j <= (n); j++)
if (must[i][j]) ans[j] = min(ans[j], dfs(j, ed[i]) + 1);
}
}
printf("%d\n", ans[a] == 107000000 ? -1 : ans[a]);
return 0;
}
| CPP |
238_E. Meeting Her | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path.
Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.
At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.
Note that Urpal doesn't know buses velocity.
Input
The first line of the input contains four integers n, m, a, b (2 ≤ n ≤ 100; 0 ≤ m ≤ n·(n - 1); 1 ≤ a, b ≤ n; a ≠ b).
The next m lines contain two integers each ui and vi (1 ≤ ui, vi ≤ n; ui ≠ vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct.
The next line contains an integer k (0 ≤ k ≤ 100). There will be k lines after this, each containing two integers si and ti (1 ≤ si, ti ≤ n; si ≠ ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
Output
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
Examples
Input
7 8 1 7
1 2
1 3
2 4
3 4
4 6
4 5
6 7
5 7
3
2 7
1 4
5 7
Output
2
Input
4 4 1 2
1 2
1 3
2 4
3 4
1
1 4
Output
-1 | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, A, B, inf = 1e9, f[110][110], x, y, K, g[110][110], Cnt[110],
d[110][110];
bool vis[110][110], V[110][110], Must[110][110];
vector<int> E1[110], E2[110], E[110][110];
queue<pair<int, int> > h;
void read(int &x) {
char ch = getchar();
int mark = 1;
for (; ch != '-' && (ch < '0' || ch > '9'); ch = getchar())
;
if (ch == '-') mark = -1, ch = getchar();
for (x = 0; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48;
x *= mark;
}
int main() {
read(n);
read(m);
read(A);
read(B);
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++) f[i][j] = f[j][i] = inf;
for (int i = 1; i <= m; i++) {
read(x);
read(y);
f[x][y] = 1;
E1[x].push_back(y);
E2[y].push_back(x);
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
read(K);
for (int i = 1; i <= K; i++) {
read(x);
read(y);
int dis = f[x][y];
if (dis == inf) continue;
memset(Cnt, 0, sizeof(Cnt));
for (int j = 1; j <= n; j++)
if (f[x][j] + f[j][y] == dis) {
V[i][j] = 1;
Cnt[f[x][j]]++;
for (int k = 0; k < E1[j].size(); k++) {
int v = E1[j][k];
if (f[x][j] + 1 + f[v][y] == dis) E[i][v].push_back(j), d[i][j]++;
}
}
for (int j = 1; j <= n; j++)
if (V[i][j] && Cnt[f[x][j]] == 1) Must[i][j] = 1;
}
for (int i = 1; i <= K; i++)
if (V[i][B]) {
vis[B][i] = 1, g[B][i] = 1;
h.push(make_pair(B, i));
}
for (int _ = 1; _ <= K; _++) {
while (h.size()) {
pair<int, int> now = h.front();
h.pop();
int u = now.first, x = now.second;
for (int i = 0; i < E[x][u].size(); i++) {
int v = E[x][u][i];
d[x][v]--;
if (d[x][v] == 0 && !vis[v][x]) {
vis[v][x] = 1;
g[v][x] = g[u][x];
h.push(make_pair(v, x));
}
}
}
for (int i = 1; i <= K; i++)
if (vis[A][i] && Must[i][A]) {
printf("%d\n", _);
return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= K; j++)
if (vis[i][j] && Must[j][i] && g[i][j] == _) {
for (int k = 1; k <= K; k++)
if (!vis[i][k] && V[k][i]) {
g[i][k] = g[i][j] + 1;
vis[i][k] = 1;
h.push(make_pair(i, k));
}
}
}
printf("-1\n");
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.