๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 4485๋ฒˆ - ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€?

by dkswnkk 2022. 11. 2.

๋ฌธ์ œ

 

4485๋ฒˆ: ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€?

์ ค๋‹ค์˜ ์ „์„ค ๊ฒŒ์ž„์—์„œ ํ™”ํ์˜ ๋‹จ์œ„๋Š” ๋ฃจํ”ผ(rupee)๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ„ํ˜น '๋„๋‘‘๋ฃจํ”ผ'๋ผ ๋ถˆ๋ฆฌ๋Š” ๊ฒ€์ •์ƒ‰ ๋ฃจํ”ผ๋„ ์กด์žฌํ•˜๋Š”๋ฐ, ์ด๊ฑธ ํš๋“ํ•˜๋ฉด ์˜คํžˆ๋ ค ์†Œ์ง€ํ•œ ๋ฃจํ”ผ๊ฐ€ ๊ฐ์†Œํ•˜๊ฒŒ ๋œ๋‹ค! ์ ค๋‹ค์˜ ์ „์„ค ์‹œ๋ฆฌ์ฆˆ์˜ ์ฃผ

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <memory.h>
#define INF 1e9

using namespace std;

int N;
int map[126][126];
int d[126][126];
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};

void dijkstra(){
    priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>>>pq;
    pq.push({map[0][0], {0,0}});
    d[0][0] = map[0][0];
    
    while(!pq.empty()){
        int dist = pq.top().first;
        int x = pq.top().second.first;
        int y = pq.top().second.second;
        pq.pop();
        
        for(int i=0; i<4; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx>=0 && nx<N && ny>=0 && ny<N){
                int cost = dist + map[nx][ny];
                if(cost < d[nx][ny]){
                    d[nx][ny] = cost;
                    pq.push({cost,{nx, ny}});
                }
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int T = 0;
    while(cin>>N){
        T++;
        if(N==0) break;
        for(int i=0; i<N; i++){
            for(int k=0; k<N; k++){
                cin>>map[i][k];
            }
        }
        
        for(int i=0; i<126; i++){
            for(int k=0; k<126; k++){
                d[i][k] = INF;
            }
        }
        
        dijkstra();
        
        cout<<"Problem "<<T<<": "<< d[N-1][N-1]<<'\n';
        memset(d, 0, sizeof(d));
        memset(map, 0, sizeof(map));
        
    }
}

 

ํ’€์ด

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ํ˜•์‹์˜ ๋‹ค์ต์ŠคํŠธ๋ผ์ž…๋‹ˆ๋‹ค. ์œ„์ฒ˜๋Ÿผ ๊ธฐ์กด์˜ ๋…ธ๋“œ - ๊ฐ„์„  ํ˜•์‹์˜ ๋‹ค์ต์ŠคํŠธ๋ผ์™€๋Š” ์‚ด์ง ๋Š๋‚Œ์ด ๋‹ค๋ฅด๋‹ˆ ์ด๋ฒˆ ๋ฌธ์ œ ํ˜•์‹์˜ ํ’€์ด๋„ ์ตํ˜€๋‘๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

[C++, ํ…œํ”Œ๋ฆฟ] ๋‹ค์ต์ŠคํŠธ๋ผ

๋‹ค์ต์ŠคํŠธ๋ผ #include #include #include #define INF 1E9 using namespace std; // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜(V), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜(E), ์‹œ์ž‘ ๋…ธ๋“œ ๋ฒˆํ˜ธ(K) // ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 20000๊ฐœ๋ผ๊ณ  ๊ฐ€์ • int V, E, K; // ๊ฐ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ

dkswnkk.tistory.com

๋Œ“๊ธ€