博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Luogu】P1144最短路计数(BFS)
阅读量:4684 次
发布时间:2019-06-09

本文共 921 字,大约阅读时间需要 3 分钟。

 

此题使用BFS记录最短路的条数。思路如下:
因为是无权无向图,所以只要被BFS到就是最短路径。因此可以记录该点的最短路和最短路的条数:
如果点y还没被访问过,则记录dis[y],同时令ans[y]=ans[x]. 如果点y已经被访问过且当前为最短路径,则ans[y]+=ans[x]

#include
#include
inline long long read(){ long long num=0,f=1; char ch=getchar(); while(!isdigit(ch)){ if(ch=='-') f=-1; ch=getchar(); } while(isdigit(ch)){ num=num*10+ch-'0'; ch=getchar(); } return num*f;}struct Edge{ int next,to;}edge[4000010];int head[1000010],num;inline void add(int from,int to){ edge[++num]=(Edge){head[from],to}; head[from]=num;}int f[2000000],h,t=1;int dis[1000010]={-1},ans[1000010]={
0,1};int main(){ int n=read(),m=read(); for(int i=1;i<=m;++i){ int from=read(),to=read(); if(from==to) continue; add(from,to); add(to,from); } f[1]=1; while(h++

 

转载于:https://www.cnblogs.com/cellular-automaton/p/7467403.html

你可能感兴趣的文章
P2709 小B的询问
查看>>
润乾报表 动态控制文本的显示
查看>>
[oracle] 如何使用myBatis在数据库中插入数据并返回主键
查看>>
PHP echo 和 print 语句
查看>>
第一讲 一个简单的Qt程序分析
查看>>
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
UML-画类图与交互图的顺序
查看>>
6月7 考试系统
查看>>
mysql 基本操作
查看>>
zTree async 动态参数处理
查看>>
Oracle学习之常见错误整理
查看>>
HTC Sensation G14开盒
查看>>
lock_sga引起的ksvcreate :process(m000) creation failed
查看>>
数据库插入数据乱码问题
查看>>
OVER(PARTITION BY)函数用法
查看>>
altium annotate 选项设置 complete existing packages
查看>>
【模式识别与机器学习】——SVM举例
查看>>
【转】IT名企面试:微软笔试题(1)
查看>>