zoukankan      html  css  js  c++  java
  • P1144 最短路计数

    题目描述

    给出一个N个顶点M条边的无向无权图,顶点编号为1~N。问从顶点1开始,到其他每个点的最短路有几条。

    输入输出格式

    输入格式:

    输入第一行包含2个正整数N,M,为图的顶点数与边数。

    接下来M行,每行两个正整数x, y,表示有一条顶点x连向顶点y的边,请注意可能有自环与重边。

    输出格式:

    输出包括N行,每行一个非负整数,第i行输出从顶点1到顶点i有多少条不同的最短路,由于答案有可能会很大,你只需要输出mod 100003后的结果即可。如果无法到达顶点i则输出0。

    输入输出样例

    输入样例#1: 复制
    5 7
    1 2
    1 3
    2 4
    3 4
    2 3
    4 5
    4 5
    
    输出样例#1: 复制
    1
    1
    1
    2
    4
    

    说明

    1到5的最短路有4条,分别为2条1-2-4-5和2条1-3-4-5(由于4-5的边有2条)。

    对于20%的数据,N ≤ 100;

    对于60%的数据,N ≤ 1000;

    对于100%的数据,N<=1000000,M<=2000000。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define inf 2147483647
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    #define ri register int
    template <class T> inline T min(T a, T b, T c)
    {
        return min(min(a, b), c);
    }
    template <class T> inline T max(T a, T b, T c)
    {
        return max(max(a, b), c);
    }
    template <class T> inline T min(T a, T b, T c, T d)
    {
        return min(min(a, b), min(c, d));
    }
    template <class T> inline T max(T a, T b, T c, T d)
    {
        return max(max(a, b), max(c, d));
    }
    #define scanf1(x) scanf("%d", &x)
    #define scanf2(x, y) scanf("%d%d", &x, &y)
    #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
    #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
    #define pi acos(-1)
    #define me(x, y) memset(x, y, sizeof(x));
    #define For(i, a, b) for (int i = a; i <= b; i++)
    #define FFor(i, a, b) for (int i = a; i >= b; i--)
    #define bug printf("***********
    ");
    #define mp make_pair
    #define pb push_back
    const int N = 1000005;
    const int mod=100003;
    // name*******************************
    struct edge
    {
        int to,next,w;
    } e[N];
    int Head[N];
    int tot=0;
    int dis[N];
    int cnt[N];
    int vis[N];
    queue<int>que;
    int n,m;
    int a,b,c;
    
    // function******************************
    void add(int u,int v,int w)
    {
        e[++tot].to=v;
        e[tot].next=Head[u];
        Head[u]=tot;
        e[tot].w=w;
    }
    void spfa(int x)
    {
        dis[x]=0;
        vis[x]=1;
        que.push(x);
        while(!que.empty())
        {
            int u=que.front();
            que.pop();
            vis[u]=0;
            for(int p=Head[u]; p; p=e[p].next)
            {
                int v=e[p].to;
                int w=e[p].w;
                if(dis[v]>dis[u]+w)
                {
                    dis[v]=dis[u]+w;
                    cnt[v]=cnt[u]%mod;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        que.push(v);
                    }
                }
                else if(dis[v]==dis[u]+w)
                {
                    cnt[v]=(cnt[v]+cnt[u])%mod;
                }
            }
        }
    }
    
    //***************************************
    int main()
    {
    //    ios::sync_with_stdio(0);
    //    cin.tie(0);
        // freopen("test.txt", "r", stdin);
        //  freopen("outout.txt","w",stdout);
        cin>>n>>m;
        me(dis,127);
        For(i,1,m)
        {
            int a,b,c;
            cin>>a>>b;
            add(a,b,1);
            add(b,a,1);
        }
        cnt[1]=1;
        spfa(1);
        For(i,1,n)
        {
            printf("%d
    ",cnt[i]);
        }
    
        return 0;
    }
  • 相关阅读:
    Leetcode 238. Product of Array Except Self
    Leetcode 103. Binary Tree Zigzag Level Order Traversal
    Leetcode 290. Word Pattern
    Leetcode 205. Isomorphic Strings
    Leetcode 107. Binary Tree Level Order Traversal II
    Leetcode 102. Binary Tree Level Order Traversal
    三目运算符
    简单判断案例— 分支结构的应用
    用switch判断月份的练习
    java基本打印练习《我行我素购物系统》
  • 原文地址:https://www.cnblogs.com/planche/p/8724662.html
Copyright © 2011-2022 走看看