Zoj2913bus Pass (Bfs+set)

Source: Internet
Author: User

Bus Pass Time limit: 5 Seconds Memory Limit: 32768 KB

You travel a lot by bus and the costs of the seperate tickets is starting to add up.

Therefore you want to see if it might is advantageous for your to buy a bus pass.

The bus system works in your country (and also in the Netherlands) are as follows:

When you buy a bus pass, you have to indicate a center zone and a star value. You were allowed to travel freely in any zone which have a distance to your center zone which was less than your star value. For example, if you had a star value of one, you can only travel in your center zone. If you have a star value of both, you can also travel in all adjacent zones, et cetera.

You had a list of all buses trips you frequently make, and would like to determine the minimum star value you need to make All these trips using your Buss pass. It's not always a easy task. For example look at the following figure:

Here's want to is able to travel from A to B and from B to D. The best center zone is 7400, for which you need a star value of 4. Note that does not even visit this zone on your trips!

Input

On the first line an integert(1 <=t<=): The number of test cases. Then to each test case:

One line with integersnz(2 <=NZ<= 9 999) andnr(1 <=nr<=): The Numbe R of Zones and the number of the bus trips, respectively.

NZ lines starting with integers idi (1 <= IDI <= 9 999) and Mzi (1 <= Mzi <=), a number identifying the i-th zone and the number of zones adjacent to it, followed by Mzi integers:the Numbe Rs of the adjacent zones.

nr lines starting with one integer MRI (1 <= MRI <=), indicating the number of Z Ones the ith bus trip visits, followed by MRI integers:the numbers of the zones through which the bus passes I n the order in which they is visited.

All zones is connected, either directly or via other zones.

Output

For each test case:

One line with both integers, the minimum star value and the ID of a center zone which achieves this minimum star value. If There is 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

Reference: http://blog.csdn.net/acvay/article/details/40221819

Test instructions: From the bus line (NR) to find a center, the center to each bus station is the best distance (this center takes into account each bus station). And find out the farthest distance from the center to one of the bus stops.

Idea: to each bus station to the BFS to find out dis[i],i this point to these bus stations the farthest distance.

Harvest: 1. Learn about set: Each element appears at most once, and the default is from small to large.

2.set<int>:: Iterator takes the number out of the set.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <queue>6#include <Set>7 using namespacestd;8 #defineMAXN 100009typedefSet<int>:: iterator it;Ten Set<int>Zone; One intlink[maxn][ A]; A intDIS[MAXN],TDIS[MAXN]; - voidBFsintSt) - { thequeue<int>Q; -memset (TDIs,0,sizeof(TDIs)); -tdis[st]=1; - Q.push (ST); +      while(!q.empty ()) -     { +         intCur=Q.front (); A Q.pop (); at         if(tdis[cur]>Dis[cur]) -dis[cur]=Tdis[cur]; -          for(intI=1; i<=link[cur][0];i++) -         { -             inttemp=Link[cur][i]; -             if(tdis[temp]==0) in             { - Q.push (temp); totdis[temp]=tdis[cur]+1; +             } -         } the     } *  $ }Panax Notoginseng intMain () - { the     intT; +scanf"%d",&t); A      while(t--) the     { + zone.clear (); -         intNz,nr,mz,mr,num,id; $scanf"%d%d",&nz,&nr); $          for(intI=0; i<nz;i++) -         { -scanf"%d%d",&id,&mz); thelink[id][0]=mz; -              for(intj=1; j<=mz;j++)Wuyi             { thescanf"%d",&link[id][j]); - Zone.insert (Link[id][j]); Wu             } -         } About         intPos; $memset (DIS,0,sizeof(TDIs)); -          for(intI=0; i<nr;i++) -         { -scanf"%d",&MR); A              for(intj=0; j<mr;j++) +                 { thescanf"%d",&POS); - BFS (POS); $                 } the         } theIt i=Zone.begin (); the         intans= *i; the          for(++i;i!=zone.end (); + +i) -             if(dis[*i]<Dis[ans]) inans=*i; theprintf"%d%d\n", Dis[ans],ans); the     } About     return 0; the}

Zoj2913bus Pass (Bfs+set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.