Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ll long long
- #define ar array<ll,2>
- class Solution {
- public:
- const int mod = 1e9+7;
- void add_self(int &a, int b){
- a+=b;
- if(a>=mod) a-=mod;
- }
- int dijkstra( vector<vector<pair<int,int>>>&g,int n){
- priority_queue<ar, vector<ar>, greater<ar>> pq;
- pq.push({0LL,0LL});
- const int inf = 1e16+5;
- vector<ll>dis(n,inf);
- vector<int>num(n,0);
- dis[0]=0;
- num[0]=1LL;
- while(!pq.empty()){
- auto [dist,node] = pq.top();
- pq.pop();
- if(dis[node]!=dist) continue;
- for(auto [d,to]: g[node]){
- if(dis[node]+d<dis[to]){
- dis[to] = (dis[node]+d);
- num[to] = (num[node]%mod);
- pq.push({dis[to],to});
- }
- else if(dis[node]+d==dis[to]){
- add_self(num[to],num[node]);
- }
- }
- }
- return num[n-1];
- }
- int countPaths(int n, vector<vector<int>>& roads) {
- vector<vector<pair<int,int>>>g(n);
- for(int i=0;i<(int)roads.size();i++){
- int u=roads[i][0],v=roads[i][1],w=roads[i][2];
- g[u].push_back({w,v});
- g[v].push_back({w,u});
- }
- return dijkstra(g,n);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement