일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 이분 탐색
- tensorflow
- object detection
- 미래는_현재와_과거로
- 크루스칼
- 가끔은_말로
- 백트래킹
- 너비 우선 탐색
- 자바스크립트
- 플로이드 와샬
- NEXT
- DP
- 세그먼트 트리
- 분할 정복
- 회고록
- dropout
- BFS
- c++
- dfs
- 다익스트라
- Overfitting
- 우선 순위 큐
- 가끔은 말로
- 조합론
- back propagation
- pytorch
- 문자열
- lazy propagation
- 2023
- 알고리즘
Archives
- Today
- Total
Doby's Lab
백준 1585번: 경찰 (C++) 본문
https://www.acmicpc.net/problem/1585
1585번: 경찰
첫째 줄에 차가 총 몇 대 있는지 주어진다. 이 값을 N이라고 하고, 50보다 작거나 같은 자연수이다. 둘째 줄에는 차가 동호도로에 들어가는 시간이 주어진다. 총 N개의 수가 공백 한 칸을 사이에
www.acmicpc.net
Solved By: MCMF
네트워크 모델링에 있어서 다음 경우들을 생각하면 됩니다.
1) 출발 시간과 도착 시간이 매칭이 안 되는 경우
>> 도착 시간이 출발 시간보다 빠르다면 이건 말이 되지 않는 경우입니다.
>> edge를 만들어줄 필요가 없습니다.
2) 벌금을 내지 않는 경우
>> edge를 만들어주고, 벌금은 내지 않으므로 cost값은 0입니다.
3) 벌금을 내는 경우
>> edge를 만들어주고, 문제에서 제시된 규칙에 따라 벌금을 측정하여 cost값으로 할당합니다.
최대 비용은 모든 cost값을 음수로 만드는 테크닉을 이용합니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#define MAX 103
#define SOURCE 101
#define SINK 102
#define INF 1e9
using namespace std;
int n;
int io[MAX];
vector<int> adj[MAX];
int c[MAX][MAX], f[MAX][MAX];
int cost[MAX][MAX];
int T, F;
int MCMF(int source, int sink){
int result = 0, cnt = 0;
while(1){
int parent[MAX], dist[MAX];
fill(parent, parent + MAX, -1);
fill(dist, dist + MAX, INF);
queue<int> q;
bool isinQ[MAX];
dist[source] = 0;
q.push(source);
isinQ[source] = true;
while(!q.empty()){
int now = q.front(); q.pop();
isinQ[now] = false;
for(int i = 0; i < adj[now].size(); i++){
int next = adj[now][i];
if(dist[now] + cost[now][next] < dist[next]
&& c[now][next] - f[now][next] > 0){
dist[next] = dist[now] + cost[now][next];
parent[next] = now;
if(!isinQ[next]){
q.push(next);
isinQ[next] = true;
}
}
}
}
if(parent[sink] == -1) break;
int temp = INF;
for(int i = sink; i != source; i = parent[i]){
temp = min(temp, c[parent[i]][i] - f[parent[i]][i]);
}
for(int i = sink; i != source; i = parent[i]){
result += temp * cost[parent[i]][i];
f[parent[i]][i] += temp;
f[i][parent[i]] -= temp;
}
cnt++;
}
if(cnt < n) return -1;
else return result;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> io[i];
adj[SOURCE].push_back(i);
adj[i].push_back(SOURCE);
c[SOURCE][i] = 1;
}
for(int i = 51; i <= n + 50; i++){
cin >> io[i];
adj[i].push_back(SINK);
adj[SINK].push_back(i);
c[i][SINK] = 1;
}
cin >> T >> F;
for(int i = 1; i <= n; i++){
for(int j = 51; j <= n + 50; j++){
if(io[j] > io[i]){
// 도착 시간 > 출발 시간
adj[i].push_back(j);
adj[j].push_back(i);
c[i][j] = 1;
}
if(io[j] - io[i] < T){
// 벌금 책정 방법
int S = io[j] - io[i];
int temp = (T - S) * (T - S);
temp = min(temp, F);
cost[i][j] = temp;
cost[j][i] = -temp;
}
}
}
int minAns = MCMF(SOURCE, SINK);
cout << minAns << ' ';
if(minAns < 0) return 0;
// 최대 비용
//init
for(int i = 0; i < MAX; i++){
for(int j = 0; j < MAX; j++){
f[i][j] = 0;
}
}
for(int i = 1; i <= n; i++){
for(int j = 51; j <= n + 50; j++){
cost[i][j] = -cost[i][j];
cost[j][i] = -cost[j][i];
}
}
int maxAns = -MCMF(SOURCE, SINK);
cout << maxAns;
return 0;
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 13699번: 점화식 (C++) (0) | 2022.05.17 |
---|---|
백준 9625번: BABBA (C++) (0) | 2022.05.17 |
백준 14950번: 정복자 (C++) (0) | 2022.05.08 |
백준 12745번: Traffic (Small) (C++) (0) | 2022.05.08 |
백준 17222번: 위스키 거래 (C++) (0) | 2022.05.08 |