Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 | 29 |
30 | 31 |
Tags
- ์์ ์ ๋ ฌ
- OS
- Spring
- c++
- GCP
- ์๋ฎฌ๋ ์ด์
- ๋งต
- ๋ฐ์ดํฌ์คํธ๋ผ
- JPA
- ๋์ ํฉ
- ๊ทธ๋ฆฌ๋
- ์ต๋จ ๊ฒฝ๋ก
- ์ฐ์ ์์ ํ
- ์ฌ๊ท
- error
- DP
- thymeleaf
- dfs
- CVE
- BFS
- ๋ถํ ์ ๋ณต
- Reversing
- web
- dynamic debugging
- java
- ๊ตฌํ
- ์คํ
- ๋ฌธ์์ด
- ์ด๋ถ ํ์
- ๋ฐฑํธ๋ํน
Archives
- Today
- Total
hades
[Baekjoon] 1238๋ฒ: ํํฐ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1238
๐ ์ค๊ณ
๊ฐ ๋ง์์์ x๊น์ง์ ์ต๋จ ์๊ฐ์ x์์ ๊ฐ ๋ง์๊น์ง์ ์ต๋จ ์๊ฐ์ ๋ํ๋ฉด ๋๋ค. ํ๋ก์ด๋ ์์ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด์ ๋ชจ๋ ๋ง์์์ ๋ชจ๋ ๋ง์๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ณ , x๊น์ง ๊ฐ๋ค๊ฐ ๋์์ค๋ ๊ฑฐ๋ฆฌ์ ์ต๋จ ์๊ฐ์ ๊ฐฑ์ ํ์๋ค. ์๊ฐ๋ณต์ก๋๋ O(V^3)์ด๋ค.
์๊ฐ ๋ณต์ก๋๋ฅผ ์ค์ด๊ธฐ ์ํด O(ElogV)์ธ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ ์ ์๋ค. ์ต์ข ์ ์ผ๋ก ์๊ฐ๋ณต์ก๋๋ O(VElogV)์ด๋ค.
๐ ํ์ด1
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
vector<vector<int>> dist(1001, vector<int>(1001, 1e9+7));
int n, m, x, start, endd, cost, max_cost = 0;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> x;
for (int i = 0; i < m; i++) {
cin >> start >> endd >> cost;
dist[start][endd] = cost;
}
for (int i = 1; i <= n; i++) {
dist[i][i] = 0;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
for (int i = 1; i <= n; i++) {
max_cost = max(max_cost, dist[i][x] + dist[x][i]);
}
cout << max_cost << "\n";
return 0;
}
๐ ํ์ด2
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
vector<vector<pair<int, int>>> adj_list(1001);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> to_x(1001, 1e9 + 7);
vector<int> from_x(1001, 1e9 + 7);
int n, m, x, start, endd, cost, max_cost = 0;
void dijkstra_to_x(int start_x) {
vector<int> temp_dist(1001, 1e9 + 7);
temp_dist[start_x] = 0;
pq.push({ 0, start_x });
while (!pq.empty()) {
int cur_x = pq.top().second;
int cur_cost = pq.top().first;
pq.pop();
for (int i = 0; i < adj_list[cur_x].size(); i++) {
int next_x = adj_list[cur_x][i].second;
int next_cost = cur_cost + adj_list[cur_x][i].first;
if (next_x == x) {
to_x[start_x] = min(to_x[start_x], next_cost);
}
else {
if (temp_dist[next_x] > next_cost) {
temp_dist[next_x] = next_cost;
pq.push({ next_cost, next_x });
}
}
}
}
}
void dijkstra_from_x(int start_x) {
from_x[start_x] = 0;
pq.push({ 0, start_x });
while (!pq.empty()) {
int cur_x = pq.top().second;
int cur_cost = pq.top().first;
pq.pop();
for (int i = 0; i < adj_list[cur_x].size(); i++) {
int next_x = adj_list[cur_x][i].second;
int next_cost = cur_cost + adj_list[cur_x][i].first;
if (from_x[next_x] > next_cost) {
from_x[next_x] = next_cost;
pq.push({ next_cost, next_x });
}
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> x;
for (int i = 0; i < m; i++) {
cin >> start >> endd >> cost;
adj_list[start].push_back({ cost, endd });
}
for (int i = 1; i <= n; i++) {
dijkstra_to_x(i);
}
dijkstra_from_x(x);
for (int i = 1; i <= n; i++) {
max_cost = max(max_cost, to_x[i] + from_x[i]);
}
cout << max_cost << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1181๋ฒ: ๋จ์ด ์ ๋ ฌ (0) | 2024.08.04 |
---|---|
[Baekjoon] 1504๋ฒ: ํน์ ํ ์ต๋จ ๊ฒฝ๋ก (0) | 2024.08.03 |
[Baekjoon] 1629๋ฒ: ๊ณฑ์ (0) | 2024.08.03 |
[Baekjoon] 1167๋ฒ: ํธ๋ฆฌ์ ์ง๋ฆ (0) | 2024.08.02 |
[Baekjoon] 1149๋ฒ: RGB๊ฑฐ๋ฆฌ (0) | 2024.08.02 |