Cow and Fields

Bessie is out grazing on the farm, which consists of n fields connected by m bidirectional roads. She is currently at field 1, and will return to her home at field n at the end of the day.

The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has k special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them.

After the road is added, Bessie will return home on the shortest path from field 1 to field n. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him!

Input
The first line contains integers n, m, and k (2≤n≤2⋅105, n−1≤m≤2⋅105, 2≤k≤n) — the number of fields on the farm, the number of roads, and the number of special fields.

The second line contains k integers a1,a2,…,ak (1≤ai≤n) — the special fields. All ai are distinct.

The i-th of the following m lines contains integers xi and yi (1≤xi,yi≤n, xi≠yi), representing a bidirectional road between fields xi and yi.

It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them.

Output
Output one integer, the maximum possible length of the shortest path from field 1 to n after Farmer John installs one road optimally.

Examples

input
5 5 3
1 3 5
1 2
2 3
3 4
3 5
2 4
output
3
input
5 4 2
2 4
1 2
2 3
3 4
4 5
output
3

Note
The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields 3 and 5, and the resulting shortest path from 1 to 5 is length 3.

Cow and Fields_第1张图片
The graph for the second example is shown below. Farmer John must add a road between fields 2 and 4, and the resulting shortest path from 1 to 5 is length 3.

Cow and Fields_第2张图片
分别从 n n n 1 1 1出发跑一遍单元最短路, d n [ i ] dn[i] dn[i]表示节点 i i i到节点 n n n的最短路长度,同理 d 1 [ i ] d1[i] d1[i]表示节点 i i i到节点 1 1 1的最短路长度。将数组 s p e spe spe中存放的 k k k个特殊节点按 d 1 d1 d1从小到大排序,则 a n s = m i n ( d 1 [ n ] , m a x ( d 1 [ s p e [ i ] ] + d n [ s p e [ i + 1 ] ] + 1 ) ) ∀ i ∈ [ 1 , k − 1 ] ans=min(d1[n],max(d1[spe[i]]+dn[spe[i+1]]+1))\forall i\in\mathbb[1,k-1] ans=min(d1[n],max(d1[spe[i]]+dn[spe[i+1]]+1))i[1,k1]

#include

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector
#define vc vector
#define pii pair
#define pll pair
#define pil pair
#define pli pair
#define mii unordered_map
#define msi unordered_map
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 2e5 + 10;
int head[N], ver[N << 1], Next[N << 1], tot;
int n, m, k, dn[N], d1[N];
int spe[N];
bool v[N];

inline void add(int x, int y) {
    ver[++tot] = y;
    Next[tot] = head[x];
    head[x] = tot;
}

inline void read() {
    n = qr(), m = qr(), k = qr();
    repi(i, 1, k) spe[i] = qr();
    repi(i, 1, m) {
        int x = qr(), y = qr();
        add(x, y), add(y, x);
    }
}

inline void spfa1() {
    memset(dn, 0x3f, sizeof(dn));
    deque<int> q;
    dn[n] = 0, q.pb(n), v[n] = true;
    while (!q.empty()) {
        int x = q.front();
        q.pop_front(), v[x] = false;
        reps(x) {
            int y = ver[i];
            if (dn[y] > dn[x] + 1) {
                dn[y] = dn[x] + 1;
                if (!v[y]) {
                    if (q.empty() || dn[y] > dn[q.front()])q.pb(y);
                    else q.push_front(y);
                    v[y] = true;
                }
            }
        }
    }
}

inline void spfa2() {
    memset(d1, 0x3f, sizeof(d1));
    deque<int> q;
    d1[1] = 0, q.pb(1), v[1] = true;
    while (!q.empty()) {
        int x = q.front();
        q.pop_front(), v[x] = false;
        reps(x) {
            int y = ver[i];
            if (d1[y] > d1[x] + 1) {
                d1[y] = d1[x] + 1;
                if (!v[y]) {
                    if (q.empty() || d1[y] > d1[q.front()])q.pb(y);
                    else q.push_front(y);
                    v[y] = true;
                }
            }
        }
    }
}

inline bool cmp(int x, int y) {
    return d1[x] < d1[y];
}

inline void solve() {
    sort(spe + 1, spe + k + 1, cmp);
    int res = 0;
    repi(i, 1, k - 1)res = max(res, d1[spe[i]] + dn[spe[i + 1]] + 1);
    pi(min(res, d1[n]));
}


int main() {
    read();
    spfa1();
    spfa2();
    solve();
    return 0;
}

你可能感兴趣的:(ACM)