일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- NEXT
- BFS
- 회고록
- dfs
- 세그먼트 트리
- object detection
- back propagation
- 자바스크립트
- 분할 정복
- 알고리즘
- 이분 탐색
- 너비 우선 탐색
- c++
- 조합론
- pytorch
- 문자열
- 플로이드 와샬
- dropout
- tensorflow
- 2023
- lazy propagation
- 크루스칼
- DP
- 우선 순위 큐
- Overfitting
- 다익스트라
- 백트래킹
- 미래는_현재와_과거로
- 가끔은_말로
- 가끔은 말로
Archives
- Today
- Total
Doby's Lab
백준 5847번: Milk Scheduling (C++) 본문
https://www.acmicpc.net/problem/5847
5847번: Milk Scheduling
Farmer John's N cows (1 <= N <= 10,000) are conveniently numbered 1..N. Each cow i takes T(i) units of time to milk. Unfortunately, some cows must be milked before others, owing to the layout of FJ's barn. If cow A must be milked before cow B, then FJ need
www.acmicpc.net
Solved By: Topological Sort, DP
어떤 소 B는 소 A가 우유를 먹기 전까지는 절대 못 먹습니다. A가 먹어야 B의 먹을 순서가 된다는 뜻은 Topological Sort를 사용해 모든 소들의 순서 관계를 파악해야 합니다. 그 과정에서 각 소가 먹는 데에 걸리는 시간이 있고, 모든 소가 먹기까지의 걸리는 시간을 구해야 합니다.
Topological Sort를 구하는 과정에서 DP를 돌려 최댓값을 구하면 되는 문제였습니다.
#include <iostream>
#include <vector>
#include <queue>
#define MAX 10001
#define INF 1e9
using namespace std;
int cost[MAX];
int indegree[MAX];
vector<int> adj[MAX];
vector<int> cache(MAX, 0);
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> cost[i];
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b;
adj[a].push_back(b);
indegree[b]++;
}
queue<int> q;
for(int i = 1; i <= n; i++){
if(!indegree[i]){
q.push(i);
cache[i] = cost[i];
}
}
int result = 0;
while(!q.empty()){
int now = q.front();
q.pop();
for(int i = 0; i < adj[now].size(); i++){
int next = adj[now][i];
cache[next] = max(cache[next], cache[now] + cost[next]);
if(--indegree[next] == 0){
q.push(next);
}
}
}
for(int i = 1; i <= n; i++){
result = max(result, cache[i]);
}
cout << result;
return 0;
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 15824번: 너 봄에는 캡사이신이 맛있단다 (C++) (0) | 2022.05.31 |
---|---|
백준 14942번: 개미 (C++) (0) | 2022.05.31 |
백준 1948번: 임계경로 (C++) (0) | 2022.05.30 |
백준 16946번: 벽 부수고 이동하기 4 (C++) (0) | 2022.05.30 |
백준 1926번: 그림 (C++) (0) | 2022.05.29 |