1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| class Solution { public: int countPaths(int n, vector<vector<int>>& roads) { int mod = 1e9+7; long long dp[300]; long long mp[300][300]; long long cnt[300]; int u[300];
for(int i=0;i<n;i++) { dp[i]=0x7fffffffffffffff; cnt[i]=0; u[i]=0; } memset(mp, 0xff, sizeof mp); for(auto r:roads) { mp[r[0]][r[1]] = r[2]; mp[r[1]][r[0]] = r[2]; } dp[0]=0; cnt[0]=1; using Pair = pair<long long, int>; priority_queue<Pair, vector<Pair>, greater<Pair>> Q; Q.push(Pair(0, 0)); while (!Q.empty()) { auto k = Q.top().second; Q.pop(); if(u[k]) continue; u[k]=1; for (int i = 0; i < n; i++) { if (mp[k][i] !=-1 && dp[k] + mp[k][i] <= dp[i]) { if(dp[k]+mp[k][i]==dp[i]) { cnt[i] += cnt[k]; } else { cnt[i] = cnt[k]; } dp[i] = dp[k] + mp[k][i]; cnt[i] %= mod; Q.push(Pair(dp[i], i)); } } } return cnt[n-1]; } };
|