πŸ™‡β€β™€οΈ[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;
}

νƒœκ·Έ:

μΉ΄ν…Œκ³ λ¦¬:

μ—…λ°μ΄νŠΈ: