답안 #330311

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
330311 2020-11-24T18:11:12 Z phathnv Bajka (COCI20_bajka) C++11
70 / 70
61 ms 2408 KB
#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

bajka.cpp: In function 'int main()':
bajka.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   74 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bajka.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   75 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 748 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 748 KB Output is correct
5 Correct 1 ms 748 KB Output is correct
6 Correct 1 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 748 KB Output is correct
2 Correct 6 ms 748 KB Output is correct
3 Correct 5 ms 748 KB Output is correct
4 Correct 5 ms 748 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 23 ms 1004 KB Output is correct
8 Correct 61 ms 2408 KB Output is correct
9 Correct 25 ms 1644 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 18 ms 912 KB Output is correct