P1144 最短路计数 题解

文章目录

    • 题目描述
    • 输入格式
    • 输出格式
    • 样例
      • 样例输入
      • 样例输出
    • 数据范围与提示
    • 完整代码

题目描述

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

输入格式

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

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

输出格式

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

样例

样例输入

5 7
1 2
1 3
2 4
3 4
2 3
4 5
4 5

样例输出

1
1
1
2
4

数据范围与提示

样例说明: 1 1 1 5 5 5 的最短路有 4 4 4 条,分别为 2 2 2 1 → 2 → 4 → 5 1\to 2\to 4\to 5 1245 2 2 2 1 → 3 → 4 → 5 1\to 3\to 4\to 5 1345(由于 4 → 5 4\to 5 45 的边有 2 2 2 条)。

对于 20 % 20\% 20% 的数据, 1 ≤ N ≤ 100 1\le N \le 100 1N100
对于 60 % 60\% 60% 的数据, 1 ≤ N ≤ 1 0 3 1\le N \le 10^3 1N103
对于 100 % 100\% 100% 的数据, 1 ≤ N ≤ 1 0 6 1\le N\le10^6 1N106 1 ≤ M ≤ 2 × 1 0 6 1\le M\le 2\times 10^6 1M2×106

题目传送门

完整代码

#include <bits/stdc++.h>
using namespace std;
inline int read() {
    register int x = 1, ans = 0;
    register char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            x = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        ans = (ans << 3) + (ans << 1) + ch - 48;
        ch = getchar();
    }
    return x * ans;
}
int n, m, c = 0;
bool vis[1000002];
int h[4000002], dist[1000002], cnt[1000002];
struct node {
    int to, nxt;
} e[4000002];
void add(int u, int v) {
    e[++c].to = v;
    e[c].nxt = h[u];
    h[u] = c;
}
struct cmp {
    bool operator()(int a, int b) { return dist[a] > dist[b]; }
};
priority_queue<pair<int, int> > q;
int main() {
    n = read(), m = read();
    for (int i = 1, a, b; i <= m; i++) {
        a = read(), b = read();
        add(a, b), add(b, a);
    }
    memset(dist, 0x3f, sizeof(dist));
    dist[1] = 0, cnt[1] = 1;
    q.push(make_pair(0, 1));
    while (q.size()) {
        int u = q.top().second;
        q.pop();
        if (vis[u])
            continue;
        vis[u] = 1;
        for (int i = h[u]; i; i = e[i].nxt) {
            int v = e[i].to;
            if (dist[v] > dist[u] + 1) {
                dist[v] = dist[u] + 1, cnt[v] = cnt[u];
                q.push(make_pair(-dist[v], v));
            } else if (dist[v] == dist[u] + 1)
                cnt[v] = (cnt[v] + cnt[u]) % 100003;
        }
    }
    for (int i = 1; i <= n; i++)
    	printf("%d\n", cnt[i]);
    return 0;
}

http://www.niftyadmin.cn/n/5154179.html

相关文章

JVM 各个参数详解

在一些规模稍大的应用中&#xff0c;Java虚拟机&#xff08;JVM&#xff09;的内存设置尤为重要&#xff0c;想在项目中取得好的效率&#xff0c;GC&#xff08;垃圾回收&#xff09;的设置是第一步。 PermGen space&#xff1a;全称是Permanent Generation space.就是说是永久…

Jetpack:030-Jetpack中的状态

文章目录 1. 概念介绍2. 使用方法2.1 可监听对象2.2 获取状态值2.3 修改状态值2.4 重组函数 3. 示例代码4. 内容总结 我们在上一章回中介绍了Jetpack中网格布局相关的内容&#xff0c;本章回中主要 介绍状态。闲话休提&#xff0c;让我们一起Talk Android Jetpack吧&#xff0…

优先级队列(堆)的概念+模拟堆的实现

文章目录 优先级队列&#xff08;堆&#xff09;的概念模拟堆的实现一、概念1.优先级队列2.堆1.堆的性质2.堆的存储3.堆的创建3.1 向下调整3.2建堆的时间复杂度 O(N) 4.堆的插入4.1向上调整4.2向上调整建堆的时间复杂度&#xff1a;O(N * log N) 5.堆的删除 优先级队列&#xf…

1.UML面向对象类图和关系

文章目录 4种静态结构图类图类的表示类与类之间的关系依赖关系(Dependency)关联关系(Association)聚合(Aggregation)组合(Composition)实现(Realization)继承/泛化(Inheritance/Generalization)常用的UML工具reference欢迎访问个人网络日志🌹🌹知行空间🌹🌹 4种静态结构…

ubuntu挂载共享目录的方法

ubuntu挂载共享目录的方法 安装NFS配置NFS 安装NFS sudo apt-get install nfs-kernel-server配置NFS 创建work共享目录:(本人将此文件放在桌面)sudo mkdir worksudo gedit /etc/exports添加: /home/zynq/Desktop/work *(rw,sync,no_root_squash,no_subtree_check)运行以下命…

运动重定向:TeachNet

Vision-based Teleoperation of Shadow Dexterous Hand using End-to-End Deep Neural Network解析 摘要1. 简介2. Related Work2.1 基于视觉的无标记远程操作2.2 基于深度的3D手部姿势估计2.3 远程操作中的主从配对2.4 遥操作映射方法 3. 师生网络Joint angle lossConsistency…

ICCV2023 Tracking paper汇总(一)(多目标跟随、单目标跟随等)

一、PVT: A Simple End-to-End Latency-Aware Visual Tracking Framework paper&#xff1a; https://openaccess.thecvf.com/content/ICCV2023/papers/Li_PVT_A_Simple_End-to-End_Latency-Aware_Visual_Tracking_Framework_ICCV_2023_paper.pdf github&#xff1a; https://…

6.2 创建和销毁互斥量

方法 pthread_mutex_init(mutex, attr) pthread_mutex_destroy(mutex) pthread_mutexattr_init(attr) pthread_mutexattr_destroy(attr) 用法 互斥量的类型为pthread_mutex_t&#xff0c;必须在使用前初始化。有如下两种初始化互斥量的方法&#xff1a; 声明时初始化&…