Skip to content

Commit

Permalink
22.07.28
Browse files Browse the repository at this point in the history
  • Loading branch information
delena0702 authored Jul 28, 2022
1 parent 63fe5cf commit 1af9057
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
60 changes: 60 additions & 0 deletions BAEKJOON/3640_제독.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from collections import defaultdict, deque
import sys
def input(): return sys.stdin.readline().strip()

while True:
try:
V, E = map(int, input().split())
except:
break

edges = [defaultdict(lambda: (-1, -1)) for _ in range(2 * V)]
for i in range(V):
edges[2 * i][2 * i + 1] = (1, 0)
edges[2 * i + 1][2 * i] = (0, 0)

for _ in range(E):
a, b, c = map(int, input().split())
edges[2 * a - 1][2 * b - 2] = (1, c)
edges[2 * b - 2][2 * a - 1] = (0, -c)
flow = [defaultdict(int) for _ in range(2 * V)]

answer = 0
for _ in range(2):
dist = [float('inf')] * (2 * V)
inqueue = [False] * (2 * V)
pre = [-1] * (2 * V)
dist[1], inqueue[1] = 0, True
queue = deque([1])
while queue:
here = queue.popleft()
inqueue[here] = False

for there in edges[here].keys():
capacity, cost = edges[here][there]

if capacity - flow[here][there] <= 0:
continue

if dist[here] + cost >= dist[there]:
continue

dist[there] = dist[here] + cost
pre[there] = here

if not inqueue[there]:
queue.append(there)
inqueue[there] = True

if pre[2 * V - 2] == -1:
break

there = 2 * V - 2
while there != 1:
here = pre[there]
flow[here][there] = flow[here][there] + 1
flow[there][here] = flow[there][here] - 1
answer = answer + edges[here][there][1]
there = here

print(answer)
27 changes: 27 additions & 0 deletions BAEKJOON/9942_하노이의_네_탑.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>

#define SIZE 1000

using namespace std;

unsigned long long dp[SIZE + 1];

int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

dp[1] = 1;
for (int i = 2; i <= 1000; i++)
{
dp[i] = ULLONG_MAX;
for (int j = 1; j < min(i, 64); j++)
dp[i] = min(dp[i], 2 * dp[i - j] + ((1ULL << j) - 1));
}

int N = 0;
for (int t = 1; cin >> N; t++)
cout << "Case " << t << ": " << dp[N] << endl;

return 0;
}

0 comments on commit 1af9057

Please sign in to comment.