알고리즘
백준 10710번 실크로드 C++
영춘권의달인
2022. 8. 3. 12:33
i번째 도시에서 j번째날의 전날의 경우의 수 :
1. i번째 도시에서 j-1번째 날에서 움직이지 않는 경우
2. i-1번째 도시에서 j-1번째 날에서 움직이는 경우
cache[i][j] = min(cache[i][j - 1], cache[i - 1][j - 1] + dist[i] * weather[j]);
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n, m;
const int MAX = 987654321;
vector<vector<int>> cache;
vector<int> dist;
vector<int> weather;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
dist = vector<int>(n + 1, 0);
weather = vector<int>(m + 1, 0);
cache = vector<vector<int>>(n + 1, vector<int>(m + 1, MAX));
for (int i = 1; i <= n; i++) {
cin >> dist[i];
}
for (int i = 1; i <= m; i++) {
cin >> weather[i];
}
for (int i = 0; i <= m; i++) {
cache[0][i] = 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cache[i][j] = min(cache[i][j - 1], cache[i - 1][j - 1] + dist[i] * weather[j]);
}
}
cout << cache[n][m];
}