본문 바로가기

BOJ/C++

[BOJ] 플로이드

[문제]

https://www.acmicpc.net/problem/11404

 

11404번: 플로이드

첫째 줄에 도시의 개수 n(1 ≤ n ≤ 100)이 주어지고 둘째 줄에는 버스의 개수 m(1 ≤ m ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스의 출발 도시의 번호가 주어진다. 버스의 정보는 버스의 시작 도시 a, 도착 도시 b, 한 번 타는데 필요한 비용 c로 이루어져 있다. 시작 도시와 도착 도시가 같은 경우는 없다. 비용은 100,000보다 작거나 같은 자연수이다. 시작

www.acmicpc.net

[풀이]

 

11404 맞았습니다!! 2028 24 C++14 / 수정 934 35초 전

 

1) 제목부터 플로이드 와셜 알고리즘을 쓰라고 되어있어서 플로이드 와셜 알고리즘을 사용한다. 

 

2) 굳이 헷갈릴 수 있는 점을 뽑자면 "시작 도시와 도착 도시를 연결하는 노선은 하나가 아닐 수 있다."

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <vector>
 
#define MAX_M 100001
#define INF 987654321
 
using namespace std;
 
//플로이드
// typedef pair<int, int> pp;
int res[101];
int bus_fee[101][101];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    int n, m;
    cin >> n >> m;
 
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (i == j) bus_fee[i][j] = 0;
            else bus_fee[i][j] = INF;
        }
    }
 
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
 
        bus_fee[a][b] = bus_fee[a][b] > c ? c : bus_fee[a][b];
    }
    //input
 
    for (int a = 1; a <= n; a++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                bus_fee[i][j] = min(bus_fee[i][j], bus_fee[i][a] + bus_fee[a][j]);
            }
        }
    }
 
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (bus_fee[i][j] == INF) cout << 0 << " ";
            else cout << bus_fee[i][j] << " ";
        }
        cout << '\n';
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

'BOJ > C++' 카테고리의 다른 글

[BOJ] 안전영역  (0) 2019.08.16
[BOJ] 분수의 합  (0) 2019.08.09
[BOJ] 타임머신  (0) 2019.08.09
[BOJ] 최단 경로  (0) 2019.08.09
[BOJ] 게임개발  (0) 2019.08.09