ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1307-D, *1900
    코드포스 2022. 1. 7. 01:05
     

    Problem - D - Codeforces

     

    codeforces.com

     

     

    풀이

    Xa+Yb 가 가장 클 때를 찾아야하는 이유는 Xa+Yb+1 이 노드 1부터 n 까지 (노트 a, b를 경유하는) 최단거리이기 때문

    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 <bits/stdc++.h>
    #define endl "\n"
    #define ooop(i, n) for(int i = 0; i < n; i++)
    #define loop(i, n) for(int i = 1; i <= n; i++)
    #define all(v) (v).begin(), (v).end()
     
    using namespace std;
    typedef long long ll;
    typedef pair<intint> pi;
    typedef pair<ll, ll> pl;
     
    const int INF = 1e6;
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int n, m, k; cin >> n >> m >> k;
        vector<int> special(k);
        for(auto& e: special) cin >> e;
        map<intvector<int> > edge;
     
        loop(i, m){
            int u, v; cin >> u >> v;
            edge[u].emplace_back(v);
            edge[v].emplace_back(u);
        }
     
        vector<int> cnt_from_1(n+1, INF);
        queue<int> q;
        q.push(1);
        cnt_from_1[1= 0;
        while(!q.empty()){
            auto cur = q.front(); q.pop();
            int nex_cnt = cnt_from_1[cur] + 1;
            for(const auto nex: edge[cur]){
                if(nex_cnt < cnt_from_1[nex]){
                    if(nex != n) q.push(nex);
                    cnt_from_1[nex] = nex_cnt;
                }
            }
        }
     
        vector<int> cnt_from_n(n+1, INF);
        q.push(n);
        cnt_from_n[n] = 0;
        while(!q.empty()){
            auto cur = q.front(); q.pop();
            int nex_cnt = cnt_from_n[cur] + 1;
            for(const auto nex: edge[cur]){
                if(nex_cnt < cnt_from_n[nex]){
                    if(nex != 1) q.push(nex);
                    cnt_from_n[nex] = nex_cnt;
                }
            }
        }
     
        vector<pi> tmp;
        ooop(i, k) tmp.emplace_back(cnt_from_1[special[i]]-cnt_from_n[special[i]], special[i]);
        sort(all(tmp));
        int maxval = 0, max_ind;
        ooop(i, k-1){
            int a = tmp[i].second, b = tmp[i+1].second;
            int dis = cnt_from_1[a] + cnt_from_n[b];
            if(maxval < dis){
                maxval = dis;
                max_ind = i;
            }
        }
     
        cout << min(cnt_from_1[n], maxval+1<< endl;
     
        return 0;

    '코드포스' 카테고리의 다른 글

    Codeforces Round 835 (Div. 4)  (0) 2023.11.11
    1651-C  (0) 2022.03.15
    1307-C, *1500  (0) 2022.01.07
    1622-C  (0) 2021.12.29
    1623-C  (0) 2021.12.29

    댓글

Designed by Tistory.