BOJ 11404. νλ‘μ΄λ
πββοΈ[Gold IV] νλ‘μ΄λ - 11404
μ±λ₯ μμ½
λ©λͺ¨λ¦¬: 2152 KB, μκ°: 24 ms
λΆλ₯
νλ‘μ΄λβμμ , κ·Έλν μ΄λ‘ , μ΅λ¨ κ²½λ‘
μ μΆ μΌμ
2024λ 1μ 17μΌ 13:34:02
λ¬Έμ μ€λͺ
n(2 β€ n β€ 100)κ°μ λμκ° μλ€. κ·Έλ¦¬κ³ ν λμμμ μΆλ°νμ¬ λ€λ₯Έ λμμ λμ°©νλ m(1 β€ m β€ 100,000)κ°μ λ²μ€κ° μλ€. κ° λ²μ€λ ν λ² μ¬μ©ν λ νμν λΉμ©μ΄ μλ€.
λͺ¨λ λμμ μ (A, B)μ λν΄μ λμ Aμμ Bλ‘ κ°λλ° νμν λΉμ©μ μ΅μκ°μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
μ λ ₯
첫째 μ€μ λμμ κ°μ nμ΄ μ£Όμ΄μ§κ³ λμ§Έ μ€μλ λ²μ€μ κ°μ mμ΄ μ£Όμ΄μ§λ€. κ·Έλ¦¬κ³ μ μ§Έ μ€λΆν° m+2μ€κΉμ§ λ€μκ³Ό κ°μ λ²μ€μ μ λ³΄κ° μ£Όμ΄μ§λ€. λ¨Όμ μ²μμλ κ·Έ λ²μ€μ μΆλ° λμμ λ²νΈκ° μ£Όμ΄μ§λ€. λ²μ€μ μ 보λ λ²μ€μ μμ λμ a, λμ°© λμ b, ν λ² νλλ° νμν λΉμ© cλ‘ μ΄λ£¨μ΄μ Έ μλ€. μμ λμμ λμ°© λμκ° κ°μ κ²½μ°λ μλ€. λΉμ©μ 100,000λ³΄λ€ μκ±°λ κ°μ μμ°μμ΄λ€.
μμ λμμ λμ°© λμλ₯Ό μ°κ²°νλ λ Έμ μ νλκ° μλ μ μλ€.
μΆλ ₯
nκ°μ μ€μ μΆλ ₯ν΄μΌ νλ€. iλ²μ§Έ μ€μ μΆλ ₯νλ jλ²μ§Έ μ«μλ λμ iμμ jλ‘ κ°λλ° νμν μ΅μ λΉμ©μ΄λ€. λ§μ½, iμμ jλ‘ κ° μ μλ κ²½μ°μλ κ·Έ μ리μ 0μ μΆλ ₯νλ€.
πνμ΄
νλ‘μ΄λ-μμ¬ μκ³ λ¦¬μ¦ λ¬Έμ μ΄λ€.
iμμ jλ‘ κ°λ₯ κ²½λ‘κ° μ¬λ¬κ°λ‘ λμ€κΈ° λλ¬Έμ μ΅λ¨ κ±°λ¦¬λ§ κΈ°λ‘ν΄μΌνλ€.
int n, m, INF = 123456789;
vector<vector<int> > dis;
void setting()
{
cin >> n >> m;
dis = vector<vector<int>>(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; ++i)
{
dis[i][i] = 0;
}
int a, b, c;
for (int i = 1; i <= m; ++i)
{
cin >> a >> b >> c;
if (dis[a][b] == INF)
{
dis[a][b] = c;
}
else if (dis[a][b] != INF && c < dis[a][b])
{
dis[a][b] = c;
}
}
}
μκΈ° μμ κΉμ§ κ°λ 거리λ 0μΌλ‘ μ΄κΈ°νν΄μΌνλ€.
νλ‘μ΄λ-μμ¬ μκ³ λ¦¬μ¦μ 3μ€ forλ¬Έμ μ΄μ©νλ€.
if (dis[i][j] > dis[i][k] + dis[k][j])
μ΄ λ§μ΄ λ€λ₯Έ κ²½λ‘λ₯Ό κ²½μ ν΄μ κ°λ κ²½μ°κ° λΉμ©μ΄ λ λλ κ²½μ°μ΄λ€.
void solve()
{
// νλ‘μ΄λ μμ¬ ν΅μ¬!
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
if (dis[i][j] > dis[i][k] + dis[k][j])
{
dis[i][j] = dis[i][k] + dis[k][j];
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
// κ°λ λ°©λ²μ΄ μλ κ²½μ°
if (dis[i][j] == INF)
cout << 0 << " ";
else
cout << dis[i][j] << " ";
}
cout << '\n';
}
}
iμμ jλ‘ κ°λ κΈ°λ³Έ μ°κ²° μ 보
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 10 |
INF | 0 | INF | 2 | INF |
8 | INF | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | INF | INF | 0 |
dis[3][2]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[3][2]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[3][2] = dis[3][1] + dis[1][2]
10 = 8 + 2
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 10 |
INF | 0 | INF | 2 | INF |
8 | 10 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | INF | INF | 0 |
dis[5][3]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[5][3]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[5][3] = dis[5][1] + dis[1][3]
10 = 7 + 3
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 10 |
INF | 0 | INF | 2 | INF |
8 | 10 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | 10 | INF | 0 |
dis[5][4]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[5][4]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[5][4] = dis[5][1] + dis[1][4]
8 = 7 + 1
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 10 |
INF | 0 | INF | 2 | INF |
8 | 10 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | 10 | 8 | 0 |
dis[5][4]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 8
dis[5][4]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[5][4] = dis[5][2] + dis[2][4]
6 = 4 + 2
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 10 |
INF | 0 | INF | 2 | INF |
8 | 10 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
dis[1][5]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 10
dis[1][5]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[1][5] = dis[1][3] + dis[3][5]
4 = 3 + 1
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 4 |
INF | 0 | INF | 2 | INF |
8 | 10 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
dis[2][5]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[2][5]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[2][5] = dis[2][4] + dis[4][5]
5 = 2 + 3
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 4 |
INF | 0 | INF | 2 | 5 |
8 | 10 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
dis[2][1]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[2][1]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[2][1] = dis[2][5] + dis[5][1]
12 = 5 + 7
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 4 |
12 | 0 | INF | 2 | 5 |
8 | 10 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
dis[2][3]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[2][3]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[2][3] = dis[2][5] + dis[5][3]
15 = 5 + 10
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 4 |
12 | 0 | 15 | 2 | 5 |
8 | 10 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
dis[3][2]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 10
dis[3][2]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[3][2] = dis[3][5] + dis[5][2]
5 = 1 + 4
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 4 |
12 | 0 | 15 | 2 | 5 |
8 | 5 | 0 | 1 | 1 |
INF | INF | INF | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
dis[4][1]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[4][1]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[4][1] = dis[4][5] + dis[5][1]
10 = 3 + 7
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 4 |
12 | 0 | 15 | 2 | 5 |
8 | 5 | 0 | 1 | 1 |
10 | INF | INF | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
dis[4][2]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[4][2]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[4][2] = dis[4][5] + dis[5][2]
7 = 3 + 4
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 4 |
12 | 0 | 15 | 2 | 5 |
8 | 5 | 0 | 1 | 1 |
10 | 7 | INF | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
dis[4][3]μμ μ
λ°μ΄νΈ λ°μ κΈ°μ‘΄μ κ° : 123456789
dis[4][3]μμ μ
λ°μ΄νΈ κ²°κ³Ό
dis[4][3] = dis[4][5] + dis[5][3]
13 = 3 + 10
1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|
0 | 2 | 3 | 1 | 4 |
12 | 0 | 15 | 2 | 5 |
8 | 5 | 0 | 1 | 1 |
10 | 7 | 13 | 0 | 3 |
7 | 4 | 10 | 6 | 0 |
κ²°κ³Ό
0 2 3 1 4
12 0 15 2 5
8 5 0 1 1
10 7 13 0 3
7 4 10 6 0
πμ 체 μ½λ
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, INF = 123456789;
vector<vector<int> > dis;
void setting()
{
cin >> n >> m;
dis = vector<vector<int>>(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; ++i)
{
dis[i][i] = 0;
}
int a, b, c;
for (int i = 1; i <= m; ++i)
{
cin >> a >> b >> c;
if (dis[a][b] == INF)
{
dis[a][b] = c;
}
else if (dis[a][b] != INF && c < dis[a][b])
{
dis[a][b] = c;
}
}
//for (int i = 1; i <= n; ++i)
//{
// for (int j = 1; j <= n; ++j)
// {
// cout << dis[i][j] << " ";
// }
// cout << '\n';
//}
//cout << endl;
}
void solve()
{
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
{
if (dis[i][j] > dis[i][k] + dis[k][j])
{
dis[i][j] = dis[i][k] + dis[k][j];
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
if (dis[i][j] == INF)
cout << 0 << " ";
else
cout << dis[i][j] << " ";
}
cout << '\n';
}
int a;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen("input.txt", "rt", stdin);
setting();
solve();
return 0;
}