当前位置:网站首页>AcWing 1134. 最短路计数 题解(最短路)
AcWing 1134. 最短路计数 题解(最短路)
2022-07-02 18:27:00 【乔大先生】
AcWing 1134. 最短路计数
路径权重都是1,用 bfs找最短路径,找的过程中用cnt数组记录路线条数,需要注意当路径长度相等时,需要将两条路径的条数相加更新
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 4 * N, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], idx;
int n, m;
int dist[N];
int cnt[N];
int q[N];
int w[N];
void Add(int a, int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++ ;
}
void bfs(){
memset(dist, 0x3f, sizeof dist);
int hh = 0, tt = 0;
q[0] = 1;
dist[1] = 0;
cnt[1] = 1;
while(hh <= tt){
int t = q[hh ++ ];
for(int i = h[t]; ~i; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + 1){
dist[j] = dist[t] + 1;
cnt[j] = cnt[t];
q[ ++ tt] = j;
}
else if(dist[j] == dist[t] + 1){
cnt[j] = (cnt[j] + cnt[t]) % 100003;
}
}
}
}
int main()
{
cin>>n>>m;
memset(h, -1, sizeof h);
while(m -- ){
int a, b;
cin>>a>>b;
Add(a, b);
Add(b, a);
}
bfs();
for(int i = 1; i <= n; i ++ ){
cout<<cnt[i]<<endl;
}
return 0;
}
边栏推荐
- MySQL
- 2022.7.1-----leetcode.241
- 性能测试如何创造业务价值
- 全志A33使用主线U-Boot
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- 冒泡排序数组
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- Codeworks 5 questions per day (1700 average) - day 4
- Which video recording software is better for the computer
- Memory management of C
猜你喜欢
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
开发固定资产管理系统,开发固定资产管理系统用什么语音
Markdown基础语法
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
Excel finds the same value in a column, deletes the row or replaces it with a blank value
《架构整洁之道》读书笔记(下)
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
IEDA refactor的用法
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
随机推荐
Markdown basic grammar
4274. 后缀表达式-二叉表达式树
【ERP软件】ERP体系二次开发有哪些危险?
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
Fastdfs installation
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
PHP parser badminton reservation applet development requires online system
数据降维——因子分析
《代碼整潔之道》讀書筆記
451-memcpy、memmove、memset的实现
MySQL表历史数据清理总结
MySQL advanced (Advanced) SQL statement
Learn the knowledge points of eight part essay ~ ~ 1
新手必看,点击两个按钮切换至不同的内容
Refactoring: improving the design of existing code (Part 1)
Data dimensionality reduction factor analysis
Thread application instance