zoukankan      html  css  js  c++  java
  • zoj 2913 Bus Pass

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2913

    解题思路:BFS搜索

      1 ///////////////////////////////////////////////////////////////////////////
      2 //problem_id: zoj 2913
      3 //user_id: SCNU20102200088
      4 ///////////////////////////////////////////////////////////////////////////
      5 
      6 #include <algorithm>
      7 #include <iostream>
      8 #include <iterator>
      9 #include <iomanip>
     10 #include <cstring>
     11 #include <cstdlib>
     12 #include <string>
     13 #include <vector>
     14 #include <cstdio>
     15 #include <cctype>
     16 #include <cmath>
     17 #include <queue>
     18 #include <stack>
     19 #include <list>
     20 #include <set>
     21 #include <map>
     22 using namespace std;
     23 
     24 ///////////////////////////////////////////////////////////////////////////
     25 typedef long long LL;
     26 const double PI=acos(-1.0);
     27 
     28 const int x4[]={-1,0,1,0};
     29 const int y4[]={0,1,0,-1};
     30 const int x8[]={-1,-1,0,1,1,1,0,-1};
     31 const int y8[]={0,1,1,1,0,-1,-1,-1};
     32 
     33 typedef int T;
     34 T max(T a,T b){ return a>b? a:b; }
     35 T min(T a,T b){ return a<b? a:b; }
     36 ///////////////////////////////////////////////////////////////////////////
     37 
     38 ///////////////////////////////////////////////////////////////////////////
     39 //Add Code:
     40 const int INF=1000000;
     41 int nz,id[10005],mz[10005],Min[10005],Max[10005],edge[10005][15];
     42 bool flag[10005];
     43 
     44 struct Node{
     45     int id,step;
     46     Node(int i,int s):id(i),step(s){}
     47 };
     48 
     49 void BFS(int i){
     50     int j,k,num;
     51     queue<Node> q;
     52     flag[i]=1,Min[i]=1;
     53     Node node(i,1);
     54     q.push(node);
     55     while(!q.empty()){
     56         Node temp=q.front();
     57         q.pop();
     58         k=temp.id,num=temp.step;
     59         for(j=1;j<=mz[k];j++){
     60             if(!flag[edge[k][j]]){
     61                 flag[edge[k][j]]=1,Min[edge[k][j]]=num+1;
     62                 Node res(edge[k][j],num+1);
     63                 q.push(res);
     64             }
     65         }
     66     }
     67     for(j=1;j<=nz;j++){
     68         if(Max[id[j]]==INF) Max[id[j]]=Min[id[j]];
     69         else Max[id[j]]=max(Max[id[j]],Min[id[j]]);
     70     }
     71 }
     72 ///////////////////////////////////////////////////////////////////////////
     73 
     74 int main(){
     75     ///////////////////////////////////////////////////////////////////////
     76     //Add code:
     77     int T,nr,mr,i,j,t,k;
     78     scanf("%d",&T);
     79     while(T--){
     80         scanf("%d%d",&nz,&nr);
     81         for(i=1;i<=nz;i++){
     82             scanf("%d",&id[i]);
     83             scanf("%d",&mz[id[i]]);
     84             for(j=1;j<=mz[id[i]];j++) scanf("%d",&edge[id[i]][j]);
     85             Max[id[i]]=INF;
     86         }
     87         for(i=1;i<=nr;i++){
     88             scanf("%d",&mr);
     89             for(j=1;j<=mr;j++){
     90                 scanf("%d",&k);
     91                 for(t=1;t<=nz;t++){
     92                     flag[id[t]]=0;
     93                     Min[id[t]]=0;
     94                 }
     95                 BFS(k);
     96             }
     97         }
     98         int MIN=INF+1,center;
     99         for(i=1;i<=nz;i++){
    100             if(Max[id[i]]<MIN || (Max[id[i]]==MIN && id[i]<center)){
    101                 MIN=Max[id[i]];
    102                 center=id[i];
    103             }
    104         }
    105         printf("%d %d
    ",MIN,center);
    106     }
    107     ///////////////////////////////////////////////////////////////////////
    108     return 0;
    109 }
    110 
    111 ///////////////////////////////////////////////////////////////////////////
    112 /*
    113 Testcase:
    114 Input:
    115 1
    116 17 2
    117 7400 6 7401 7402 7403 7404 7405 7406
    118 7401 6 7412 7402 7400 7406 7410 7411
    119 7402 5 7412 7403 7400 7401 7411
    120 7403 6 7413 7414 7404 7400 7402 7412
    121 7404 5 7403 7414 7415 7405 7400
    122 7405 6 7404 7415 7407 7408 7406 7400
    123 7406 7 7400 7405 7407 7408 7409 7410 7401
    124 7407 4 7408 7406 7405 7415
    125 7408 4 7409 7406 7405 7407
    126 7409 3 7410 7406 7408
    127 7410 4 7411 7401 7406 7409
    128 7411 5 7416 7412 7402 7401 7410
    129 7412 6 7416 7411 7401 7402 7403 7413
    130 7413 3 7412 7403 7414
    131 7414 3 7413 7403 7404
    132 7415 3 7404 7405 7407
    133 7416 2 7411 7412
    134 5 7409 7408 7407 7405 7415
    135 6 7415 7404 7414 7413 7412 7416
    136 Output:
    137 4 7400
    138 */
    139 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    让xamarin的Entry绑定时,支持Nullable类型
    xamarin.forms 绑定页面里指定元素的某个属性值
    俄文环境下,字符串转数字失败
    使用devstack安装openstack
    在linux中访问macos 下的分区。
    git 多用户多仓库配置
    Macos上 ProxyChains 的坑, 需要关闭 sip
    MacOS 的预览 Preview 打开pdf 容易卡死 解决方案
    一客户端使用多个密钥对登录多个主机的解决方案.
    MacOS 10.13.6 下装xcode 流程
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3273335.html
Copyright © 2011-2022 走看看