-
Notifications
You must be signed in to change notification settings - Fork 1
30-kokeunho #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
30-kokeunho #120
Conversation
wnsmir
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ค๋๋ง์ ๋ ธ๋ ๊ฐ์ ์ ํ์ bfs๋ฅผ๋ง๋ ๋ฐ๊ฐ์ ์ต๋๋ค!
๋จผ์ ๋ฌธ์ ๋ฅผ๋ณด๊ณ ์ต๋๊ฐ์ ์ฐพ์ผ๋ผํด์ dfs๋ก ๋ชจ๋ ๊ฒฝ์ฐ์์๋ฅผ ๋ค ์ฒดํฌํด๋ณด๊ณ ์ต๋๊ฐ์ ์ฐพ์๋ณด๋ ค ํ์ง๋ง ์ต๋๊ฐ์ด 10์ต์ธ๊ฑธ๋ณด๊ณ ์ด๋ถํ์์ ์จ์ผํ๋? ๋ผ๋ ์๊ฐ์ ํ์ต๋๋ค.
๋ค๋ง ์ด๋ฅผ ์ด๋ถํ์์ผ๋ก ์ต๋๊ฐ์ ์ ํ๊ณ , ์ง์ bfs๋ก ๋์ ํด๋ณด๋ ๊ตฌ์กฐ๋ก ํ ์๊ฐ์ ํ๊ธฐ๊น์ง๋ ์๊ฐ์ด ์ข ๊ฑธ๋ ธ๋๊ฒ ๊ฐ์ต๋๋ค.
๋ฌธ์ ๊ฐ ์ด๋์ ๋ ์ฌ์ ๋ ์ด์ ๋ ์์์ง์ ๊ณผ ๋์ง์ ์ ์ด๋ฏธ ์ ํด์ฃผ์๊ธฐ๋๋ฌธ์ bfs๋ฅผ ๋๋ฆด๋๋ ์ฌ์ค ๊ฐ์ค์น๋ง ๋ฃ์ด์ฃผ๋ฉด ๋์๊ธฐ ๋๋ฌธ์ ๋๋ค.
์ฌ์ค ์ด๋ถ๋ถ์ด ์ฌ์ฐ๋ฉด์๋ ์๊ฐ์ ๋ง์ด ์ก์๋จน์๋๋ฐ, bfs๋๋ณดํต ๊ฐ์ ์ ๊ฐ์ค์น๊ฐ ์์๋ ์ฌ์ฉํ์ง๋ง, ์ด๋ฌธ์ ๋ visited์กฐ๊ฑด ์ด์ธ์๋ ๊ฐ์ค์น๋ฅผ ๋ฒํธ์ ์๋์ง์ ์กฐ๊ฑด์ ๋ํด bfs๊ฒฝ๋ก๋ฅผ ์ค๊ฐ์ ์ฐจ๋จํ ์ ์์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
์ ๋ฐํ ๋ฌธ์ ์๋ณด๊ณ ๊ฐ๋๋ค!
import sys
from collections import deque
N, M = map(int, input().split())
edges = [[] for _ in range(N+1)]
for _ in range(M):
a, b, c = map(int, input().split())
edges[a].append((b, c))
edges[b].append((a, c))
start, end = map(int, input().split())
def bfs(weight):
visited = [False] * (N+1)
queue = deque()
queue.append(start)
visited[start] = True
while queue:
now = queue.popleft()
if now == end:
return True
for next, limit in edges[now]:
if not visited[next] and limit >= weight:
visited[next] = True
queue.append(next)
return False
left = 1
right = 1_000_000_000
answer = 0
while left <= right:
mid = (left + right) // 2
# ๊ฐ๋ฅํ ๋
if bfs(mid):
# ์ค๋ฅธ์ชฝ ๊ตฌ๊ฐ์์ ๋ค์ ์ด๋ถํ์
answer = mid
left = mid + 1
# ๋ถ๊ฐ๋ฅํ๋ค๋ฉด
else:
#์ผ์ชฝ๊ตฌ๊ฐ์์ ๋ค์ ์ด๋ถํ์
right = mid - 1
print(answer)์ ์ฐธ๊ณ ๋ก ํ์ด์ ์ ์ ์ ์ค๊ฐ์ ์ธ๋๋ฐ๋ฅผ ์ฌ์ฉํ์ฌ๋ ์ ํ ๋ฌธ์ ๋์ง ์์ต๋๋ค!
1_000_000_000 == 1000000000
kangrae-jo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฒ์์ ๋ฌด์ง์ฑ์ผ๋ก ๊ทธ๋ฅ ๋ค์ต์คํธ๋ผ ๋๋ ธ๋ค๊ฐ ํ๋ ธ๋ค์.
์ ๋ ๋๋
์ฆ์ด ์๋๋ด
๋๋ค.
๋ฌธ์ ์ดํด๊ฐ ํ ๋ฒ์ ์๋๋ค์..
๊ทธ์น๋ง ์ด๋ ค์ด ๋ฌธ์ ๋ ์๋์๋ค๊ณ ์๊ฐํฉ๋๋ค.
๋ง์ํ์ ๋๋ก ์ด๋ถํ์์ผ๋ก ์ต์ ํด๋ฅผ ์ฐพ๋ ๋ฌธ์ ์ธ๋ฐ,
์๋ง ์ด์ ์ ์ ํฌ๊ฐ ์ข ํ๋ ํ๋ผ๋ฉํธ๋ฆญ ์์น๋ผ๊ณ ๋ถ๋ฅผ ์ ์์ ๊ฒ ๊ฐ์์.
๋ญ๊ฐ ํ ๋ฒ์ bfs ๋ง์ 's์์ e ๊น์ง ๊ฐ์ค์น ์ค์ ๊ฐ์ฅ ์์ ๋
์'์ ์ฐพ๋ ๋ฐฉ๋ฒ์ ์๊ฐํด๋ณด๋ ค๊ณ ํ๋๋ฐ..
ํ๋ ธ๋ค์.. ๋ฐ๊ฒฌํ ๊ฐ์ฅ ์์ ๊ฐ์ค์น๋ฅผ ์ง๋์ง ์๊ณ ๋ ๋์์ ๋์ฐฉํ ์ ์๋ ๊ฒฝ์ฐ์ ์ ๋ ๊ณ ๋ คํด์ผํ๊ธฐ ๋๋ฌธ์ ์ด๋ ค์ด ๋ฐฉ์๊ฐ๋ค์. ์ฌํ๋๋ค.
์ข์ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค.
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
bool solution(const vector<vector<pair<int, int>>>& graph, int m, int s, int e) {
vector<bool> visited(graph.size(), false);
queue<int> q;
q.push(s);
visited[s] = true;
while (!q.empty()) {
int cur = q.front();
q.pop();
if (cur == e) return true;
for (auto [next, dist] : graph[cur]) {
if (!visited[next] && dist >= m) {
visited[next] = true;
q.push(next);
}
}
}
return false;
}
int main() {
int N, M;
cin >> N >> M;
vector<vector<pair<int, int>>> graph(N + 1);
while (M--) {
int A, B, C;
cin >> A >> B >> C;
graph[A].push_back({B, C});
graph[B].push_back({A, C});
}
int s, e;
cin >> s >> e;
long long start = 1, end = 1000000000;
while (start <= end) {
long long mid = (start + end) / 2;
if (solution(graph, mid, s, e)) start = mid + 1;
else end = mid - 1;
}
cout << end;
return 0;
}
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BFS/Dijkstra๋ผ๋ ์๊ฐ์ด ์ฒ์ ๋ฑ ๋ค์ด์ ์ฐ๋นํํ ์งฐ๋๋ฐ, ์ด๊ฒ ์ค์๊ฐ์ผ๋ก ์ต๋ ์ค๋์ ์ ๋ฐ์ดํธ ํ๋ ๋ฐฉ๋ฒ์ด ์๋ ์ค๋ฅด๋๋ผ๊ตฌ์. ๊ทธ๋์ ๊ณจ๋จธ๋ฆฌ ์ธ๋งค๋ค๊ฐ, ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ ์ต๋ ์ค๋์ด 10์ต์ผ๋ก ํฐ ๊ฑฐ ๋ณด๊ณ "๋ญ๊ฐ ์ด์งํ์ ๊น์ด ๋ณด์ด๋๋ฐ"๋ผ๋ ์๊ฐ์ด ๋ค์ด์ ์์ ์ด์งํ์์ผ๋ก ๊ธฐ์ค ์ค๋์ ์ก๊ณ , ๊ทธ ์ค๋๊ฐ์ผ๋ก ๋ชฉ์ ์ง์ ๋๋ฌํ ์ ์๋ ์ง์ ๋ฐ๋ผ ๊ธฐ์ค ์ค๋๊ฐ์ ์กฐ์ ํ๋ฉฐ ์ต๋๊ฐ์ ๊ฐฑ์ ํ๋ ์์ผ๋ก ํ๋๊น ๋ง๋๋ผ๊ตฌ์. ์ฝ๊ฐ ๋ฐ์์ ์ ํ(?)
์ด์งํ์
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
int maxWeight = 0;
vector<vector<pair<int, int>>> graph(N + 1);
while(M--) {
int A, B, C;
cin >> A >> B >> C;
graph[A].emplace_back(B, C);
graph[B].emplace_back(A, C);
maxWeight = max(maxWeight, C);
}
int src, dst;
cin >> src >> dst;
auto Check = [&](const int stdLimit) {
queue<int> q;
q.emplace(src);
vector<bool> visited(N + 1, false);
visited[src] = true;
while(!q.empty()) {
const int u = q.front();
q.pop();
if(u == dst) {
return true;
}
for(const auto& [v, limit] : graph[u]) {
if(!visited[v] && limit >= stdLimit) {
visited[v] = true;
q.emplace(v);
}
}
}
return false;
};
int ans = 0;
int lo = 1, hi = maxWeight;
while(lo <= hi) {
int mid = (lo + hi) / 2;
if(Check(mid)) {
ans = max(ans, mid);
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << ans;
return 0;
}๊ทธ๋ฐ๋ฐ ๋ญ๊ฐ ์ค์๊ฐ์ผ๋ก ์ต๋ ์ค๋์ ์ ๋ฐ์ดํธ ํ ์ ์์ ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ์๊พธ ๋ค์ด์ ๋ค๋ฅธ ์ฌ๋ ํ์ด๋ฅผ ๋ดค๋๋ฐ, ์ต์ ์คํจ๋ ํธ๋ฆฌ ๊ธฐ๋ฒ์ ์ฌ์ฉํ๋ฉด ์ด๊ฒ ๊ฐ๋ฅํด์ ์ด๊ฒ๋ ๋ค๊ณ ์์ต๋๋ค ใ
์คํจ๋ ํธ๋ฆฌ๋ฅผ ๊ตฌ์ถํ ๋, ๋ณดํต์ ๊ฐ์ ์ ๊ฐ์ค์น๊ฐ ์์ ์์ผ๋ก ์ ๋ ฌํ์ง๋ง ์ฌ๊ธฐ์๋ ๊ฑฐ๊พธ๋ก ๊ฐ์ค์น๊ฐ ํฐ ๊ฒ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํฉ๋๋ค. ์ฐ๋ฆฌ์ ๋ชฉ์ ์ด ๊ฐ์ค์น๊ฐ ๊ฐ์ฅ ํฐ ๊ฐ์ ์ฐพ๋ ๊ฑฐ๋๊น์.
๊ทธ๋ฆฌ๊ณ ๊ฐ์ ๋ค์ ๋ํด Union์ ์ํํฉ๋๋ค. ๊ฐ์ค์น๊ฐ ํฐ ๊ฐ์ ๋ค ๋จผ์ Union๋๊ฒ ์ฃ ? ๊ทธ๋ฌ๋ฉด ์ด๋ ์๊ฐ ์ถ๋ฐ ๊ณต์ฅ ๋ ธ๋์ ๋์ฐฉ ๊ณต์ฅ ๋ ธ๋๊ฐ ์ฐ๊ฒฐ๋๋(๋ ๋ ธ๋์ ๋ถ๋ชจ๊ฐ ๊ฐ์) ์๊ฐ์ด ์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ๊ทธ ๋ ํ์ธํ๋ ๊ฐ์ ์ ๊ฐ์ค์น๋ฅผ ์ถ๋ ฅํ๋ฉด ๋ฉ๋๋ค. ๋ ๊ณต์ฅ์ด ์ฐ๊ฒฐ๋๋ ์๊ฐ์ ๊ฒฝ๋ก๊ฐ ๊ฐ์ฅ ํฐ ํ์ค ์ ํ์ ๊ฐ๋ ๊ฒฝ๋ก์ด๊ธฐ ๋๋ฌธ์ด์ฃ . ๊ฐ์ ์ ๊ฐ์ค์น๊ฐ ํฐ ์์๋๋ก ์ฐ๊ฒฐ์ ์๋ํ๊ธฐ ๋๋ฌธ์ด์ฃ .
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
using Edge = tuple<int, int, int>;
vector<Edge> edges(M);
for(auto& [u, v, weight] : edges) {
cin >> u >> v >> weight;
}
int src, dst;
cin >> src >> dst;
vector<int> p(N + 1);
iota(p.begin(), p.end(), 0);
sort(edges.begin(), edges.end(), [](const Edge& lhs, const Edge& rhs) {
return get<2>(lhs) > get<2>(rhs);
});
auto Find = [&](int x) {
while(x != p[x]) {
p[x] = p[p[x]];
x = p[x];
}
return p[x];
};
auto Union = [&](int u, int v) {
u = Find(u);
v = Find(v);
p[u] = v;
};
for(const auto& [u, v, weight] : edges) {
Union(u, v);
if(Find(src) == Find(dst)) {
cout << weight;
break;
}
}
return 0;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฌธ์ ๋ฅผ ์ฝ๊ณ ์ฒจ์ ๊ทธ๋ฆฌ๋ํ๊ฒ ๊ฐ์ฅ ํฐ ๋ ธ๋๋ฅผ ์ฐพ์ผ๋ฉด์ ๊ฐ๋ฉด ๋๊ฒ ๋ค! ํ๋๋ฐ ๋ฐ๋ก๋ฅผ ๋ฐ๋ก ์ฐพ์์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ๊ณ ๋ฏผํ์ต๋๋ค. ๊ทธ๋ฅ ๋ธ๋ฃจํธํฌ์ค๋ก ์ ๋ถ๋ค ํด์ผํ๋? ์ถ์๋๋ฐ n์ด 10000์ธ๋ฐ ๋ง์ด ์๋์ฃ ..
10๋ถ์ ๋ ๊ณ ๋ฏผํ๋ค๊ฐ ์ฐ์ฐ์ฐฎ๊ฒ ์ด๋ถ ํ์์ด๋ผ๋ ์๊ฐ์ด ๋ ์ฌ๋์๋๋ค. 1 ~ 10000000๊น์ง ์ด๋ถํ์์ผ๋ก ์ํํ๋ฉด์ ๊ทธ ๊ฐ์ ๊ฐ์ง๋ ๊ฒฝ๋ก๊ฐ ์กด์ฌํ๋์ง๋ฅผ ์ฐพ์ผ๋ฉด ๋๋ค ์๊ฐํ์ด์. ๊ทผ๋ฐ up & down ์กฐ๊ฑด์ ์ ๋ง์ถ์ง ๋ชปํด ๋ต์ง๋ฅผ ๋ดค์ต๋๋ค.
๊ฑฐ์ ๋ค ์๋๋ฐ ์์ฝ๋ค์ใ ์ ๋ ฅ ๊ด๋ จํด์ ํ์ด์ฌ ๋ฌธ๋ฒ์ ๋ชฐ๋ผ ์ค์ฉ๋ ์ฝ๋๋ฅผ ๋ณด๊ณ ์น๋ค๊ฐ ๊ทธ๋ฅ ๊ฐ์์ก์ด๋๋ค..
import sys
from collections import deque
input = sys.stdin.readline
def bfs(weight):
visited = [False for _ in range(n+1)]
q = deque()
q.append(s)
visited[s] = True
while q:
now = q.popleft()
if now == e:
return True
for next, edge in graph[now]:
if not visited[next] and edge >= weight:
visited[next] = True
q.append(next)
return False
n, m = map(int, input().split())
graph = [[] for _ in range(n+1)]
for _ in range(m):
a, b, w = map(int, input().split())
graph[a].append((b, w))
graph[b].append((a, w))
s, e = map(int, input().split())
left = 1
right = 1_000_000_000
answer = 0
while left <= right:
mid = (left + right) // 2
if bfs(mid):
answer = mid
left = mid + 1
else:
right = mid - 1
print(answer)
๐ ๋ฌธ์ ๋งํฌ
[BOJ] ์ค๋์ ํ https://www.acmicpc.net/problem/1939
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
์ด์ ์ค๋๋ง์ ๋ค์ต์คํธ๋ผ ๋ฌธ์ ๋ฅผ ํ๊ณ
๋น์ทํ ์ ํ์ผ๋ก ํ์ด๋ณด๋ ค๊ณ ๊ณ ๋ฅธ ๋ฌธ์ ์ธ๋ฐ
์ผ๋ฐ์ ์ธ ๋ค์ต์คํธ๋ผ๋ ๋ฌ๋ผ์ ๋ช๋ฒ ์คํจํ๊ณ
๊ฒฐ๊ตญ ํ์ด๋ฅผ ์ฐธ๊ณ ํด์ ์ด๋ถ ํ์ + bfs ํ์ด๋ก ํ์ด์ ๊ฐ์ ธ์์ต๋๋ค.
(๋ค์ต์คํธ๋ผ๋ก ๋ค์ ํ์ด์ ํด๊ฒฐํ์ต๋๋ค๋ง ์ด๋ฒ ํ์ด๊ฐ ๋ ์ข์๋ณด์ฌ์ ํด๋น ํ์ด๋ก ์์ฑํฉ๋๋ค.)
bfs ๋ฉ์๋๋ A์์ B๊น์ง mid ์ค๋์ ์ค์์ฑ๋ก ๊ฐ ์ ์๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ๊ฒ์ ๋๋ค.
์ด๋ mid ์ค๋์ ์ด๋ถ ํ์์ ํตํด ๊ฒฐ์ ํฉ๋๋ค.
๋ง์ ์๋ ์ฝ๋ ์ฐ๋ ค๋ ์ธ๊ฒ ์๋ค์. ์ด์์ ๋๋ท.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ