洛谷 P1144

import java.io.*;
import java.util.*;

public class P1144 {
    
    static int maxN =1000005 ;
    static int maxM =2000005;
    static int mod = 100003;
    static List<Integer> list[] = new ArrayList[maxM];
    static int cnt[] = new int[maxN];
    static int dis[] = new int[maxN];
    static boolean isVis[]= new boolean[maxN];
    static PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        for(int i = 1;i <= N;i++){
            list[i] = new ArrayList<Integer>();
        }
        Arrays.fill(cnt, 0);
        Arrays.fill(dis, Integer.MAX_VALUE);
        Arrays.fill(isVis, false);
        pq.clear();
        for(int i = 1;i <= M;i++){
             st = new StringTokenizer(br.readLine());
             int x = Integer.parseInt(st.nextToken());
             int y = Integer.parseInt(st.nextToken());
             list[x].add(y);
             list[y].add(x);
        }
        
        dis[1] = 0;
        cnt[1] = 1;
        pq.add(new Edge(1,0));
        while(!pq.isEmpty()){
            Edge cur = pq.poll();
            if(!isVis[cur.index]){
                isVis[cur.index] = true;
                for(int i = 0;i < list[cur.index].size();i++){
                    int next = list[cur.index].get(i);
                    if(dis[next] == cur.weight + 1){
                        cnt[next] += cnt[cur.index];
                        cnt[next] %= mod;
                    }
                    if(dis[next] > cur.weight + 1){
                        dis[next] = cur.weight + 1;
                        cnt[next] = cnt[cur.index];
                        pq.add(new Edge(next,dis[next]));
                    }
                }
            }
        }
        
        for(int i = 1;i <= N;i ++){
            System.out.println(cnt[i]);
        }
    }
    
    static class Edge implements Comparable<Edge>{
        int index;
        int weight;
        
        public Edge(int index, int weight) {
            super();
            this.index = index;
            this.weight = weight;
        }

        public int compareTo(Edge o) {
            return this.weight - o.weight;
        }
        
    }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值