-
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
Showing
2 changed files
with
64 additions
and
37 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,33 @@ | ||
from sys import stdin | ||
from heapq import * | ||
|
||
input = stdin.readline | ||
maxsize = 10**6 | ||
n,m,r=map(int,input().split()) | ||
d = [[maxsize]*n for _ in range(n)] | ||
t = list(map(int,input().split())) | ||
|
||
for i in range(r): | ||
a,b,c=map(int,input().split()) | ||
d[a-1][b-1] = c | ||
d[b-1][a-1] = c | ||
|
||
for k in range(n): | ||
for i in range(n): | ||
for j in range(n): | ||
d[i][j] = min(d[i][k]+d[k][j],d[i][j]) | ||
|
||
|
||
def go(start): | ||
res = t[start] | ||
for i in range(n): | ||
if i==start: | ||
continue | ||
if m>=d[start][i]: | ||
res += t[i] | ||
return res | ||
|
||
res = 0 | ||
for i in range(n): | ||
res = max(go(i),res) | ||
print(res) |
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 |
---|---|---|
@@ -1,55 +1,49 @@ | ||
--- | ||
layout: post | ||
title: boj 1480 해설 | ||
title: boj 14938 해설 | ||
categories: [Baekjoon] | ||
tags: [백준 1480, boj 1480] | ||
tags: [백준 14938, boj 14938] | ||
--- | ||
|
||
백준 1480번 | ||
백준 14938번 | ||
====== | ||
|
||
dp, Bitmasking | ||
floid warshall | ||
진짜 문제 안풀린다. 와샬 알고리즘 쓸 때 k i j 순서 주의하자... | ||
----- | ||
|
||
호호호! | ||
``` | ||
세준이는 잘 모르겠지만, 세준이는 보석에 미쳐있다. | ||
따라서, 숌 보석상에 있는 모든 보석을 다훔치려고 한다. | ||
하지만, 세준이는 보석을 다 가져올 수는 없다. 그 이유는 가방의 개수에 제한이 있고, | ||
한 가방마다 넣을 수 있는 보석의 개수가 제한이 있기 때문이다. 세준이는 M개의 가방을 가지고 있다. | ||
그리고 각각의 가방은 C그램의 보석을 담을 수 있다. | ||
``` | ||
위 설명처럼 핵심은 아래와 같다. | ||
*현 위치에서 보석을 담을 경우 | ||
*이럴 경우 dfs(bag,cap-a[i],path|(1<<i))로 현재 가방에서 용량을 a[i]만큼 용량을 줄이고 다음을 탐색하는 경우이다. | ||
*현 위치에서 보석을 안담고 다음 가방 탐색 | ||
*이럴 경우는 dfs(bag+1,c,path|(1<<i))로 다음 가방에 담는 것이다. | ||
|
||
```python | ||
from sys import stdin, setrecursionlimit | ||
from sys import stdin | ||
from heapq import * | ||
|
||
setrecursionlimit(10**6) | ||
input = stdin.readline | ||
maxsize = 10**6 | ||
n,m,r=map(int,input().split()) | ||
d = [[maxsize]*n for _ in range(n)] | ||
t = list(map(int,input().split())) | ||
|
||
for i in range(r): | ||
a,b,c=map(int,input().split()) | ||
d[a-1][b-1] = c | ||
d[b-1][a-1] = c | ||
|
||
n,m,c=map(int,input().split()) | ||
a=list(map(int,input().split())) | ||
d=[[[-1]*(1<<n) for _ in range(c+1)] for _ in range(m+1)] | ||
for k in range(n): | ||
for i in range(n): | ||
for j in range(n): | ||
d[i][j] = min(d[i][k]+d[k][j],d[i][j]) | ||
|
||
all_path=(1<<n)-1 | ||
|
||
def dfs(b,cap,path): | ||
if path==all_path or b==m: | ||
return 0 | ||
if d[b][cap][path]!=-1: | ||
return d[b][cap][path] | ||
d[b][cap][path] = 0 | ||
def go(start): | ||
res = t[start] | ||
for i in range(n): | ||
if (1<<i)&path or cap<a[i]: | ||
if i==start: | ||
continue | ||
if cap-a[i]>0: | ||
d[b][cap][path]=max(d[b][cap][path],dfs(b,cap-a[i],path|(1<<i))+1) | ||
d[b][cap][path]=max(d[b][cap][path],dfs(b+1,c,path|(1<<i))+1) | ||
return d[b][cap][path] | ||
|
||
print(dfs(0,c,0)) | ||
if m>=d[start][i]: | ||
res += t[i] | ||
return res | ||
|
||
res = 0 | ||
for i in range(n): | ||
res = max(go(i),res) | ||
print(res) | ||
``` |