这道题卡dij?就卡一个点

P2505 [HAOI2012] 道路

king_storm @ 2019-09-11 19:27:26

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=6000;
const int inf=0x7f7f7f7f;
const int mod=1000000007;

struct node
{
    int to,next,w,id;
}edge[maxn],edge1[maxn];
struct point
{
    int id,w;
    point(int a,int b)
    {
        id=a;
        w=b;
    }
    bool friend operator < (point a,point b)
    {
        return a.w>b.w;
    }
};
priority_queue<point > q;
int k,n,m,top,k1;
int dis[maxn],head[maxn],head1[maxn],vis[maxn],topz[maxn],sta[maxn],topf[maxn],ans[maxn];
void add(int u,int v,int w)
{
    edge[++k].next=head[u];
    edge[k].to=v;
    edge[k].w=w;
    head[u]=k;
}
void add1(int u,int v,int w,int c)
{
    edge1[++k1].next=head1[u];
    edge1[k1].to=v;
    edge1[k1].w=w;
    head1[u]=k1;
    edge1[k1].id=c;
}
void dij(int s)
{
    top=0;
    for(int i=1;i<=n;i++) dis[i]=inf;
    memset(topz,0,sizeof(topz));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    topz[s]=1;
    q.push(point(s,0));
    while(!q.empty())
    {
        int u=q.top().id;
        q.pop();
        if(vis[u]) continue ;
        vis[u]=1;
        sta[++top]=u;
        for(int i=head[u];i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dis[v]==dis[u]+edge[i].w) topz[v]=(topz[u]+topz[v])%mod;
            if(dis[v]>dis[u]+edge[i].w)
            {
                dis[v]=dis[u]+edge[i].w;
                topz[v]=topz[u];
                q.push(point(v,dis[v]));
            }
        }
    }
}
void topu()
{
    memset(topf,0,sizeof(topf));
    while(top)
    {
        topf[sta[top]]++;
        for(int i=head1[sta[top]];i;i=edge1[i].next)
        {
            int v=edge1[i].to;
            if(dis[sta[top]]==dis[v]+edge1[i].w) topf[v]=(topf[v]+topf[sta[top]])%mod,ans[edge1[i].id]=(ans[edge1[i].id]+topz[v]*topf[sta[top]])%mod;
        }
        top--;
    }
}
int main()
{
    int u,v,w;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
        add1(v,u,w,i);
    }
    for(int i=1;i<=n;i++)
    {
        dij(i);
        topu();
    }
    for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
    return 0;
}

by king_storm @ 2019-09-11 19:27:58

就卡第二个点,难受


by TOGEKISS @ 2019-09-11 19:38:27

我的spfa貌似也被卡掉了。。。


by 蒟蒻365 @ 2019-09-11 19:59:37

我还真不知道dij能卡...


by Love_xyh @ 2020-03-04 02:18:41

@king_storm 可能是卡了stl吧...


by Love_xyh @ 2020-03-04 02:19:00

@king_storm 毕竟最短路次数有点多


by Candycar @ 2022-07-28 14:31:53

开个 O_2 就过了?


by Y_J_Y @ 2023-01-31 23:27:28

dijkstra Tle 3个点,Spfa Tle 1个点,我哭死,果然vector常数大


|