Problem: Example

Input

4 2 1 1
1 2
3
4
3
1 2
2 4
3 4


Output

2 1

Solution (in CPP):

Solution:
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1001001001;
const ll LONGINF = 1001001001001001LL;
const ll MOD = 1000000007LL;

vector< pair<int, int> > bfs(const vector< vector< pair<int, int> > > &G,
                             int v) {
    int N = G.size();
    vector< pair<int, int> > res(N, make_pair(INF, INF));
    res[v] = make_pair(0, v);

    // コスト、最小 idx、現在地
    deque< tuple<int, int, int> > deq;
    deq.emplace_back(0, v, v);
    while(deq.size()) {
        int cost, min_idx, pos; tie(cost, min_idx, pos) = deq.front();
        deq.pop_front();

        for(auto e : G[pos]) {
            int to, c; tie(to, c) = e;
            int n_cost = cost + c, n_min_idx = min(min_idx, to);
            if(res[to] > make_pair(n_cost, n_min_idx)) {
                res[to] = make_pair(n_cost, n_min_idx);
                if(c == 1) deq.emplace_back(n_cost, n_min_idx, to);
                else deq.emplace_front(n_cost, n_min_idx, to);
            }
        }
    }
    return res;
}

int main() {
    int N, A, B, C; cin >> N >> A >> B >> C;
    vector< vector< pair<int, int> > > G(N + 3);
    int PA = N, PB = N + 1, PC = N + 2;
    for(int i=0; i<A; i++) {
        int a; cin >> a; a--;
        G[a].emplace_back(PA, 0);
        G[PA].emplace_back(a, 0);
    }
    for(int i=0; i<B; i++) {
        int b; cin >> b; b--;
        G[b].emplace_back(PB, 0);
        G[PB].emplace_back(b, 0);
    }
    for(int i=0; i<C; i++) {
        int c; cin >> c; c--;
        G[c].emplace_back(PC, 0);
        G[PC].emplace_back(c, 0);
    }

    int M; cin >> M;
    for(int i=0; i<M; i++) {
        int x, y; cin >> x >> y; x--; y--;
        G[x].emplace_back(y, 1);
        G[y].emplace_back(x, 1);
    }

    auto da = bfs(G, PA);
    auto db = bfs(G, PB);
    auto dc = bfs(G, PC);

    pair<int, int> ans(INF, INF);
    for(int i=0; i<N; i++) {
        int cost = da[i].first + db[i].first + dc[i].first;
        int midx = min({da[i].second, db[i].second, dc[i].second});
        if(ans > make_pair(cost, midx)) ans = make_pair(cost, midx);
    }
    cout << ans.first << " " << ans.second + 1 << endl;
    return 0;
}