zoukankan      html  css  js  c++  java
  • 【洛谷P1144】最短路计数

    最短路计数

    题目链接

    然而是一道搜索。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 int n,m,sum,head,tail,Head[1000010],t[1000010],f[1000010],que[1000010];  //f记录最短路径数,t记录最短路长度
     7 const int RQY = 100003;
     8 struct NODE{        //邻接表
     9     int next;
    10     int to;
    11 } e[2000020];
    12 inline void add(int x,int y)
    13 {
    14     e[++sum].to=y;
    15     e[sum].next=Head[x];
    16     Head[x]=sum;
    17 }
    18 int main()
    19 {
    20     scanf("%d%d",&n,&m);
    21     int x,y;
    22     for(int i=1;i<=m;i++)
    23     {
    24         scanf("%d%d",&x,&y);
    25         add(x,y);
    26         add(y,x);
    27     }
    28     memset(t,-1,sizeof(t));
    29     que[++tail]=1;
    30     f[1]=1;
    31     t[1]=0;
    32     while(head<=tail)    //bfs
    33     {
    34         int u=que[++head];
    35         for(int i=Head[u];i;i=e[i].next)
    36         {
    37             int v=e[i].to;
    38             if(t[v]==-1)
    39             {
    40                 que[++tail]=v;
    41                 t[v]=t[u]+1;
    42                 f[v]=f[u]%RQY;
    43             }
    44             else if(t[v]==t[u]+1) f[v]=(f[v]+f[u])%RQY;
    45         }
    46     }
    47     for(int i=1;i<=n;i++)
    48      printf("%d
    ",f[i]);
    49     return 0;
    50 }

    一定要%RQY

  • 相关阅读:
    Pipe
    An Easy Problem?!
    Kadj Squares
    Space Ant
    Intersection
    让网页变为可编辑状态
    typescript入门基础
    大家都能看懂的 canvas基础教程
    数组的foreach方法和jQuery中的each方法
    html单行、多行文本溢出隐藏
  • 原文地址:https://www.cnblogs.com/yjkhhh/p/8858367.html
Copyright © 2011-2022 走看看