zoukankan      html  css  js  c++  java
  • Bus Pass

    ZOJ Problem Set - 2913
    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

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <string>
      7 #include <vector>
      8 #include <stack>
      9 #include <queue>
     10 #include <set>
     11 #include <map>
     12 #include <iomanip>
     13 using namespace std;
     14 const int INF=0x5fffffff;
     15 const int MS=10005;
     16 const double EXP=1e-8;
     17 
     18 int nz,nr;
     19 int num[MS];
     20 int edge[MS][11];
     21 int res[MS];
     22 int cur;
     23 int reach[MS];//   防止在同一站重复访问。每一站对应一个cur,标记作用
     24 int vis[MS];
     25 void  bfs(int s)
     26 {
     27     int i,a,b;
     28     int val,at;
     29     queue<int> que[2];   //一层一层访问,可以用滚动队列
     30     a=0;b=1;val=1;
     31     if(reach[s]<cur)
     32     {
     33         que[b].push(s);
     34         reach[s]=cur;
     35         res[s]=max(res[s],val);
     36     }
     37     while(!que[b].empty())
     38     {
     39         swap(a,b);
     40         val++;    //层次加1
     41         while(!que[a].empty())
     42         {
     43             at=que[a].front();
     44             que[a].pop();
     45             for(i=0;i<num[at];i++)
     46             {
     47                 if(reach[edge[at][i]]<cur)
     48                 {
     49                     reach[edge[at][i]]=cur;
     50                     que[b].push(edge[at][i]);
     51                     res[edge[at][i]]=max(res[edge[at][i]],val);
     52                 }
     53             }
     54         }
     55     }
     56 }
     57 
     58 int main()
     59 {
     60     int T;
     61     int i,j;
     62     int id;
     63     int mr;
     64     int ret,center;
     65     scanf("%d",&T);
     66     while(T--)
     67     {
     68         memset(reach,0,sizeof(reach));
     69         memset(res,0,sizeof(res));
     70         memset(vis,0,sizeof(vis));
     71         cur=1;
     72         scanf("%d %d",&nz,&nr);
     73         for(i=0;i<nz;i++)
     74         {
     75             scanf("%d",&id);
     76             scanf("%d",&num[id]);
     77             for(j=0;j<num[id];j++)
     78             {
     79                 scanf("%d",&edge[id][j]);
     80             }
     81         }
     82         for(i=0;i<nr;i++)
     83         {
     84             scanf("%d",&mr);
     85             for(j=0;j<mr;j++)
     86             {
     87                 scanf("%d",&id);
     88                 if(vis[id]==0)   //可能多条线路中有公共站点
     89                 {
     90                     vis[id]=1;
     91                     bfs(id);
     92                     cur++;
     93                 }
     94             }
     95         }
     96         ret=INF;center=-1;
     97         for(i=0;i<10000;i++)
     98         {
     99             if(reach[i]==cur-1&&res[i]<ret)
    100             {
    101                 ret=res[i];
    102                 center=i;
    103             }
    104         }
    105         printf("%d %d
    ",ret,center);
    106     }
    107     return 0;
    108 }
  • 相关阅读:
    一文读懂高性能网络编程中的I/O模型
    QQ的成功,远没有你想象的那么顺利和轻松
    解密“达达-京东到家”的订单即时派发技术原理和实践
    以网游服务端的网络接入层设计为例,理解实时通信的技术挑战
    老罗最新发布了“子弹短信”这款IM,主打熟人社交能否对标微信?
    最火移动端跨平台方案盘点:React Native、weex、Flutter
    ogre3D学习基础11 -- 交换两个场景管理器
    STL学习笔记2--list
    STL学习笔记1--vector
    设计模式 --- 学习总结
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4302308.html
Copyright © 2011-2022 走看看