Hrbust 2292 Tom and Jerry【树上最短路+枚举】

Tom and Jerry
Time Limit: 3000 MSMemory Limit: 327680 K
Total Submit: 68(18 users)Total Accepted: 20(16 users)Rating: Special Judge: No
Description

Tom and Jerry are trapped in a tree with n vertex (2 <= n <= 100000). Initially they are on two different vertex, as we know, jerry will run to his home and tom want to catch jerry before he reach his home. But unluckily, there are no home in this tree, so jerry’s being caught is just a matter of time. Your task is to calculate the minimal time tom can catch jerry (Tom catch Jerry means they are at the some vertex at the same time). To make things simple, we define that every second, Jerry make his choice first and then Tom make his choice. Of course, both of them will choose their way optimally. 

We define every move as follow:

At every move, Tom or Jerry can choose whether to move to an adjacent vertex or just stay where he is. 

We define the speed as follows:

Speed v means Tom or Jerry can make at most v moves in every second.

You should know that Jerry's speed is always 1, Tom's speed is v.

Input

The first line contains an integer T, then T cases follows. 

In each case, there are n+1 lines of input. The first line is a single integer n, indicating the number of vertex numbered from 0 to n-1. Each of the next n-1 lines contains 2 integers a and b, means there is an edge between a and b.

The last line contains 3 integers t, j and v(v < min(n, 20)), means the initial place of Tom and Jerry and Tom's speed.

Output
For each case, you should output the minimal time Tom needed to catch Jerry.
Sample Input
2
9
0 1
1 2
2 3
3 4
4 5
2 6
6 7
7 8
5 0 1
9
0 1
1 2
2 3
3 4
4 5
2 6
6 7
7 8
0 5 1
Sample Output
6
5
Source
"尚学堂杯"哈尔滨理工大学第五届程序设计团队赛(正式)

题目大意:

现在有N个点的一棵树,初始的时候猫和老鼠的位子已知,老鼠的速度永远都是1,猫的速度是V.

对于速度,表示一秒可以进行的操作次数,每一秒都可以进行两种操作:移动到相邻的点,或者是不动。

问最慢猫抓到耗子的时间。


思路:


1、肯定耗子想要跑的尽可能的远,而且在跑到那个终点位子End之前的路径上,猫不会抓到耗子。

那么我们可以通过枚举End这个点来判断,对于一个点u,如果耗子到达这个点的耗用时间量小于猫到达这个点的耗用时间量,那么这个点就可以作为End点。而且在这个点u猫抓到耗子的时间就是猫到达这个点的时间。

过程维护最大,那么满足条件的最大时间,就是最终答案。


2、因为题目要求耗子和猫都会做出最优的决策,那么肯定是要处理两次树上的最短路,处理出来猫和耗子到各个点的最短路径长度,然后计算时间维护满足条件的最大时间即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int >mp[100600];
int vis[100600];
int dis[2][100600];
void Dfs(int u,int d)
{
    vis[u]=1;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            dis[d][v]=dis[d][u]+1;
            Dfs(v,d);
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(dis,0,sizeof(dis));
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        int t,j,v;
        scanf("%d%d%d",&t,&j,&v);
        memset(vis,0,sizeof(vis));
        Dfs(t,0);
        memset(vis,0,sizeof(vis));
        Dfs(j,1);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int tmp=dis[0][i]/v;
            if(dis[0][i]%v!=0)tmp++;
            if(tmp>dis[1][i])
            {
                ans=max(ans,tmp);
            }
        }
        printf("%d\n",ans);
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值