zoj2913 Bus Pass

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.

 

    题目是要求一个城市中心,使得公交路线中经过的所有城市到该中心的最小距离的最大距离最小。对于路线中经过的每一个城市我们都可以通过求一次BFS,得到该城市到其它城市的最短距离。假设路线中经过了m个城市,这样的话我们就得到了其它的每个城市(ci)分别到这m个城市的最短距离,取这m个最短距离的最大值,即为以ci为中心的“阀值”,然后取所有城市中的最小“阀值”即为所求。

#include <iostream>
#include<cstdio>
#include<queue>
#include<vector>

using namespace std;
const int N=10000;
struct st
{
    int id,step;
}w,v;
vector<int>map[N];
bool visit[N];
int dist[N];

void bfs(int s)
{
    queue<st>q;
    w.id=s;
    w.step=1;
    q.push(w);
    visit[s]=true;
    while(!q.empty())
    {
        w=q.front();
        q.pop();
        if(w.step>dist[w.id])
            dist[w.id]=w.step;
        for(int i=0;i<map[w.id].size();i++)
        {
            int j=map[w.id][i];
            if(!visit[j])
            {
                visit[j]=true;
                v.id=j;
                v.step=w.step+1;
                q.push(v);
            }
        }
    }
}

int main()
{
    int i,j,k,t,nz,nr,id,mz,mr;
    int city[N];
    int route[N],cnt;//经过的城市
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&nz,&nr);
        for(i=1;i<N;i++)
            map[i].clear();
        for(i=1;i<=nz;i++)
        {
            scanf("%d%d",&id,&mz);
            city[i]=id;//记录出现的城市的编号
            for(k=0;k<mz;k++)
            {
                int to;
                scanf("%d",&to);
                //建边
                map[id].push_back(to);
                map[to].push_back(id);
            }
        }
        cnt=1;
        for(i=1;i<=nr;i++)
        {
            scanf("%d",&mr);
            for(j=0;j<mr;j++)
            {
                int c;
                scanf("%d",&c);
                for(k=1;k<cnt;k++)//去重
                    if(c==route[k])
                        break;
                if(k==cnt)
                    route[cnt++]=c;
            }
        }
        for(i=1;i<=nz;i++)
            dist[city[i]]=0;
        for(i=1;i<cnt;i++)
        {
            for(j=1;j<=nz;j++)
                visit[city[j]]=false;
            bfs(route[i]);
        }
        dist[0]=10000000;
        int pos=0;
        for(i=1;i<=nz;i++)
            if(dist[city[i]]<dist[pos])
                pos=city[i];
            else if(dist[city[i]]==dist[pos]&&city[i]<pos)
                pos=city[i];
        printf("%d %d\n",dist[pos],pos);
    }
    return 0;
}



 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值