1일1알

백준 10710번 실크로드 C++ 본문

알고리즘

백준 10710번 실크로드 C++

영춘권의달인 2022. 8. 3. 12:33

출처 : https://www.acmicpc.net/problem/10710

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];
}

'알고리즘' 카테고리의 다른 글

백준 1039번 교환 C++  (0) 2022.08.06
백준 21924번 도시 건설 C++  (0) 2022.08.05
백준 17142번 연구소 3 C++  (0) 2022.08.02
백준 13460번 구슬 탈출 2 C++  (0) 2022.08.01
백준 15685번 드래곤 커브 C++  (0) 2022.07.31