zoukankan      html  css  js  c++  java
  • NOIP 模拟 $84; m 寻找道路$

    题解 (by;zjvarphi)

    先把所有到 1 节点距离为 0 的节点加到队列里,然后进行 bfs。

    bfs 每次遍历一个点的出边时,先遍历权值为 0 的边,再遍历权值为 1 的边,这样可以保证每次一个点被转移的时候一定是最小距离。

    Code
    #include<bits/stdc++.h>
    #define ri signed
    #define pd(i) ++i
    #define bq(i) --i
    #define func(x) std::function<x>
    namespace IO{
        char buf[1<<21],*p1=buf,*p2=buf;
        #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
        #define debug1(x) std::cerr << #x"=" << x << ' '
        #define debug2(x) std::cerr << #x"=" << x << std::endl
        #define Debug(x) assert(x)
        struct nanfeng_stream{
            template<typename T>inline nanfeng_stream &operator>>(T &x) {
                bool f=false;x=0;char ch=gc();
                while(!isdigit(ch)) f|=ch=='-',ch=gc();
                while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
                return x=f?-x:x,*this;
            }
        }cin;
    }
    using IO::cin;
    namespace nanfeng{
        #define FI FILE *IN
        #define FO FILE *OUT
        template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
        template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
        static const int N=1e6+7,MOD=1e9+7;
        struct edge{int v,nxt,w;}e[N<<1];
        int first[N],t=1,dep[N],dis[N],que[N],st[N],hd=1,tl,n,m;
        bool vis[N];
        auto add=[](int u,int v,int w) {e[t]={v,first[u],w},first[u]=t++;};
        func(void(int)) dfs=[](int x) {
            dep[x]=dis[x]=0;
            vis[x]=true;
            que[++tl]=x;
            for (ri i(first[x]),v;i;i=e[i].nxt) {
                if (vis[v=e[i].v]||e[i].w) continue;
                dfs(v);
            }
        };
        inline int main() {
            FI=freopen("path.in","r",stdin);
            FO=freopen("path.out","w",stdout);
            cin >> n >> m;
            memset(dis+1,-1,sizeof(int)*n);
            for (ri i(1),u,v,w;i<=m;pd(i)) cin >> u >> v >> w,add(u,v,w);
            dfs(1);
            for (ri i(1);i<=n;pd(i)) {
                int cnt=0,lsp=dep[que[hd]],lss=dis[que[hd]];
                while(hd<=tl&&dep[que[hd]]==lsp&&dis[que[hd]]==lss) st[++cnt]=que[hd++]; 
                for (ri j(1);j<=cnt;pd(j)) {
                    int x=st[j];
                    for (ri k(first[x]),v;k;k=e[k].nxt) {
                        if (vis[v=e[k].v]||e[k].w) continue;
                        dep[v]=dep[x]+1;
                        dis[v]=(dis[x]<<1)%MOD;
                        vis[que[++tl]=v]=true;
                    }
                }
                for (ri j(1);j<=cnt;pd(j)) {
                    int x=st[j];
                    for (ri k(first[x]),v;k;k=e[k].nxt) {
                        if (vis[v=e[k].v]||!e[k].w) continue;
                        dep[v]=dep[x]+1;
                        dis[v]=((dis[x]<<1)+1)%MOD;
                        vis[que[++tl]=v]=true;
                    }
                }
            }
            for (ri i(2);i<=n;pd(i)) printf("%d ",dis[i]);
            return 0;
        }
    }
    int main() {return nanfeng::main();}
    
  • 相关阅读:
    Linux安装nginx
    linux目录结构
    Django-admin
    celery
    Flask-Migrate
    Flask-Script
    Flask-SQLAlchemy
    SQLAlchemy
    mongodb的增删改查
    websocket的加密和解密过程
  • 原文地址:https://www.cnblogs.com/nanfeng-blog/p/15473810.html
Copyright © 2011-2022 走看看