QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#851177#5034. >.<Zi_Gao0 0ms0kbC++235.0kb2025-01-10 16:17:112025-01-10 16:17:20

Judging History

你现在查看的是最新测评结果

  • [2025-01-10 16:17:20]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2025-01-10 16:17:11]
  • 提交

answer

#include<bits/stdc++.h>
#define INPUT_TYPE int
#define OUTPUT_TYPE long long

INPUT_TYPE read(){
    register INPUT_TYPE x=0;register char c=getchar(),f=0;
    while(c<'0'||'9'<c) f=(c=='-'),c=getchar();
    while('0'<=c&&c<='9') x=(x*10)+(c&15),c=getchar();
    return f?-x:x;
}
void print(OUTPUT_TYPE x){
    if(x<0) putchar('-'),x=-x;
    if(x>=10) print(x/10),x%=10;
    putchar(x|'0');
    return;
}

#define lc (tree[(p)].lChild)
#define rc (tree[(p)].rChild)

const int PSEG_DATA_SIZE=200010;
const int PSEG_SIZE=PSEG_DATA_SIZE<<5;

struct PSEGTREE_NODE{
    int data;
    int lChild,rChild;
};

struct PSEGTREE{
    PSEGTREE_NODE tree[PSEG_SIZE];
    int root[PSEG_SIZE],tot,totok,L,R;

    int getMem(){
        return tot++;
    }

    int build_(int l,int r){
        int p=getMem();
        if(l==r) tree[p].data=0;
        else{
            int mid=(l+r)>>1;
            tree[p].lChild=build_(l,mid);
            tree[p].rChild=build_(mid+1,r);
        }
        return p;
    }

    int query(int p,int pos,int l,int r){
        if(l==r) return tree[p].data;
        int mid=(l+r)>>1;
        return pos<=mid?query(lc,pos,l,mid):query(rc,pos,mid+1,r);
    }

    int update(int old,int pos,int l,int r,int c){
        int p=getMem();
        tree[p]=tree[old];
        if(l==r) tree[p].data=c;
        else{
            int mid=(l+r)>>1;
            if(pos<=mid) tree[p].lChild=update(tree[p].lChild,pos,l,mid,c);
            else tree[p].rChild=update(tree[p].rChild,pos,mid+1,r,c);
        }
        return p;
    }

    void build(int l,int r){
        root[1]=build_(L=l,R=r);
    }
    int query(int ver,int pos){
        return query(root[ver],pos,L,R);
    }
    void modify(int old,int ver,int pos,int c){
        root[ver]=update(root[old],pos,L,R,c);
        return;
    }
}pseg;

int n;

struct AC{
    std::map<int,int> mp[400010];
    
    int vis[400010],fail[400010],from[400010];
    int cnt=0;
    void insert(int *s,int n,int flg){
        register int i,p=0;
        for(i=0;i<n;++i){
            if(!mp[p][s[i]]){
                mp[p][s[i]]=++cnt;
                from[cnt]=s[i];
            }
            p=mp[p][s[i]];
        }
        vis[p]|=flg;
    }
    void addCur(int u){
        pseg.root[u]=pseg.root[fail[u]];
        for(auto [s,v]:mp[u]){
            pseg.modify(u,u,s,v);
        }
    }
    void build(){
        register int u,w;
        pseg.build(1,n);
        fail[0]=0;
        std::queue<int> Q;
        for(auto [_,v]:mp[0]){
            fail[v]=0;
            Q.push(v);
        }
        addCur(0);
        pseg.totok=pseg.tot;
        while(!Q.empty()){
            u=Q.front(),Q.pop();
            addCur(u);
            vis[u]|=vis[fail[u]];
            for(auto [s,v]:mp[u]){
                fail[v]=pseg.query(fail[u],s);
                Q.push(v);
            }
        }
    }
    int go(int p,int s){return pseg.query(p,s);}
}ac;

int str[400010];

struct NODE{
    int u;
    long long w;
    bool operator < (const NODE &o) const{
        return w>o.w;
    }
};

std::priority_queue<NODE> Q;

long long dis[400010];
char vis[400010];

std::map<int,int> mp[400010],mp0[400010];

void solve(int p,int l,int r,int u,long long d){
    if(!p||pseg.tree[p].data==-1) return;
    if(p<pseg.totok){
        auto it=mp0[u].lower_bound(l);
        while(it!=mp0[u].end()&&it->first<=r){
            if(d+it->second<dis[it->first])
                Q.push({it->first,dis[it->first]=d+it->second});
            it=mp0[u].erase(it);
        }
        return;
    }
    if(l==r){
        // assert(pseg.tree[p].data>0);
        if(mp[u].count(l)&&d+mp[u][l]<dis[pseg.tree[p].data])
            Q.push({pseg.tree[p].data,dis[pseg.tree[p].data]=d+mp[u][l]});
        pseg.tree[p].data=-1;
        return;
    }
    int mid=(l+r)>>1;
    solve(pseg.tree[p].lChild,l,mid,u,d);
    solve(pseg.tree[p].rChild,mid+1,r,u,d);
    pseg.tree[p].data=-1;
    return;
}

int main(){
    // #ifndef ONLINE_JUDGE
    freopen("compete.in","r",stdin);
    freopen("compete.out","w",stdout);
    // #endif

    register int i,j,u,p,pv,v,w;
    register long long now,res=0x3f3f3f3f3f3f3f3fll;
    n=read();
    int m=read();
    int k=read();

    for(i=0;i<m;++i){
        u=read();v=read();w=read();
        mp[u][v]=w;
    }

    for(i=1;i<=n;++i){
        str[0]=i;
        ac.insert(str,1,0);
        mp0[i]=mp[i];
    }

    while(k--){
        w=read();
        for(i=0;i<w;++i) str[i]=read();
        ac.insert(str,w,1);
    }

    ac.build();

    memset(dis,0x3f,sizeof(dis));
    Q.push({ac.go(0,0),dis[ac.go(0,0)]=0});

    while(!Q.empty()){
        u=Q.top().u,Q.pop();
        if(vis[u]||ac.vis[u]) continue;
        vis[u]=1;
        solve(pseg.root[u],1,n,ac.from[u],dis[u]);
    }

    for(i=1;i<=ac.cnt;++i) if((!ac.vis[i])&&ac.from[i]==n) res=std::min(res,dis[i]);

    print(res>=0x3f3f3f3f3f3f3f3fll/2?-1:res);

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Dangerous Syscalls

Test #1:

score: 0
Dangerous Syscalls

input:

35 100 0
34 7 447733879
24 20 187005344
14 34 654502303
2 31 865194349
20 33 9517055
33 15 991889891
24 33 395599993
13 16 237525328
9 5 373850826
30 34 391470240
10 7 650077565
26 10 400825980
34 27 189924713
19 27 907609573
20 10 614945312
10 5 960007605
1 7 984076202
32 25 539699728
24 31 2553027...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Dangerous Syscalls

Test #11:

score: 0
Dangerous Syscalls

input:

50000 200000 1
7542 37166 116613326
3581 43961 629220971
12873 42953 440313807
31744 5286 697832848
25882 12748 106667302
34760 29676 181570340
41570 9240 885513989
22227 35688 63657981
43180 29194 174058940
8977 41899 48262624
7465 18291 600002514
46925 9281 951474878
2115 31162 373758881
5386 3798...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #2:

0%