zoukankan      html  css  js  c++  java
  • P2296 寻找道路

    P2296 寻找道路
    路径上的所有点的出边所指向的点都直接或间接与终点连通。
    题中的这个条件很难受,操作是,反向建边,然后从终点bfs,然后把能走到的点设成true。然后把false的点按反向边扩展一层,把扩展到的点卡掉。然后正向bfs就好了,小心“图G 中可能存在重边和自环,题目保证终点没有出边。”;
    我3个点一直WA,在正向bfs的时候,deep[p->n]=min(deep[p->n],deep[tt]+1);

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<ctime>
    #include<algorithm>
    #include<queue>
    #define inf 2147483647
    #define For(i,a,b) for(register int i=a;i<=b;i++)
    #define p(a) putchar(a)
    #define g() getchar()
    
    using namespace std;
    int n,m,x,y,s,t;
    bool b2[10010];
    bool b1[10010];
    int deep[10010];
    bool vis[10010];
    queue<int>q;
    struct node
    {
        int n;
        node *next;
    }*e1[200010],*e2[200010];
    void in(int &x)
    {
        int y=1;
        char c=g();x=0;
        while(c<'0'||c>'9')
        {
            if(c=='-')
            y=-1;
            c=g();
        }
        while(c<='9'&&c>='0')x=x*10+c-'0',c=getchar();
        x*=y;
    }
    void o(int x)
    {
        if(x<0)
        {
            p('-');
            x=-x;
        }
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void push(int y,node *&temp)
    {
        node *p;
        p=new node();
        p->n=y;
        if(temp==NULL)
        temp=p;
        else
        {
            p->next=temp->next;
            temp->next=p;
        }
    }
    
    void bfs(int x)
    {
        q.push(x);
        node *p;
        int tt;
        while(q.size()>0)
        {
            tt=q.front();
            p=e2[tt];
            b2[tt]=true;
            while(p!=NULL)
            {
                if(!b2[p->n]&&p->n!=tt)
                {
                    q.push(p->n);
                }
                p=p->next;
            }
            q.pop();
        }
    }
    
    void bfs1(int x)
    {
        if(!b1[x])
        {
        q.push(x);
        deep[x]=0;    
        }
        node *p;
        int tt;
        while(q.size()>0)
        {
            tt=q.front();
            p=e1[tt];
            vis[tt]=true;
            while(p!=NULL)
            {
                
                if(!b1[p->n]&&!vis[p->n]&&p->n!=tt)
                {
                    deep[p->n]=min(deep[p->n],deep[tt]+1);
                    if(p->n==t)
                    {
                        o(deep[t]);
                        exit(0);
                    }
                    q.push(p->n);
                }
                p=p->next;
            }
            q.pop();
        }
    }
    
    int main()
    {
    
        in(n),in(m);
        For(i,1,n)
        deep[i]=inf;
        For(i,1,m)
        {
            in(x),in(y);
            push(y,e1[x]);
            push(x,e2[y]);
        }
        in(s),in(t);
        bfs(t);
        For(j,1,n)
        {
            if(!b2[j])
            {
                b1[j]=true;
                for(node *i=e2[j];i!=NULL;i=i->next)
            {
                b1[i->n]=true;
            }
            }
        }
        bfs1(s);
        o(-1);
        return 0;
    }
  • 相关阅读:
    普通函数跟箭头函数中this的指向问题
    vue之router学习笔记
    vue之登录和token处理
    vue之router钩子函数
    eslint----standard 代码规范
    vscode----配置vue开发环境
    vue----安装教程
    vue----全局组件,局部组件
    vue----常用实例方法--$mount(),$destroy(),$watch(),$forceUpdate()
    vue----生命周期
  • 原文地址:https://www.cnblogs.com/war1111/p/7659309.html
Copyright © 2011-2022 走看看