-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaWalkThroughTheForest_UVA10917.cpp
98 lines (79 loc) · 1.96 KB
/
aWalkThroughTheForest_UVA10917.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <bits/stdc++.h>
using namespace std;
ifstream fin("aWalkThroughTheForest_UVA10917.in");
ofstream fout("aWalkThroughTheForest_UVA10917.out");
using ll = long long;
const ll inf = LLONG_MAX / 2;
struct segment
{
ll u;
ll d;
bool operator < (const segment & temp) const
{
return d > temp.d;
}
};
segment _segment(ll u, ll d)
{
segment temp{u, d}; return temp;
}
ll count(ll u, vector<ll> & dp, vector<ll> & dis, vector<vector<ll>> & g)
{
if (u == 2)
{
return 1;
}
else if (dp[u] != -1)
{
return dp[u];
}
else
{
ll ans = 0;
ll size = g[u].size();
for (ll i = 0; i <= size - 1; ++i)
{
ll v = g[u][i];
if (dis[v] < dis[u])
{
ans += count(v, dp, dis, g);
}
}
dp[u] = ans;
return ans;
}
}
int main()
{
while (true)
{
ll n = 0; fin >> n;
if (n == 0) break;
ll m; fin >> m;
vector<vector<ll>> a(n + 1, vector<ll>(n + 1, inf)),
g(n + 1);
for (ll c = 1; c <= m; ++c)
{
ll u, v, d; fin >> u >> v >> d;
a[u][v] = d; a[v][u] = a[u][v];
g[u].push_back(v); g[v].push_back(u);
}
vector<ll> dis(n + 1, inf);
priority_queue<segment> pq; pq.push(_segment(2, 0));
while (pq.empty() == false)
{
segment now = pq.top(); pq.pop();
if (now.d >= dis[now.u]) continue;
dis[now.u] = now.d;
ll size = g[now.u].size();
for (ll i = 0; i <= size - 1; ++i)
{
ll v = g[now.u][i];
pq.push(_segment(v, now.d + a[now.u][v]));
}
}
vector<ll> dp(n + 1, -1);
fout << count(1, dp, dis, g) << '\n';
}
return 0;
}