Transmission Door
Description
A businessman in a capital city often goes to the towns to do business, and they do it on their own lines to save time.
Suppose there are N towns, the capital is numbered 1, the merchant departs from the capital, there are road connections between the other towns, and if there are direct links between any two towns, it takes a unit of time to travel between them. The country has a well-developed road network, which can reach any town from the capital, and the road network will not be a ring.
Your task is to help the businessman calculate his shortest travel time.
Input
The first line in the input file has an integer n,1<=n<=30 000, which is the number of towns. N-1 lines below, each line consists of two integers a and b (1<=a, b<=n; a<>b), indicating that town A and town B have road connections. In the N+1 Act an integer m, the following M-line, each line has the number of towns that the merchant needs to pass sequentially.
Output
Outputs the shortest time the merchant travels in the output file.
Sample Input
5
1 2
1 5
3 5
4 5
4
1
3
2
5
Sample Output
7
Ideas
very naked The topic of the recent public ancestor, run once SPFA record the distance of the capital to each city and then find the nearest public ancestor of two points, so that the shortest possible two points can be calculated based on the distance and the nearest public ancestor.
#include <bits/stdc++.h>using namespace Std;const int maxn = 30005;int tot = 0,road[maxn],dis[maxn],flag[maxn],vis [Maxn],head[maxn],fa[maxn],ancestor[maxn];struct edge{int To,next;} Edge[maxn<<1];int tt = 0,h[maxn],answer[maxn];struct query{int q,next;int index;} Qry[maxn<<1];void init (int N) {for (int i = 0;i <= n;i++) {fa[i] = i;ancestor[i] = 0;vis[i] = False;head[i] = H[i] =-1;}} void Addedge (int u,int v) {Edge[tot] = (edge) {V,head[u]};head[u] = tot++;} void add_query (int u,int v,int index) {QRY[TT] = (query) {V,h[u],index};h[u] = Tt++;qry[tt] = (query) {U,h[v],index};h[v] = t t++;} void Spfa () {queue<int>que;memset (dis,0x3f,sizeof (dis)); memset (flag,false,sizeof (flag)); Que.push (1);d is[1] = 0;flag[1] = True;while (!que.empty ()) {int u = que.front (); Que.pop (); Flag[u] = false;for (int i = head[u];i! = -1;i = EDG E[i].next) {int v = edge[i].to;if (Dis[u] + 1 < dis[v]) {Dis[v] = Dis[u] + 1;if (!flag[v]) {Que.push (v); flag[v] = true;}} }}}int find (int x) {int r = x;while (r! = FA[R]) r = Fa[r];int i = x,j;while (i! = r) {j = fa[i];fa[i] = R;i = j;} return r;} void Union (int x,int y) {x = find (x), y = Find (Y), if (x = = y) return;fa[y] = x;} void Targin_lca (int u) {ancestor[u] = U;vis[u] = true;for (int i = head[u]; I! = -1;i = edge[i].next) {int v = edge[i].to;if (Vis[v]) Continue;targin_lca (v); Union (u,v); Ancestor[find (u)] = u;} for (int i = h[u];i! = -1;i = qry[i].next) {int v = qry[i].q;if (Vis[v]) {Answer[qry[i].index] = Ancestor[find (v)];}} int main () {int n,m,u,v,sum = 0;SCANF ("%d", &n), Init (N), for (int i = 1;i < n;i++) {scanf ("%d%d", &u,&v); added GE (u,v); Addedge (v,u);} SPFA (); scanf ("%d", &m), scanf ("%d", &road[0]), for (int i = 1;i < m;i++) scanf ("%d", &road[i]), Add_query ( Road[i-1],road[i],i); Targin_lca (1); for (int i = 1;i < m;i++) {sum + = Dis[road[i]] + dis[road[i-1]]-2*dis[answer[i]];} printf ("%d\n", sum); return 0;}
Codevs 1036 Business Travel (Targin for LCA)