• 洛谷1144 最短路计数


    原题链接

    模板题。
    由于此题特殊,边权均为(1),所以可以直接跑(BFS),每个点的最短路就是该点在(BFS)搜索树中的深度,某个点的最短路计数则用上一层中能到达该点的计数来更新即可。

    #include<cstdio>
    using namespace std;
    const int N = 1e6 + 10;
    const int M = 4e6 + 10;
    const int mod = 100003;
    int fi[N], ne[M], di[M], dis[N], cnt[N], q[M], l;
    bool v[N];
    inline int re()
    {
    	int x = 0;
    	char c = getchar();
    	bool p = 0;
    	for (; c < '0' || c > '9'; c = getchar())
    		p |= c == '-';
    	for (; c >= '0' && c <= '9'; c = getchar())
    		x = x * 10 + c - '0';
    	return p ? -x : x;
    }
    inline void add(int x, int y)
    {
    	di[++l] = y;
    	ne[l] = fi[x];
    	fi[x] = l;
    }
    int main()
    {
    	int i, n, m, x, y, head = 0, tail = 1;
    	n = re();
    	m = re();
    	for (i = 1; i <= m; i++)
    	{
    		x = re();
    		y = re();
    		add(x, y);
    		add(y, x);
    	}
    	v[1] = cnt[1] = q[1] = 1;
    	while (head ^ tail)
    	{
    		x = q[++head];
    		for (i = fi[x]; i; i = ne[i])
    		{
    			if (!v[y = di[i]])
    			{
    				dis[y] = dis[x] + 1;
    				q[++tail] = y;
    				v[y] = 1;
    			}
    			if (!(dis[y] ^ (dis[x] + 1)))
    				cnt[y] = (cnt[y] + cnt[x]) % mod;
    		}
    	}
    	for (i = 1; i <= n; i++)
    		printf("%d
    ", cnt[i]);
    	return 0;
    }
    
  • 相关阅读:
    基于redis实现滑动窗口式的短信发送接口限流
    Linux 宝塔下的PHP如何与本地的nginx关联
    Linux 下php安装gd库
    Linux Mysql8重置密码
    PHP 无限分级类
    redis 缓存穿透,缓存雪崩,缓存击穿
    yii2 事务添加
    ConcurrentHashMap
    Volatile
    this引用的逸出
  • 原文地址:https://www.cnblogs.com/Iowa-Battleship/p/9804197.html
Copyright © 2020-2023  润新知