길민호(ethan.mino)
코딩수첩
길민호(ethan.mino)
전체 방문자
오늘
어제
  • 분류 전체보기 (215)
    • Computer Science (0)
    • Web (6)
      • CSS (0)
      • HTML (0)
    • Node.js (0)
    • Javascript (2)
    • Java (46)
      • Spring (27)
      • Jsp (0)
    • C\C++ (2)
    • Programming (0)
    • AI (0)
    • Database (7)
    • Git (5)
    • Algorithm (119)
      • Stack (0)
      • Queue (0)
      • Linked List (0)
      • Sort (0)
      • Simulation (27)
      • Recursion (0)
      • Backtracking (4)
      • Two Pointer (3)
      • Dynamic Programming (19)
      • Greedy (10)
      • Graph (3)
      • Dijkstra (1)
      • BFS\DFS (8)
      • Floyd (1)
      • MST (4)
      • Tree (4)
      • Binary Search (8)
      • Binary Search Tree (4)
    • IntelliJ (4)
    • Vscode (0)
    • Operating System (0)
    • 후기 (3)
    • 성장일지 (13)
    • 스터디 (7)
    • 설치 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • ㅡ

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
길민호(ethan.mino)

코딩수첩

Algorithm/Dijkstra

[SW Expert Academy 1249번] 보급로 (C++)

2022. 7. 1. 23:30

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV15QRX6APsCFAYD 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


보급로 문제는 다익스트라를 이용하여 해결할 수 있습니다.

다익스트라는 하나의 시작점으로부터 다른 모든 정점까지의 최단 거리를 구하는 알고리즘입니다.

 

board[x][y]의 값이 c이면, board[x][y]로 이동하는 비용은 c입니다.

따라서 이 값을, 간선의 비용으로 두고 다익스트라를 이용하여 시작점에서 도착점까지의 최단 거리를 계산하면 됩니다.

 

아래는 우선순위 큐를 이용한 다익스트라 코드입니다.

// Created by 길민호 on 2022/07/01.

#include <bits/stdc++.h>
using namespace std;

#define SIZE 101
#define d first
#define pos second
#define x first
#define y second
#define INF 20000
typedef pair<int, int> pii;

int dist[SIZE][SIZE];
pair<int, int> pre[SIZE][SIZE];

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int shortest(int map[SIZE][SIZE], pii st, pii end, int n){
    fill(&dist[0][0], &dist[n - 1][n], INF);
    dist[st.x][st.y] = 0;

    priority_queue<pair<int, pair<int, int>>,
            vector<pair<int, pair<int, int>>>,
            greater<pair<int, pair<int, int>>>> pq;

    pq.push({dist[st.x][st.y], st});
    while(!pq.empty()){
        pair<int, pii> cur = pq.top(); pq.pop();
        if(dist[cur.pos.x][cur.pos.y] != cur.d) continue;

        for(int dir = 0; dir < 4; dir++){
            int nx = cur.pos.x + dx[dir];
            int ny = cur.pos.y + dy[dir];

            if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
            if(dist[nx][ny] > dist[cur.pos.x][cur.pos.y] + map[nx][ny]){
                dist[nx][ny] = dist[cur.pos.x][cur.pos.y] + map[nx][ny];
                pq.push({dist[nx][ny], {nx, ny}});
                pre[nx][ny] = {cur.pos.x, cur.pos.y};
            }
        }
    }

    return dist[end.x][end.y];
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t; cin >> t;
    int cur = 1;

    while(t--){
        int n; cin >> n;
        int board[SIZE][SIZE];

        for(int i = 0; i < n; i++){
            string str; cin >> str;
            for(int j = 0; j < str.size(); j++)
                board[i][j] = str[j] - 48;
        }

        pii st = {0, 0}, en = {n - 1, n - 1};

        int d = shortest(board, st, en, n);
        cout << "#" << cur++ << " " << d << "\n";
    }
}
    길민호(ethan.mino)
    길민호(ethan.mino)
    💻 호기심 많은 서버 개발자 길민호입니다.

    티스토리툴바