zoukankan      html  css  js  c++  java
  • [洛谷P1144]最短路计数

    题目大意:给你一个无权无向图,要你求出1到其他所有节点的最短路的条数

    题解:bfs,每次查到一个节点就把该节点的ans值加上它父节点的ans值

    C++ Code:

    #include<cstdio>
    const int mod=100003;
    using namespace std;
    int n,m;
    int ans[1000100],step[1000100];
    int head[1000100],cnt;
    struct Edge{
    	int to,nxt;
    }e[2000100<<1];
    int q[1000100],h,t;
    void addE(int a,int b){
    	e[++cnt]=(Edge){b,head[a]};
    	head[a]=cnt;
    }
    int main(){
    	scanf("%d%d",&n,&m);
    	for (int i=0;i<m;i++){
    		int a,b;
    		scanf("%d%d",&a,&b);
    		if (a==b)continue;
    		addE(a,b);
    		addE(b,a);
    	}
    	q[t=step[1]=1]=ans[1]=1;
    	while (h<t){
    		int x=q[++h];
    		for (int i=head[x];i;i=e[i].nxt){
    			int to=e[i].to;
    			if ((!step[to])||(step[x]<step[to])){
    				if (!step[to])q[++t]=to,step[to]=step[x]+1;
    				ans[to]=(ans[to]+ans[x])%mod;
    			}
    		}
    	}
    	for (int i=1;i<=n;i++)printf("%d
    ",ans[i]);
    	return 0;
    }
    
  • 相关阅读:
    JDBC JAVA数据库插入语句
    uri与url
    struts标签库
    jdbc使用
    mysql安装配置
    Json Web Token
    实现一个简单vue
    vue v2.5.0源码-双向数据绑定
    vue v2.5.0源码-初始化流程
    webpack
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/8592129.html
Copyright © 2011-2022 走看看