-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-1112.cpp
73 lines (56 loc) · 1.49 KB
/
uva-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
//uva 1112
//Mice and Maze
#include <iostream>
#include <climits>
#include <vector>
#include <set>
#include <map>
using namespace std;
void Dijkstra(vector <map <int, int> > & Graph, vector <int> & minDis, int start);
int main(void)
{
int T = 0;
cin >> T;
while(T--){
int n, end, time;
cin >> n >> end >> time;
--end;
int m;
cin >> m;
vector < map <int, int> > GraphT(n);
for(int i = 0; i < m; ++i){
int a, b, t;
cin >> a >> b >> t;
--a, --b;
GraphT[b].insert(make_pair(a, t));
}
vector <int> minDis(n, INT_MAX);
Dijkstra(GraphT, minDis, end);
int count = 0;
for(auto a : minDis)
if (a <= time)
++count;
cout << count << endl;
if (T) cout << endl;
}
return 0;
}
void Dijkstra(vector <map <int, int> > & Graph, vector <int> & minDis, int start)
{
minDis[start] = 0;
set <pair <int, int> > unvisitedNode;
unvisitedNode.insert(make_pair(0, start));
while(!unvisitedNode.empty()){
pair <int, int> top = *unvisitedNode.begin();
unvisitedNode.erase(top);
int node = top.second;
int value = top.first;
for(auto a : Graph[node]){
if(value + a.second < minDis[a.first]){
minDis[a.first] = value + a.second;
unvisitedNode.insert(make_pair(minDis[a.first], a.first));
}
}
}
return;
}