-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1112.cpp
101 lines (93 loc) · 1.6 KB
/
1112.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
99
100
101
/*
1112 - Mice and Maze
*/
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pp;
vector<pp>graph[101];
int cost[101];
void reset_all(int n)
{
for(int i=0; i<=n; i++)
{
graph[i].clear();
cost[i]=INT_MAX;
}
}
void dijkstra(int e,int c,int t)
{
priority_queue<pp,vector<pp>,greater<pp>>q;
q.push(make_pair(c,e));
cost[e]=c;
while(!q.empty())
{
pp u = q.top();
q.pop();
int uu = u.second;
///this is u
int tu = u.first;
///this is cost u
if(cost[uu]>t)
continue;
for(int i=0; i<graph[uu].size(); i++)
{
pp v = graph[uu][i];
///this is v
int tv = v.second;
///this is cost v
int vv = v.first;
if(tu+tv<cost[vv])
{
cost[vv]=tu+tv;
q.push(make_pair(cost[vv],vv));
}
}
}
}
int main()
{
///freopen("input.txt", "r", stdin);
///freopen("output.txt", "w", stdout);
int t,tc=0;
cin>>t;
while(t--)
{
if(tc)printf("\n");
tc=1;
int m,n,e,t,x,y,c,ans=0;
cin>>n>>e>>t;
cin>>m;
reset_all(n);
for(int i=0; i<m; i++)
{
cin>>x>>y>>c;
graph[y].push_back(make_pair(x,c));
}
dijkstra(e,0,t);
for(int i=1; i<=n; i++)
{
if(cost[i]<=t)
ans++;
}
cout<<ans<<endl;
}
return 0;
}
/*
Sample Input
1
4
2
1
8
1 2 1
1 3 1
2 1 1
2 4 1
3 1 1
3 4 1
4 2 1
4 3 1
Sample Output
3
*/