ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 해킹
    백준알고리즘 2019. 9. 28. 23:34

    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
    76
    77
    78
    79
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<utility>
    #include<cstring>
     
    using namespace std;
     
    int tc,n, d, c;
    vector<pair<intlong long> > map[10000];
    long long visit[10000];
    bool dist[10000];
    vector<pair<intint> > vv;
    int v;
    int INF = 100000000;
     
     
    void dijkstra(int start) {
        visit[start] = 0;
        dist[start] = true;
        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] = true;
                    pq.push(make_pair(next, -nextDistance));
                }
            }
        }
     
    }
     
    int main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cin >> tc;
        for (int t = 0; t < tc; t++) {
            cin >> n >> d >> c;
            for (int i = 0; i < n; i++) {
                visit[i] = INF;
            }
            for (int i = 0; i < d; i++) {
                int a, b, s;
                cin >> a >> b >> s;
                //map[a - 1].push_back({ b - 1,s });
                map[b - 1].push_back({ a - 1,s });
            }
     
            dijkstra(c-1);
            int maxx = 0;
            for (int i = 0; i < n; i++) {
                if (visit[i] != INF)
                    v++;
                if (visit[i] > maxx && visit[i] != INF)
                    maxx = visit[i];
            }
            
            cout << v <<' '<<maxx <<'\n';
            for(int i = 0; i< n; i++){
                dist[i] = false;
            }
            for(int i = 0; i< n; i++){
                visit[i] = 0;
            }
            for (int i = 0; i < n; i++) {
                map[i].clear();
            }
            v = 0;
        }
        return 0;
    }
    http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
     

    다익스트라 알고리즘이다.

    처음써봤고, 나동빈님 강의를 보고 풀어본 문제중 하나다.

    컴퓨터 해킹갯수를 세는게, INF가 아닐때와 d가 true일때 생각해본건데..

    INF개수를 세는게 맞았다

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

    최소비용 구하기  (0) 2019.09.29
    최단 경로  (0) 2019.09.29
    최소 스패닝 트리  (0) 2019.09.27
    통나무 옮기기  (0) 2019.09.27
    다리만들기2  (0) 2019.09.27

    댓글

Designed by Tistory.