Title

# p1144_最短路计数问题

5 7
1 2
1 3
2 4
3 4
2 3
4 5
4 5

1
1
1
2
4

## 说明/提示

111到555的最短路有444条，分别为222条1−2−4−51-2-4-51−2−4−5和222条1−3−4−51-3-4-51−3−4−5（由于4−54-54−5的边有222条）。

## AC 的代码


#include<bits/stdc++.h>
using namespace std;
const int MOD = 100003;
const int N = 1000000*2 +3;
const int M = 2000000*2 + 5;
int n,m;
struct Edge{
int u,v,next;
}e[M];
int dist[N];
bool vis[N];
int dist_cnt[N];
struct State {

int w;
int from;
bool operator < (const State &x) const {

return w> x.w;
}

};
void reset() {
for(int i=0;i<=n;++i) dist[i] = INT_MAX;

}
Edge &edg = e[++cnt];
edg.u = x,edg.v = y;
}

queue<int> q;
void dijkstra() {
reset();
dist[1] = 0;
dist_cnt[1] = 1;
q.push(1);
while(q.size()) {
int t = q.front();q.pop();
while(h) {
Edge& edg = e[h];
//cout <<" t: "<< t <<" u: "<< edg.u << " edg.v : "<<edg.v <<"dist[v]: "<<dist[edg.v] << endl;
if(dist[edg.v] > dist[t] + 1) {
dist[edg.v] = dist[t]+1;
dist_cnt[edg.v] = dist_cnt[t];
q.push(edg.v);
}else if(dist[edg.v] == dist[t] + 1) {
dist_cnt[edg.v] = (dist_cnt[edg.v] + dist_cnt[t])%MOD;
}
h = edg.next;

}
}

}

int main(void) {
cin>>n>>m;

for(int i=0;i<m;++i) {
int x,y;
cin>>x>>y;
if(x==y) continue;
}
dijkstra();
for(int i=1;i<=n;++i) cout << dist_cnt[i] <<endl;

return 0;

}


posted @ 2021-02-27 01:09  .geek  阅读(69)  评论(0编辑  收藏  举报