牛客网NOIP赛前集训营-提高组(第六场)-A-最长路[拓扑排序+hash+倍增]

题意

给定一个 \(n\)\(m\) 边的边权非负的有向图,边有字符,求以每个点为开头的最长路字典序最小的路径 \(hash\) 值。

\(n,m\leq 10^6\)

分析

  • 首先建反图拓扑排序后入度不为0的点的答案为 \(inf\)

  • \(dep\) 相同时,怎么比较两种转移的优劣?注意到建完反图之后可以通过判定两个转移第一个字典序不同的位置的大小,可以倍增\(+hash\) 实现。

  • 总时间复杂度为 \(O(20*(n+m))\)

代码

#include<bits/stdc++.h>
using namespace std;
#define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].last,v=e[i].to)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
typedef long long LL;
inline int gi(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
    return x*f;
}
template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
const int N=1e6 + 7,mod=998244353;
int n,m,edc,hd=1,tl;
int head[N],ind[N],q[N],dep[N],to[N][21];
LL h[N][21],bin[N];
struct edge{
    int last,to,c;
    edge(){}edge(int last,int to,int c):last(last),to(to),c(c){}
}e[N*2];
void Add(int a,int b,int c){
    e[++edc]=edge(head[a],b,c),head[a]=edc;++ind[b];
}
bool better(int x,int y){
    for(int i=20;~i;--i)
    if(h[x][i]==h[y][i]) x=to[x][i],y=to[y][i];
    return h[x][0]<h[y][0];
}
void topo(){
    rep(i,1,n) if(!ind[i]) q[++tl]=i,dep[i]=1;
    for(;hd<=tl;++hd){
        int u=q[hd];
        rep(i,1,20){
            to[u][i]=to[to[u][i-1]][i-1];
            h[u][i]=(h[u][i-1]+h[to[u][i-1]][i-1]*bin[1<<i-1])%mod;
        }
        go(u){
            if(dep[u]+1>dep[v]) dep[v]=dep[u]+1,to[v][0]=u,h[v][0]=e[i].c;
            else if(dep[u]+1==dep[v]){
                if(!to[v][0]||e[i].c<h[v][0]||(e[i].c==h[v][0]&&better(u,to[v][0])))
                to[v][0]=u,h[v][0]=e[i].c;
            }
            if(--ind[v]==0) q[++tl]=v;
        }
    }
    rep(i,1,n)
    if(ind[i]) puts("Infinity");
    else printf("%lld\n",29*h[i][20]%mod);
}
int main(){
    n=gi(),m=gi();
    for(int i=1,a,b,c;i<=m;++i){
        a=gi(),b=gi(),c=gi();
        Add(b,a,c);
    }
    bin[0]=1;
    rep(i,1,N-1) bin[i]=1ll*bin[i-1]*29%mod;
    topo();
    return 0;
}

还有一种做法,将所有同层的点根据字典序优劣排序后转移到下一层,可以归纳证明。

#include<bits/stdc++.h>
using namespace std;
#define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].last,v=e[i].to)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
typedef long long LL;
inline int gi(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
    return x*f;
}
template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
const int N=1e6 + 7,mod=998244353,inf=0x3f3f3f3f;
typedef pair<int,int> pii;pii f[N];
#define mp make_pair
int n,m,edc,hd=1,tl;
int head[N],ind[N],q[N],dep[N],rk[N],h[N];
struct edge{
    int last,to,c;
    edge(){}edge(int last,int to,int c):last(last),to(to),c(c){}
}e[N*2];
void Add(int a,int b,int c){
    e[++edc]=edge(head[a],b,c),head[a]=edc;++ind[b];
}
vector<int>G[N];
bool cmp(int a,int b){
    return f[a]<f[b];   
}
void topo(){
    rep(i,1,n) if(!ind[i]) q[++tl]=i,dep[i]=1,f[i]=mp(0,0);
    else f[i]=mp(inf,inf);
    for(;hd<=tl;++hd){
        int u=q[hd];
        G[dep[u]].pb(u);
        go(u){
            Max(dep[v],dep[u]+1);
            if(--ind[v]==0) q[++tl]=v;
        }
    }
    rep(j,1,n){
        for(auto u:G[j])
        go(u)if(dep[u]+1==dep[v]){
            if(mp(e[i].c,rk[u])<f[v]){
                f[v]=mp(e[i].c,rk[u]);
                h[v]=(1ll*h[u]*29+e[i].c)%mod;
            }
        }
        sort(G[j+1].begin(),G[j+1].end(),cmp);
        rep(i,0,(int)G[j+1].size()-1) rk[G[j+1][i]]=i;
    }
    rep(i,1,n)
    if(ind[i]) puts("Infinity");
    else printf("%lld\n",1ll*29*h[i]%mod);
}
int main(){
    n=gi(),m=gi();
    for(int i=1,a,b,c;i<=m;++i){
        a=gi(),b=gi(),c=gi();
        Add(b,a,c);
    }
    topo();
    return 0;
}

转载于:https://www.cnblogs.com/yqgAKIOI/p/9828258.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值