-
Notifications
You must be signed in to change notification settings - Fork 0
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
[최단경로] 11월 29일 #15
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "13_\uCD5C\uB2E8\uACBD\uB85C"
[최단경로] 11월 29일 #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//13_최단경로 필수 2번:1238.cpp | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <queue> | ||
#include <climits> | ||
|
||
using namespace std; | ||
|
||
const int INF = INT_MAX; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(nullptr); | ||
|
||
int v, e, x; | ||
cin >> v >> e >> x; | ||
|
||
vector<vector<pair<int, int>>> graph(v + 1); | ||
|
||
for (int i = 0; i < e; ++i) | ||
{ | ||
int a, b, cost; | ||
cin >> a >> b >> cost; | ||
graph[a].emplace_back(b, cost); | ||
} | ||
|
||
auto dijkstra = [&](int start) | ||
{ | ||
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; | ||
vector<int> distance(v + 1, INF); | ||
|
||
pq.push({0, start}); | ||
distance[start] = 0; | ||
|
||
while (!pq.empty()) | ||
{ | ||
int dist = pq.top().first; | ||
int now = pq.top().second; | ||
pq.pop(); | ||
|
||
if (distance[now] < dist) continue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 최적화에 중요합니다. 잘하셨네요! |
||
|
||
for (const auto& edge : graph[now]) | ||
{ | ||
int node_index = edge.first; | ||
int node_cost = edge.second; | ||
int cost = dist + node_cost; | ||
|
||
if (distance[node_index] > cost) | ||
{ | ||
distance[node_index] = cost; | ||
pq.push({cost, node_index}); | ||
} | ||
} | ||
} | ||
|
||
return distance; | ||
}; | ||
|
||
int result = 0; | ||
|
||
for (int i = 1; i <= v; ++i) | ||
{ | ||
vector<int> go = dijkstra(i); | ||
vector<int> back = dijkstra(x); | ||
result = max(result, go[x] + back[i]); | ||
} | ||
|
||
cout << result << "\n"; | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//13_최단경로 필수 1번:15685.cpp | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
bool map[101][101] = {false}; | ||
int dx[] = {1, 0, -1, 0}; | ||
int dy[] = {0, -1, 0, 1}; | ||
int ans = 0; | ||
|
||
void dragonCurve(int x, int y, int d, int g); | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
|
||
int n; | ||
cin >> n; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
int x, y, d, g; | ||
cin >> x >> y >> d >> g; | ||
dragonCurve(x, y, d, g); | ||
} | ||
|
||
for (int i = 0; i < 100; i++) | ||
{ | ||
for (int j = 0; j < 100; j++) | ||
{ | ||
if (map[i][j] && map[i][j + 1] && map[i + 1][j] && map[i + 1][j + 1]) | ||
{ | ||
ans++; | ||
} | ||
} | ||
} | ||
|
||
cout << ans << "\n"; | ||
|
||
return 0; | ||
} | ||
|
||
void dragonCurve(int x, int y, int d, int g) | ||
{ | ||
vector<int> d_list; | ||
d_list.push_back(d); | ||
|
||
for (int i = 1; i <= g; i++) | ||
{ | ||
for (int j = d_list.size() - 1; j >= 0; j--) { | ||
d_list.push_back((d_list[j] + 1) % 4); | ||
} | ||
} | ||
|
||
map[y][x] = true; | ||
for (int direction : d_list) | ||
{ | ||
x += dx[direction]; | ||
y += dy[direction]; | ||
map[y][x] = true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
//13_최단경로 도전 2번:1865.cpp | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//13_필수3: 2458.cpp | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
const int INF = 999999999; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(nullptr); | ||
|
||
int n, m; | ||
cin >> n >> m; | ||
|
||
vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF)); | ||
|
||
for (int i = 1; i <= m; ++i) | ||
{ | ||
int a, b; | ||
cin >> a >> b; | ||
dist[a][b] = 1; | ||
} | ||
|
||
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]); | ||
} | ||
} | ||
} | ||
Comment on lines
+28
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3. 모듈화를 하면 좋을 것 같습니다. |
||
|
||
int ans = 0; | ||
|
||
for (int i = 1; i <= n; ++i) | ||
{ | ||
int cnt = 0; | ||
|
||
for (int j = 1; j <= n; ++j) | ||
{ | ||
if (dist[i][j] != INF || dist[j][i] != INF) { | ||
cnt++; | ||
} | ||
} | ||
|
||
if (cnt == n - 1) ans++; | ||
} | ||
|
||
Comment on lines
+40
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3. 여기도요! 잘 푸셨네요~ |
||
cout << ans << "\n"; | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//13_최단경로 도전 1번:합승택시요금.cpp |
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.
P3. pair<int,int>를 typedef을 사용해서 짧게 사용하면 어떨까요?