luogu 1144

最短路计数

#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10;
const int oo = (1 << 30);

#define gc getchar()

inline int read() {
    int x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}

struct Node {
    int u, dis_;
    bool operator < (Node a) const {
        return dis_ > a.dis_;
    }
};
struct Node_2 {int u, v, w, nxt;} G[M];
int head[N], dis[N], cnt;
int f[N];
int n, m, k, Mod;
bool vis[N];
priority_queue <Node> Q;
int Cnt[N];

inline void Link(int u, int v, int w) {
    G[++ cnt].u = u; G[cnt].v = v; G[cnt].w = w; G[cnt].nxt = head[u]; head[u] = cnt;
}

void Dij(int start) {
    for(int i = 1; i <= n; i ++) dis[i] = oo, vis[i] = 0, Cnt[i] = 0;
    Q.push((Node) {start, 0});
    dis[start] = 0;
    Cnt[start] = 1;
    while(!Q.empty()) {
        Node topp = Q.top();
        Q.pop();
        if(vis[topp.u]) continue;
        vis[topp.u] = 1;
        for(int i = head[topp.u]; ~ i; i = G[i].nxt) {
            int v = G[i].v;
            if(dis[v] > dis[topp.u] + G[i].w) {
                Cnt[v] = Cnt[topp.u];
                dis[v] = dis[topp.u] + G[i].w;
                Q.push((Node) {v, dis[v]});
            } else if(dis[v] == dis[topp.u] + G[i].w) {
                (Cnt[v] += Cnt[topp.u]) %= Mod;
            }
        }
    }
}

int main() {
    Mod = 100003;
    n = read(), m = read();
    for(int i = 1; i <= n; i ++) head[i] = -1;
    cnt = 0;
    for(int i = 1; i <= m; i ++) {
        int u, v, w;
        u = read(), v = read(), w = 1;
        Link(u, v, w); Link(v, u, w);
    }
    Dij(1);
    for(int i = 1; i <= n; i ++) {
        cout << Cnt[i] % Mod << "\n";
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shandongs1/p/9634933.html