Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ue5
- 백트래킹
- 다익스트라
- 시뮬레이션
- 수학
- 브루트포스
- XR Interaction Toolkit
- 문자열
- 다이나믹 프로그래밍
- 트리
- 알고리즘
- 유니티
- 정렬
- 스택
- Team Fortress 2
- c++
- 누적 합
- 그래프
- 구현
- 백준
- DFS
- 그리디 알고리즘
- VR
- 유니온 파인드
- Unreal Engine 5
- 투 포인터
- 우선순위 큐
- 재귀
- BFS
- 자료구조
Archives
- Today
- Total
1일1알
백준 24954번 물약 구매 C++ 본문

백트래킹을 이용해서 물약을 구매하는 모든 순서를 구해서 순서에 맞춰서 가격을 줄여가면서 나온 동전 중에 가장 적게 나온 동전의 값을 구했다.
#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 <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int n;
vector<int> potions(11);
vector<int> products;
vector<bool> visited(11, false);
vector<vector<pair<int, int>>> discounts(11, vector<pair<int, int>>());
int ans = 987654321;
void BT() {
if (products.size() >= n) {
int coin = 0;
vector<int> prices;
prices.assign(potions.begin(), potions.end());
for (auto a : products) {
coin += prices[a];
for (auto b : discounts[a]) {
prices[b.first] = max(1, prices[b.first] - b.second);
}
}
ans = min(ans, coin);
return;
}
for (int i = 1; i <= n; i++) {
if (visited[i]) continue;
visited[i] = true;
products.push_back(i);
BT();
visited[i] = false;
products.pop_back();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> potions[i];
}
for (int i = 1; i <= n; i++) {
int p;
cin >> p;
for (int j = 0; j < p; j++) {
int a, d;
cin >> a >> d;
discounts[i].push_back({ a,d });
}
}
BT();
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 15918번 랭퍼든 수열쟁이야!! C++ (0) | 2022.05.18 |
---|---|
백준 2470번 두 용액 C++ (0) | 2022.05.17 |
백준 17359번 전구 길만 걷자 C++ (0) | 2022.05.14 |
백준 11728번 배열 합치기 C++ (0) | 2022.05.13 |
백준 10819번 차이를 최대로 C++ (0) | 2022.05.12 |