zoukankan      html  css  js  c++  java
  • zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂

    2130: hipercijevi

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 595  Solved: 112

    SubmitStatusWeb Board

    Description

    在遥远的星系, 最快的交通方式是用某种管道。 每个管道直接互相连接N个站。 那么我们从第一个站到第N个站最少要经过多少个站呢?

    Input

    输入文件的第一行为T表示有T组数据

    每个数据第一行包含三个正整数 N (1<=N<=100000) 表示站的个数; K (1<=K<=1000) 表示一个管道直接连接了多少个站; M (1<=M<=1000) 表示管道的数量。

    接下来的M行, 每行包含一个管道的描述: K个正整数, 表示这个管道连接的K个站的编号。

    Output

    输出文件T行,每行包含一个正整数,表示从第一个站到第N个站最少需要经过多少个站。 如果无法从第一个站到达第N个站,输出-1 。

    Sample Input

    2
    9 3 5
    1 2 3
    1 4 5
    3 6 7
    5 6 7
    6 8 9
    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

    4
    3


    思路:
    比赛时候按照bfs写的,但是这道题目太卡时间。也没有想起用输入输出外挂...
    下来想用spfa做,写到一半发现,还不如直接bfs来的直接,而且速度更快
    所以,就是链式前向星+BFS+输入输出外挂,可惜冲不进700ms之内,运气有点差
    题意有点指向不明,官方题解释每条管道为点,向管道上所有的点进行连接,建边
    代码:
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=200005;
    const int maxm=2000005;
    struct edgenode {
        int to,w,next;
    }edges[maxm];
    struct node {
        int id,step;
    };
    bool vis[maxn];
    int head[maxn],data[1005];
    int cnt,n;
    void out(int a) {
        if(a<0) {putchar('-');a=-a;}
        if(a>=10)out(a/10);
        putchar(a%10+'0');
    }
    int in() {
        int flag=1;
        char ch;
        int a=0;
        while((ch=getchar())==' '||ch=='
    ');
        if(ch=='-') flag=-1;
        else a+=ch-'0';
        while((ch=getchar())!=' '&&ch!='
    ') {
            a*=10;a+=ch-'0';
        }
        return flag*a;
    }
    void addedge(int u, int v, int w) {
        edges[cnt].to=v;
        edges[cnt].w=w;
        edges[cnt].next=head[u];
        head[u]=cnt++;
    }
    int bfs() {
        queue<node> q;
        node now,last;
        now.id=1;now.step=0;
        vis[1]=true;
        q.push(now);
        while(!q.empty()) {
            now=q.front();q.pop();
            for(int temp=head[now.id];temp!=-1;temp=edges[temp].next) {
                if(vis[edges[temp].to]) continue;
                vis[edges[temp].to]=true;
                last.id=edges[temp].to;
                last.step=now.step+1;
                q.push(last);
                if(last.id==n) return last.step/2+1;
            }
        }
        return -1;
    }
    int main() {
        int t,m,k;
        t=in();
        while(t--) {
            n=in();k=in();m=in();
            cnt=0;
            memset(vis,false,sizeof(vis));
            for(int i=0;i<maxn;++i) head[i]=-1;
            for(int i=0;i<maxm;++i) edges[i].next=-1;
            for(int i=0;i<m;++i) {
                for(int j=0;j<k;++j) {
                    data[j]=in();
                    addedge(data[j],n+i+1,1);
                    addedge(n+i+1,data[j],1);
                }
            }
            out(bfs());printf("
    ");
        }
        return 0;
    }
    



  • 相关阅读:
    拓扑排序
    Frame Stacking 框架堆叠
    第二课 欧几里德算法与扩展欧几里德算法
    欧拉回路
    第一课 快速幂取模
    cookie使用汇总 c设置ookie的生命周期
    .net ArrayList的用法简介
    关于C#的partial修饰符
    sql server修改表结构的sql语句
    Web MVC模式中的基本表单提交
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7775979.html
Copyright © 2011-2022 走看看