Bus Pass
Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 667 accepted submission (s): 271
Problem descriptionyou 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 Cees.
You have a list of all bus trips you frequently make, and wowould 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!
Inputon 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.
Outputfor 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 input117 27400 6 7401 7402 7403 7404 7405 74067401 7412 7402 7400 7406 7410 74117402 7412 7403 7400 7401 74117403 7413 7414 7404 7400 7402 74127404 7403 7414 7415 7405 74007405 7404 7415 7407 7408 7406 74007406 7400 7405 7407 7408 7409 7410 74017407 4 7408 7406 7405 74157408 7409 7406 7405 74077409 7410 3 7406 74087410 7411 7401 7406 74097411 7416 7412 7402 7401 74107412 7416 7411 7401 7402 7403 74137413 3 7412 7403 3 74147414 7413 7403 3 74047415 7404 7405 2 74077416 7411 74125 7409 7408 7407 7405 74156 7415 7404 7414 7413 7412 7416
Sample output4 7400
# Include <iostream> # include <stack> # include <cstring> # include <cstdio> # include <string> # include <algorithm> # include <queue> using namespace STD; /* the maximum distance between a certain point and all bus stations is the minimum. Then, you can first find the distance between each bus station and all points, and obtain the maximum distance between each point and the bus station, leave at least all points. */# Define MS (ARR, Val) memset (ARR, Val, sizeof (ARR) # define n 10000 # define INF 0x3fffffffstruct node {int seq; int lay ;}; int ID [N] [11]; int idtag [N]; int idmax [N]; int iddis [N]; queue <int> q; void BFS (int p) // calculate BFS for the minimum short circuit of the unit, because the distance between two adjacent points is 1. {MS (iddis, 0); MS (idtag, 0); q. push (p); idtag [p] = iddis [p] = 1; while (! Q. empty () {int S = Q. front (); q. pop (); int I = 0; while (ID [s] [I]) {If (! Idtag [ID [s] [I]) {iddis [ID [s] [I] = iddis [s] + 1; idtag [ID [s] [I] = 1; q. push (ID [s] [I]) ;} I ++ ;}} int main () {int T, NZ, NR, mzn, IDP, MRN, TT, ANS, Pos; CIN> T; while (t --) {MS (ID, 0); MS (idmax, 0); CIN> NZ> NR; for (INT I = 0; I <NZ; I ++) {CIN> IDP> mzn; For (Int J = 0; j <mzn; j ++) {CIN> ID [IDP] [J] ;}for (INT I = 0; I <NR; I ++) {CIN> MRN; for (Int J = 0; j <MRN; j ++) {CIN> tt; BFS (TT); For (INT I = 0; I <N; I ++) {idmax [I] = max (idmax [I], iddis [I]) ;}} Pos = 0; ans = inf; for (INT I = 0; I <n; I ++) {If (idmax [I]> 0 & Ans> idmax [I]) {pos = I; ans = idmax [I] ;}}cout <ans <<<'' <POS <Endl;} return 0 ;}