# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
880997 |
2023-11-30T10:21:44 Z |
TAhmed33 |
Bajka (COCI20_bajka) |
C++ |
|
182 ms |
6812 KB |
#include <bits/stdc++.h>
using namespace std;
vector <pair <int, int>> adj[301];
const int inf = 2e5 + 25;
int dist[301][301];
int n, m; string s, t;
int main () {
cin >> n >> m >> s >> t;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (s[i] == s[j]) {
adj[i].push_back({j, j - i});
adj[j].push_back({i, j - i});
}
}
}
for (int i = 1; i < n; i++) {
adj[i - 1].push_back({i, 1});
adj[i].push_back({i - 1, 1});
}
for (int i = 0; i + 1 < n; i++) {
adj[i + 1].push_back({i, 1});
adj[i].push_back({i + 1, 1});
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dist[i][j] = inf;
}
}
priority_queue <vector <int>, vector <vector <int>>, greater <vector <int>>> pq;
for (int i = 0; i < n; i++) {
if (s[i] == t[0]) {
dist[i][0] = 0;
pq.push({0, i, 0});
}
}
int mn = inf;
while (!pq.empty()) {
auto k = pq.top();
pq.pop();
if (k[0] > dist[k[1]][k[2]]) continue;
if (k[2] == m - 1) {
mn = min(mn, k[0]);
continue;
}
for (auto [node, val] : adj[k[1]]) {
if (s[node] == s[k[1]]) {
if (k[0] + val < dist[node][k[2]]) {
dist[node][k[2]] = k[0] + val;
pq.push({k[0] + val, node, k[2]});
}
}
if (s[node] == t[k[2] + 1] && abs(node - k[1]) == 1) {
if (k[0] + val < dist[node][k[2] + 1]) {
dist[node][k[2] + 1] = k[0] + val;
pq.push({k[0] + val, node, k[2] + 1});
}
}
}
}
if (mn >= inf) {
cout << "-1\n";
} else {
cout << mn << '\n';
}
}
Compilation message
bajka.cpp: In function 'int main()':
bajka.cpp:46:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
46 | for (auto [node, val] : adj[k[1]]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
860 KB |
Output is correct |
2 |
Correct |
5 ms |
1112 KB |
Output is correct |
3 |
Correct |
3 ms |
860 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
33 ms |
1740 KB |
Output is correct |
8 |
Correct |
182 ms |
6812 KB |
Output is correct |
9 |
Correct |
80 ms |
4876 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
18 ms |
1272 KB |
Output is correct |