2014
01-05

# Bus Pass

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!

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.

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.

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

4 7400

/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//

URL   : http://acm.hdu.edu.cn/showproblem.php?pid=2377
Name  : 2377 Bus Pass

Date  : Tuesday, January 24, 2012
Time Stage : 2 hours

Result:
5290124	2012-01-24 15:02:29	Accepted	2377
375MS	2692K	2336 B
C++	pyy

5289711	2012-01-24 12:12:48	Wrong Answer	2377
375MS	2692K	2292 B
C++	pyy

5289706	2012-01-24 12:07:45	Wrong Answer	2377
390MS	2692K	2282 B
C++	pyy

Test Data :

Review :

//----------------------------------------------------------------------------*/

#include <cstdio>
#include <CSTRING>

#include <queue>

using namespace std ;

#define MEM(a, v)		memset (a, v, sizeof (a))	// a for address, v for value

#define INF		(-1)
#define MAXN	10000

struct EDGE {
int v, w, next ;
} ;

bool	used[MAXN] ;

int		nz, nr, edgeCnt ;
int		first[MAXN], max_d[MAXN], dist[MAXN] ;
EDGE	edge[20*MAXN] ;

void add (const int u, const int v, const int w)
{
edge[edgeCnt].v = v ;
edge[edgeCnt].w = w ;
edge[edgeCnt].next = first[u] ;
first[u] = edgeCnt++ ;
}

void spfa (const int beg)
{
int i, d, u, v ;
queue<int>	q ;

MEM (dist, INF) ;
MEM (used, 0) ;

// 这里不加就WA，考验思维缜密度，搞程序的一定要细心啊！
// 真心不明白为什么要加这里，求解释！
if (max_d[beg] == INF)
max_d[beg] = 1 ;

dist[beg] = 1 ;
used[beg] = 1 ;
q.push (beg) ;

while (!q.empty ())
{
u = q.front () ;
q.pop () ;
used[u] = 0 ;

for (i = first[u] ; i != -1 ; i = edge[i].next)
{
d = dist[u] + edge[i].w ;
v = edge[i].v ;
if (dist[v] == INF || dist[v] > d)
{
dist[v] = d ;
if (max_d[v] == INF || max_d[v] < dist[v])
max_d[v] = dist[v] ;
if (!used[v])
{
q.push (v) ;
used[v] = 1 ;
}
}
}
}
}

int main ()
{
int i, j ;
int u, v, w ;
int tcase, mz, minV, id ;
while (scanf ("%d", &tcase) != EOF)
{
while (tcase--)
{
MEM (first, -1) ;
MEM (max_d, INF) ;
edgeCnt = 0 ;

scanf ("%d%d", &nz, &nr) ;
for (i = 0 ; i < nz ; ++i)
{
scanf ("%d%d", &u, &mz) ;
while (mz--)
{
scanf ("%d", &v) ;
}
}

while (nr--)
{
scanf ("%d", &v) ;
for (i = 0 ; i < v ; ++i)
{
scanf ("%d", &u) ;
spfa (u) ;
}
}
minV = INF ;
id = -1 ;
for (i = 0 ; i < MAXN ; ++i)
if (max_d[i] != INF)
{
if (minV == INF || minV > max_d[i])
{
minV = max_d[i] ;
id = i ;
}
}
printf ("%d %d\n", minV, id) ;
}
}
return 0 ;
}

1. 算法是程序的灵魂，算法分简单和复杂，如果不搞大数据类，程序员了解一下简单点的算法也是可以的，但是会算法的一定要会编程才行，程序员不一定要会算法，利于自己项目需要的可以简单了解。