hdoj 4607 Park Visit 【树的直径的应用 求一棵树中经过k个点需要走的最短距离】

来源:互联网 发布:曹变蛟 知乎 编辑:程序博客网 时间:2024/04/29 08:18

Park Visit

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2754    Accepted Submission(s): 1249


Problem Description
Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all of them. After consideration, she decides to visit only K spots among them. She takes out a map of the park, and luckily, finds that there're entrances at each feature spot! Claire wants to choose an entrance, and find a way of visit to minimize the distance she has to walk. For convenience, we can assume the length of all paths are 1.
Claire is too tired. Can you help her?
 

Input
An integer T(T≤20) will exist in the first line of input, indicating the number of test cases.
Each test case begins with two integers N and M(1≤N,M≤105), which respectively denotes the number of nodes and queries.
The following (N-1) lines, each with a pair of integers (u,v), describe the tree edges.
The following M lines, each with an integer K(1≤K≤N), describe the queries.
The nodes are labeled from 1 to N.
 

Output
For each query, output the minimum walking distance, one per line.
 

Sample Input
14 23 21 24 224
 

Sample Output
14
 

题意:给你一个N个点和N-1边组成的图(图连通 且 无环),每条边距离为1。问你在图中经过k个点需要行走的最短距离。(起点任意选)


树的直径的应用 。 为了方便表述,下面我用S-T路径来表示树的直径。

思路:先求出S-T路径长度ans,分类讨论(我们已经知道这条S-T路径上有ans+1个点)

一:k <= ans + 1,直接在这条路径上任意选起点,最短距离为k - 1;

二:k > ans+1,说明我们即使走完ans + 1个点也不够。但是我们已经知道这条路径是树的直径,因此它的S-T两个端点度数都为1,这就说明端点只和S-T路径上的点有直接边相连。

针对第二种情况,为了走够k个点,我们只能选择S-T路径之外的点来走。但由于图是连通图且无环,S-T路径之外的点一定会与S-T路径上的点有直接边连接。给个图解释

          

S —— 2 —— 3 —— 4 ——T
                      |          |
                      5         6

如图,若题目要求走够6个点,我们在走完S-T这条路径之后,才仅仅走了5个点。没办法,剩余一个点只能走路径之外的点,如图我们可以知道如果要走就要走一个来回(如 3 - > 4 - >3),最后要回到S-T路径上,继续走S-T路径。这样最少需要走(k - ans - 1) * 2 + ans。




AC代码:

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define MAXN 100000+10#define MAXM 200000+100using namespace std;struct Edge{    int from, to, val, next;};Edge edge[MAXM];int head[MAXN], edgenum;int dist[MAXN];bool vis[MAXN];int N, M;int node;//最长路S-T端点int ans;//记录最长路void init(){    edgenum = 0;    memset(head, -1, sizeof(head));}void addEdge(int u, int v, int w){    Edge E = {u, v, w, head[u]};    edge[edgenum] = E;    head[u] = edgenum++;}void getMap(){    int a, b;    for(int i = 1; i < N; i++)    {        scanf("%d%d", &a, &b);        addEdge(a, b, 1);        addEdge(b, a, 1);    }}void SPFA(int sx){    queue<int> Q;    memset(dist, 0, sizeof(dist));    memset(vis, false, sizeof(vis));    vis[sx] = true;    ans = 0;    node = sx;    Q.push(sx);    while(!Q.empty())    {        int u = Q.front();        Q.pop();        for(int i = head[u]; i != -1; i = edge[i].next)        {            Edge E = edge[i];            if(!vis[E.to] && dist[E.to] < dist[u] + E.val)            {                vis[E.to] = true;                dist[E.to] = dist[u] + E.val;                if(dist[E.to] > ans)                {                    ans = dist[E.to];                    node = E.to;                }                Q.push(E.to);            }        }    }}void solve(){    SPFA(1);//找S-T端点    SPFA(node);//求最长路    int k;    for(int i = 0; i < M; i++)    {        scanf("%d", &k);        if(k <= ans + 1)//最长路上有ans + 1个点            printf("%d\n", k - 1);        else            printf("%d\n", (k - ans - 1) * 2 + ans);    }}int main(){    int t;    scanf("%d", &t);    while(t--)    {        scanf("%d%d", &N, &M);        init();        getMap();        solve();    }    return 0;}



0 0