Bus Pass-ZOJ2913

来源:互联网 发布:python time sleep参数 编辑:程序博客网 时间:2024/05/19 17:58

Bus Pass

Time Limit: 5 Seconds Memory Limit: 32768 KB

You travel a lot by bus and the costs of all the seperate tickets are starting to add up.Therefore you want to see if it might be advantageous for you to buy a bus pass.The way the bus system works in your country (and also in the Netherlands) is as follows:
when you buy a bus pass, you have to indicate a center zone and a star value. You are allowed to travel freely in any zone which has a distance to your center zone which is less than your star value. For example, if you have a star value of one, you can only travel in your center zone. If you have a star value of two, you can also travel in all adjacent zones, et cetera.
You have a list of all bus trips you frequently make, and would like to determine the minimum star value you need to make all these trips using your buss pass. But this is not always an easy task. For example look at the following figure:
这里写图片描述
Here you want to be able to travel from A to B and from B to D. The best center zone is 7400, for which you only need a star value of 4. Note that you do not even visit this zone on your trips!

Input

On the first line an integert(1 <=t<= 100): the number of test cases. Then for each test case:
One line with two integersnz(2 <=nz<= 9 999) andnr(1 <=nr<= 10): the number of zones and the number of bus trips, respectively.
nz lines starting with two integers idi (1 <= idi <= 9 999) and mzi (1 <= mzi <= 10), a number identifying the i-th zone and the number of zones adjacent to it, followed by mzi integers: the numbers of the adjacent zones.
nr lines starting with one integer mri (1 <= mri <= 20), indicating the number of zones the ith bus trip visits, followed by mri integers: the numbers of the zones through which the bus passes in the order in which they are visited.All zones are connected, either directly or via other zones.

Output
For each test case:
One line with two integers, the minimum star value and the id of a center zone which achieves this minimum star value. If there are multiple possibilities, choose the zone with the lowest number.

Sample Input
1
17 2
7400 6 7401 7402 7403 7404 7405 7406
7401 6 7412 7402 7400 7406 7410 7411
7402 5 7412 7403 7400 7401 7411
7403 6 7413 7414 7404 7400 7402 7412
7404 5 7403 7414 7415 7405 7400
7405 6 7404 7415 7407 7408 7406 7400
7406 7 7400 7405 7407 7408 7409 7410 7401
7407 4 7408 7406 7405 7415
7408 4 7409 7406 7405 7407
7409 3 7410 7406 7408
7410 4 7411 7401 7406 7409
7411 5 7416 7412 7402 7401 7410
7412 6 7416 7411 7401 7402 7403 7413
7413 3 7412 7403 7414
7414 3 7413 7403 7404
7415 3 7404 7405 7407
7416 2 7411 7412
5 7409 7408 7407 7405 7415
6 7415 7404 7414 7413 7412 7416

Sample Output
4 7400

分析:
典型的BFS问题,题目是问哪个区域到公交路线上所有区域的最大距离最小。我们可以反向思考,从路线上的所有区域出发,记录到各个区域的距离,每次更新保留较大的值(因为要满足所有的在路线上的区域)。
注意:
(1):由于要多次进行BFS,cur表示当前进行的次数,用reach[]数组记录各个区域被访问的次数,当reach[s]==cur表示,s区域在第cur次已经被访问过了。
(2):用val值表示区域的距离(出发点为1),在处理当前层区域时,会将下一层的区域入队列保存。但是这样就存在一个问题了,只有一个val,若果将val++,则在处理当前层的下一个区域是就会有问题(还可以再用一个变量表示当前的,一个变量表示下一层的,但是他们都在一个队列里面,就会不知道接下来的区域到底是哪一层的了),所以这里我们用了两个队列,进行滚动操作,(若果q[b]保存的是当前层,则下一层保存在q[a]里,同理若果q[a]保存的是当前层,则下一层保存在q[b]里)

AC代码:

#include <iostream>#include <cstring>#include <cstdio>#include <cmath>using namespace std;const int zmax = 10000;const int INF = 100000;int nz,nr;int mz[zmax];       //记录每个区域相邻区域的个数int Edge[zmax][10]; //Edge[i][j]表示第i个区域第j个相邻的区域int res[zmax];      //结果int cur;            //记录路线上的第几个区域正在执行BFSint reach[zmax];    //reach[s]==cur,表示当前的BFS已经访问过了s区域void BFS(int s){    int a,b,val,at;    queue<int> q[2];    a=0,b=1,val=0;    if(reach[s]<cur)    {        q[b].push(s);        reach[s]=cur;        res[s]=max(res[s],val+1);    }    while(!q[b].empty())    {        swap(a,b);        val++;        while(!q[a].empty())        {            at=q[a].front();            q[a].pop();            for(int i=0;i<ma[at];i++)            {                if(reach[[Edge[at][i]]<cur)                {                    q[b].push(Edge[at][i]);                    reach[Edge[at][i]]=cur;                    res[Edge[at][i]]=max(res[Edge[at][i]],val+1);                }            }        }    }}int main(){    int T,id,mr;    int ret,center;    scanf("%d",&T);    while(T--)    {        memset(reach,-1,sizeof(reach));        memset(res,0,sizeof(res));        cur = 0;        scanf("%d%d",&na,&nr);        for(int i=0;i<nz;i++)        {            scanf("%d%d",&id,&mz[id]);            for(int j=0;j<mz[id];j++)            {                scanf("%d",&Edge[id][j]);            }        }        for(int i=0;i<nr;i++)        {            scanf("%d",&mr);            for(int j=0;j<mr;j++)            {                scanf("%d",&id);                BFS(id);                cur++;            }        }        ret=INF;        center=-1;        for(int i=0;i<10000;i++)        {            if(reach[i]=cur-1 && res[i]<ret)            {                ret = res[i];                center=i;            }        }        printf("%d %d\n",ret,center);    }    return 0;}
1 0