ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 택배
    백준알고리즘 2019. 9. 29. 20:41

    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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<utility>
     
    using namespace std;
     
    int tc,n, d, c;
    vector<pair<int,int> > map[20000];
    int visit[20000];
    int dist[20000];
    int node, edge;
    int INF = 987654321;
    int mapp[200][200];
     
     
    void dijkstra(int start) {
        visit[start] = 0;
        priority_queue<pair<intlong long> > pq;
        pq.push(make_pair(start, 0));
        while (!pq.empty()) {
            int current = pq.top().first;
            int distance = -pq.top().second;
            pq.pop();
            if (visit[current] < distance) continue;
            for(int i = 0; i< map[current].size(); i++){
                int next = map[current][i].first;
                int nextDistance = distance + map[current][i].second;
                if (nextDistance < visit[next]) {
                    visit[next] = nextDistance;
                    dist[next] = current+1;
                    pq.push(make_pair(next, -nextDistance));
                }
            }
        }
    }
     
     
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cin >> node >> edge;
     
     
        for (int i = 0; i < edge; i++) {
            int a, b, c;
            cin >> a >> b >> c;
            map[a - 1].push_back({b-1,c});
            map[b - 1].push_back({ a - 1,c });
     
        }
     
        for(int i = 0; i < node;i++){
            for (int i = 0; i < node; i++) {
                visit[i] = INF;
            }
            dijkstra(i);
            for (int j = 0; j < node; j++) {
                if (visit[j] != INF && i != j) {
                    mapp[j][i] = dist[j];
                }
            }
        }
     
        for(int i = 0;i < node; i++){
            for(int j = 0; j < node; j++){
                if(i != j)
                cout<<mapp[i][j]<<' ';
                else cout<<'-'<<' ';
            }cout<<'\n';
        }
     
        return 0;
    }
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
     

    다익스트라 문제, 다익스트라알고리즘 복습하면서 풀어본 문제

    다익스트라와 최소신장트리는 자꾸까먹는것같아서 복습차원에서 다시푼문제

    '백준알고리즘' 카테고리의 다른 글

    맥주 마시면서 걸어가기  (0) 2019.10.01
    (4991) 로봇 청소기  (0) 2019.10.01
    네트워크 연결  (0) 2019.09.29
    최소비용 구하기  (0) 2019.09.29
    최단 경로  (0) 2019.09.29

    댓글

Designed by Tistory.