-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSolution.cpp
More file actions
75 lines (67 loc) · 1.43 KB
/
Solution.cpp
File metadata and controls
75 lines (67 loc) · 1.43 KB
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
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
const int N = 101;
vector<pii>G[N];
int dist[N];
int ct;
void dijkstra(int src)
{
priority_queue<pii, vector<pii>, greater<pii> >pq;
pq.push({0, src});
dist[src] = 0;
while(!pq.empty())
{
int uw = pq.top().first;
int u = pq.top().second;
pq.pop();
if(uw != dist[u] || uw >= ct)
continue;
int sz = G[u].size();
for(int i = 0; i < sz; i++)
{
int vw = G[u][i].first;
int v = G[u][i].second;
int cost = dist[u] + vw;
if(dist[v] > cost)
{
dist[v] = cost;
pq.push({cost, v});
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, e;
scanf("%d", &n);
scanf("%d", &e);
scanf("%d", &ct);
int m;
scanf("%d", &m);
int u, v, w;
for(int i = 1; i <= n; i++)
{
G[i].clear();
dist[i] = INT_MAX / 2;
}
while(m--)
{
scanf("%d %d %d", &u, &v, &w);
G[v].push_back({w, u});
}
dijkstra(e);
int ans = 0;
for(int i = 1; i <= n; i++)
if(dist[i] <= ct)
ans++;
printf("%d\n", ans);
if(t)
puts("");
}
return 0;
}