| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 330311 | phathnv | Bajka (COCI20_bajka) | C++11 | 61 ms | 2408 KiB | 
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define taskname "Bajka"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 302;
const int INF = 1e9 + 1;
struct data{
    int x, y, val;
    data(int _x, int _y, int _val){
        x = _x;
        y = _y;
        val = _val;
    }
};
bool operator < (const data &d1, const data &d2){
    return d1.val > d2.val;
}
int n, m, dp[N][N];
char sca[N], fav[N];
void readInput(){
    cin >> n >> m >> (sca + 1) >> (fav + 1);
}
void solve(){
    for(int i = 0; i <= m; i++)
        for(int j = 0; j <= n; j++)
            dp[i][j] = INF;
    priority_queue <data> pq;
    for(int j = 1; j <= n; j++)
        if (fav[1] == sca[j]){
            dp[1][j] = 0;
            pq.push(data(1, j, 0));
        }
    while (!pq.empty()){
        int x = pq.top().x;
        int y = pq.top().y;
        int val = pq.top().val;
        pq.pop();
        if (dp[x][y] != val)
            continue;
        if (x == m){
            cout << val;
            return;
        }
        for(int z = 1; z <= n; z++)
            if (y != z && sca[z] == sca[y] && dp[x][z] > dp[x][y] + abs(y - z)){
                dp[x][z] = dp[x][y] + abs(y - z);
                pq.push(data(x, z, dp[x][z]));
            }
        for(int z = y - 1; z <= y + 1; z++)
            if (y != z && sca[z] == fav[x + 1] && dp[x + 1][z] > dp[x][y] + abs(y - z)){
                dp[x + 1][z] = dp[x][y] + abs(y - z);
                pq.push(data(x + 1, z, dp[x + 1][z]));
            }
    }
    cout << -1;
}
int main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    readInput();
    solve();
    return 0;
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
