Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions 13_최단경로/1238.cpp
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;
Copy link

@Dong-droid Dong-droid Nov 29, 2023

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을 사용해서 짧게 사용하면 어떨까요?

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;

Choose a reason for hiding this comment

The 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;
}
65 changes: 65 additions & 0 deletions 13_최단경로/15685.cpp
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;
}
}
2 changes: 2 additions & 0 deletions 13_최단경로/1865.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//13_최단경로 도전 2번:1865.cpp

58 changes: 58 additions & 0 deletions 13_최단경로/2458.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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3. 여기도요! 잘 푸셨네요~

cout << ans << "\n";

return 0;
}
1 change: 1 addition & 0 deletions 13_최단경로/합승택시요금.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//13_최단경로 도전 1번:합승택시요금.cpp