메뉴 건너뛰기

Programing

BAEKJOON

1753

관리자 2019.12.21 11:42 조회 수 : 8

C++

#include <cstdio>

#include <queue>

#include <vector>

#include <climits>

 

using namespace std;

 

struct Edge

{

    int v;

    int dist;

};

 

const int MAX_N = 20001;

 

vector<Edge> v_vec[MAX_N];

int dist[MAX_N];

 

struct Q_info

{

    int v;

    int dist;

    bool operator < (const Q_info& info) const{

        return dist > info.dist;

    }

};

 

priority_queue<Q_info> pq;

int N,M,S;

 

void dijkstra(int s)

{

    dist[s] = 0;

    pq.push({s, 0});

 

    while(!pq.empty())

    {

        Q_info info = pq.top();

        pq.pop();

        //Q_info info2 = { 1, 3};

        //bool b = info.operator < (info2);

        //bool b2 = info < info2;

 

        if(info.dist != dist[info.v])

            continue;

 

        for(auto &e: v_vec[info.v])

        {

            if(dist[e.v] > info.dist + e.dist)

            {

                dist[e.v] = info.dist + e.dist;

                pq.push({e.v, dist[e.v]});

            }

        }

    }

}

 

int main()

{

    scanf("%d %d %d",&N,&M,&S);

    for(int i=0;i<M;i++){

        int a,b,c;

        scanf("%d %d %d",&a,&b,&c);

        v_vec[a].push_back({b,c});

    }

 

    for(int i=1; i<=N; ++i)

        dist[i] = INT_MAX;

 

    dijkstra (S);

    for(int i=1;i<=N;i++){

        if(dist[i]==INT_MAX)

            printf("INF\n");

        else

            printf("%d\n",dist[i]);

    }

 

    return 0;

}

번호 제목 글쓴이 날짜 조회 수
공지 안내사항 관리자 2019.12.21 163
45 2468 관리자 2019.12.21 8
44 2458 관리자 2020.04.11 44
43 2457 관리자 2020.04.11 47
42 2454 관리자 2020.04.11 43
41 2450 관리자 2020.04.11 40
40 2339 관리자 2020.04.11 41
39 2307 관리자 2019.12.21 8
38 2250 관리자 2019.12.21 8
37 2233 관리자 2019.12.21 6
36 2170 관리자 2019.12.21 7
35 2132 관리자 2019.12.21 7
34 2096 관리자 2019.12.21 8
33 2042 관리자 2020.04.11 45
32 2003 관리자 2020.04.11 39
31 1991 관리자 2019.12.21 8
30 1967 관리자 2019.12.21 6
29 1966 관리자 2020.04.11 45
28 1946 관리자 2020.04.11 39
27 1874 관리자 2020.04.11 43
26 1839 관리자 2020.04.11 44
25 17611 관리자 2019.12.21 7
» 1753 관리자 2019.12.21 8
23 1742 관리자 2020.04.11 6
22 1720 관리자 2020.04.11 6
21 16210 관리자 2020.04.11 57
20 16201 관리자 2020.04.11 44
19 15971 관리자 2019.12.21 6
18 14865 관리자 2019.12.21 8
17 14503 관리자 2019.12.21 7
16 14502 관리자 2019.12.21 6
위로