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

    题目传送门

    解题思路:

    因为所有的边权都为1,所以一个点的最短路就相当于是它在BFS搜索树中的深度。

    一个点最短路一定经过了一个层数比它少一的结点(否则不是最短路)。

    所以用每个相邻且层数比当前结点层数少一的点更新当前点的路径条数即可。

    //来自GGN_2015大佬的思路

    AC代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<vector>
     4 #include<queue>
     5 
     6 using namespace std;
     7 
     8 int n,m,x,y,deep[1000001],cnt[1000001];
     9 vector<int> a[1000001]; 
    10 queue<int> q;
    11 bool vis[1000001];
    12 
    13 inline void prepare() {
    14     q.push(1);
    15     deep[1] = 0;
    16     vis[1] = 1;
    17     cnt[1] = 1;
    18 }
    19 
    20 inline void solve() {
    21     while(!q.empty()) {
    22         int x = q.front();
    23         q.pop();
    24         for(int i = 0;i < a[x].size(); i++) {
    25             int t = a[x][i];
    26             if(!vis[t]) {
    27                 vis[t] = 1;
    28                 deep[t] = deep[x] + 1;
    29                 q.push(t);
    30             }
    31             if(deep[t] == deep[x] + 1) 
    32                 cnt[t] = (cnt[t] + cnt[x]) % 100003;
    33         }
    34     }
    35 }
    36 
    37 inline void _read() {
    38     scanf("%d%d",&n,&m);
    39     for(int i = 1;i <= m ;i++) {
    40         scanf("%d%d",&x,&y);
    41         a[x].push_back(y);
    42         a[y].push_back(x);
    43     }
    44 }
    45 
    46 inline void _print() {
    47     for(int i = 1;i <= n; i++)
    48         printf("%d
    ",cnt[i]);
    49 }
    50 
    51 int main() {
    52     _read();
    53     prepare();
    54     solve();
    55     _print();
    56     return 0;
    57 }
  • 相关阅读:
    线程唤醒
    以前、现在
    黑马程序员训练营基础测试
    流程图
    线程同步
    Thread联系
    Java线程
    Java中文件流笔记
    字符串习题
    匿名内部类
  • 原文地址:https://www.cnblogs.com/lipeiyi520/p/11258115.html
Copyright © 2011-2022 走看看