forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
51 lines (45 loc) · 1.33 KB
/
Solution.cpp
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
class Solution {
public:
int countPaths(int n, vector<vector<int>>& roads) {
const long long inf = LLONG_MAX / 2;
const int mod = 1e9 + 7;
vector<vector<long long>> g(n, vector<long long>(n, inf));
for (auto& e : g) {
fill(e.begin(), e.end(), inf);
}
for (auto& r : roads) {
int u = r[0], v = r[1], t = r[2];
g[u][v] = t;
g[v][u] = t;
}
g[0][0] = 0;
vector<long long> dist(n, inf);
fill(dist.begin(), dist.end(), inf);
dist[0] = 0;
vector<long long> f(n);
f[0] = 1;
vector<bool> vis(n);
for (int i = 0; i < n; ++i) {
int t = -1;
for (int j = 0; j < n; ++j) {
if (!vis[j] && (t == -1 || dist[j] < dist[t])) {
t = j;
}
}
vis[t] = true;
for (int j = 0; j < n; ++j) {
if (j == t) {
continue;
}
long long ne = dist[t] + g[t][j];
if (dist[j] > ne) {
dist[j] = ne;
f[j] = f[t];
} else if (dist[j] == ne) {
f[j] = (f[j] + f[t]) % mod;
}
}
}
return (int) f[n - 1];
}
};