Skip to content

Commit

Permalink
2024-03-19 플로이드
Browse files Browse the repository at this point in the history
  • Loading branch information
InSange committed Mar 18, 2024
1 parent f50918e commit ed29d7c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
| 차시 | 날짜 | 문제유형 | 링크 | 풀이 |
|:----:|:---------:|:----:|:-----:|:----:|
| 1차시 | 2024.03.11 | BFS | [숨바꼭질 4](https://www.acmicpc.net/problem/13913) | [#1](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/3) |
| 2차시 | 2024.03.15 | DP | [](https://www.acmicpc.net/problem/7579) | - |
| 2차시 | 2024.03.15 | DP | [](https://www.acmicpc.net/problem/7579) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/6) |
| 3차시 | 2024.03.19 | 플로이드 워셜 | [](https://www.acmicpc.net/problem/7579) | [#2](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/6) |
=======
---
63 changes: 63 additions & 0 deletions InSange/플로이드 워셜/11404.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <iostream>
#include <vector>

using namespace std;

const int INF = 99999999;
int n, m, a, b, c;
vector<vector<int>> cities;

void Solve()
{
cin >> n >> m;
n++;
cities.assign(n, vector<int>(n, INF));
// cities[i][i] -> (0, 0), (1, 1) ... (n, n) 부분들은 다 0으로 초기화
// 자기 자신에서 자기 자신으로 방문 비용은 0
for (int i = 1; i < n; i++)
{
cities[i][i] = 0;
}

for (int i = 0; i < m; i++)
{
cin >> a >> b >> c;

if (cities[a][b] < c) continue;
cities[a][b] = c;
}
// 3중 for문은 밖에 있는 층은 고정된 값으로 변화를 찾는다.
// 즉 i, j가 k에 있는 값들을 모두 방문할 때 까지 fix된다.
// 만약 k가 두번째 혹은 첫번째(안쪽)에 있는 for문에서 돌게되면
// i, j 가 위치한 값은 k가 배열 안의 모든 경우의 수를 돌아보기 때문에
// 만약 돌아보는 곳에 하나라도 INF값이 있게되면 작은 값으로 최신화가 안된다.
for (int k = 1; k < n; k++)
{
for (int i = 1; i < n; i++)
{
for (int j = 1; j < n; j++)
{
cities[i][j] = min(cities[i][k] + cities[k][j], cities[i][j]);
}
}
}

for (int i = 1; i < n; i++)
{
for (int j = 1; j < n; j++)
{
cout << (cities[i][j] == INF ? 0 : cities[i][j]) << " ";
}
cout << "\n";
}
}

int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);

Solve();

return 0;
}

0 comments on commit ed29d7c

Please sign in to comment.