zoukankan      html  css  js  c++  java
  • sicily 9360 vector

    9360. HIPERCIJEVI
     
     
    Total: 104 Accepted: 21 Rating:
    Unrated
     
     
         
         
     
    Time Limit: 4sec    Memory Limit:256MB
    Description

    In a galaxy far, far away, the fastest method of transportation is using hypertubes. Each hypertube 

    directly connects K stations with each other. What is the minimum number of stations that we need to 

    pass through in order to get from station 1 to station N? 

    Input

    The first line of input contains three positive integers: N (1 ≤ ≤ 100 000), the number of stations, K 

    (1 ≤ ≤ 1 000), the number of stations that any single hypertube directly interconnects, and M (1 ≤  ≤ 1 000), the number of hypertubes. 

    Each of the following M lines contains the description of a single hypertube: K positive integers, the 

    labels of stations connected to that hypertube. 

    Output

    The first and only line of output must contain the required minimum number of stations. If it isn't 

    possible to travel from station 1 to station N, output -1. 

    Sample Input
    Copy sample input to clipboard
    input 1:
    9 3 5 
    1 2 3 
    1 4 5 
    3 6 7 
    5 6 7 
    6 8 9
    
    input 2:
    15 8 4 
    11 12 8 14 13 6 10 7 
    1 5 8 12 13 6 2 4 
    10 15 4 5 9 8 14 12 
    11 12 14 3 5 6 1 13 
    Sample Output
    output 1:
    4
    
    output 2:
    3
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include<cstdio>
    using namespace std;
    #define maxn 100005
    vector <int>p[maxn+1000];
    int dis[maxn+1000];
    queue <int>q;
    int n,k,m;
    int main()
    {
        int i,j;
        while(cin>>n>>k>>m)
        {
            for(i=1; i<=n+m; i++)
                p[i].clear();
            for(i=1; i<=m; i++)
                for(j=1; j<=k; j++)
                {
                    int a;
                    scanf("%d",&a);
                    p[i+n].push_back(a);
                    p[a].push_back(i+n);
                }
            memset(dis,-1,sizeof(dis));
            dis[1]=0;
            while(!q.empty())
            q.pop();
            q.push(1);
            int ans=-1;
            while(!q.empty())
            {
                int tmp=q.front();
                q.pop();
                for(i=0;i<p[tmp].size();i++)
                    if(dis[p[tmp][i]]==-1)
                {
                    dis[p[tmp][i]]=dis[tmp]+1;
                    if(p[tmp][i]==n)
                        ans=dis[p[tmp][i]]/2+1;
                    q.push(p[tmp][i]);
                }
                if(ans!=-1)
                    break;
            }
            cout<<ans<<endl;
        }
    }    
    View Code
  • 相关阅读:
    正则化方法:L1和L2 regularization、数据集扩增、dropout
    xgboost原理及应用
    机器学习系列------1. GBDT算法的原理
    c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
    STL中的set容器的一点总结
    2016-12-17 新浪博客服务器挂掉了,所有博客页面都无法打开
    Centos 6.5 下php5.6.2 的编译安装
    Docker的基本组成
    Docker简介
    基于Dubbo框架构建分布式服务(集群容错&负载均衡)
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3432198.html
Copyright © 2011-2022 走看看