当前位置:网站首页>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;
}
边栏推荐
- ICDE 2023|TKDE Poster Session(CFP)
- Use cheat engine to modify money, life and stars in Kingdom rush
- 搭建主从模式集群redis
- Compile oglpg-9th-edition source code with clion
- 高频面试题
- 思维意识转变是施工企业数字化转型成败的关键
- The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
- Qpropertyanimation use and toast case list in QT
- Develop fixed asset management system, what voice is used to develop fixed asset management system
- [test development] takes you to know what software testing is
猜你喜欢

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

《架构整洁之道》读书笔记(下)

Advanced performance test series "24. Execute SQL script through JDBC"

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

Markdown基础语法

Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5

Excel finds the same value in a column, deletes the row or replaces it with a blank value

Yunna | why use the fixed asset management system and how to enable it

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
冒泡排序数组
随机推荐
性能测试如何创造业务价值
A4988 drive stepper motor "recommended collection"
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
QT中的QPropertyAnimation使用和toast案列
How to print mybats log plug-in using XML file
Digital scroll strip animation
Novice must see, click two buttons to switch to different content
High frequency interview questions
Processing strategy of message queue message loss and repeated message sending
Virtual machine initialization script, virtual machine mutual secret key free
PHP parser badminton reservation applet development requires online system
PyTorch函数中的__call__和forward函数
Codeworks 5 questions per day (1700 average) - day 4
预处理和预处理宏
golang:[]byte转string
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
移动机器人路径规划:人工势场法[通俗易懂]
451-memcpy、memmove、memset的实现
电脑使用哪个录制视频软件比较好
Date tool class (updated from time to time)