Submission #330310

#TimeUsernameProblemLanguageResultExecution timeMemory
330310phathnvBajka (COCI20_bajka)C++11
20 / 70
19 ms896 KiB
#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 (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])); } 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)

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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...