zoukankan      html  css  js  c++  java
  • 洛谷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;
    }
    
  • 相关阅读:
    Jenkins 插件管理
    持续集成 目录
    gitlab 目录
    jenkins 目录
    POJ 2828
    POJ 2782
    POJ 2725
    POJ 2769
    POJ 2739
    POJ 2707
  • 原文地址:https://www.cnblogs.com/Iowa-Battleship/p/9804197.html
Copyright © 2011-2022 走看看