WA60分求查错

P2505 [HAOI2012] 道路

torque @ 2019-10-23 10:25:26

被卡了一上午

#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define N 2001
#define int long long
#define MOD 1000000007
#define rnt register int
using namespace std;
struct node{
    int to,len,nxt;
    node(int X,int Y,int Z):to(X),len(Y),nxt(Z){}
    node(){}
} e[N*3];
bool vis[N*3],in[N*3];
int n,m,x,y,z,cnt,ind[N],ans[N],dis[N],cnt0[N],cnt1[N],head[N];
inline void ins(int a,int b,int c){
    e[++cnt]=node(b,c,head[a]);
    head[a]=cnt;
}
inline void SPFA(int S){
    queue <int> Q;
    memset(ind,0,sizeof(ind));
    memset(in,false,sizeof(in));
    memset(dis,0x3f,sizeof(dis));
    memset(vis,false,sizeof(vis));
    dis[S]=0;vis[S]=true;Q.push(S);
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();vis[u]=false;
        for(rnt i=head[u];i;i=e[i].nxt){
            int v=e[i].to;
            if(dis[v]>dis[u]+e[i].len){
                dis[v]=dis[u]+e[i].len;
                if(!vis[v]) vis[v]=true,Q.push(v);
            }
        }
    }
    for(rnt i=1;i<=n;i=-~i) for(rnt j=head[i];j;j=e[j].nxt)
        if(dis[i]+e[j].len==dis[e[j].to]) in[j]=true,++ind[e[j].to];
}
inline void topu(int S){
    queue <int> Q;
    stack <int> QQ;
    memset(cnt0,0,sizeof(cnt0));
    memset(cnt1,0,sizeof(cnt1));
    cnt0[S]=1;Q.push(S);
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();QQ.push(u);
        for(rnt i=head[u];i;i=e[i].nxt) if(in[i]){
            int v=e[i].to;
            cnt0[v]=(cnt0[v]+cnt0[u])%MOD;
            if(--ind[v]==0) Q.push(v);
        }
    }
    while(!QQ.empty()){
        int u=QQ.top();
        QQ.pop();cnt1[u]=1;
        for(rnt i=head[u];i;i=e[i].nxt) if(in[i]){
            int v=e[i].to;
            cnt1[u]=(cnt1[u]+cnt1[v])%MOD;
        }
    }
    for(rnt i=1;i<=n;i=-~i) for(rnt j=head[i];j;j=e[j].nxt) if(in[j])
        ans[j]=(ans[j]+cnt0[i]*cnt1[e[j].to]%MOD)%MOD;
}
signed main(){
    scanf("%lld%lld",&n,&m);
    for(rnt i=1;i<=m;i=-~i){
        scanf("%lld%lld%lld",&x,&y,&z);
        ins(x,y,z);//ins(y,x,z);
    }
    for(rnt i=1;i<=n;i=-~i){
        SPFA(i);
        topu(i);
    }
    for(rnt i=1;i<=m;i=-~i) printf("%lld\n",ans[i]);
    return 0;
}

by drinkyum @ 2023-07-27 01:16:27

ans开太小


by rechenz @ 2023-10-31 21:19:09

@drinkyum 路人相同错误,感谢


|