-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63fe5cf
commit 1af9057
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |