-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210129_BOJ_10282.cpp
More file actions
156 lines (134 loc) · 2.46 KB
/
Copy path210129_BOJ_10282.cpp
File metadata and controls
156 lines (134 loc) · 2.46 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 987654321;
int T = 0;
int n, d, c;
vector<vector<pair<int, int>>> v;
void input() {
v.assign(n + 1, vector<pair<int, int>>());
for (int i = 0; i < d; ++i) {
int n1, n2, seconds;
cin >> n2 >> n1 >> seconds;
v[n1].push_back({ seconds, n2 });
}
}
// 다익스트라ㅠㅠ
void findAnswer() {
vector<int> D(n + 1, 987654321);
D[c] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, c});
while (!pq.empty()) {
int weight = pq.top().first;
int x = pq.top().second;
pq.pop();
for (int i = 0; i < v[x].size(); ++i) {
int time = weight + v[x][i].first;
int y = v[x][i].second;
if (D[y] > time) {
D[y] = time;
pq.push({ time, y });
}
}
}
int cnt = 0;
int Max = -1;;
for (int i = 1; i < D.size(); ++i) {
if (D[i] == INF) continue;
if (D[i] >= 0)
cnt++;
if (D[i] > Max) {
Max = D[i];
}
}
cout << cnt << " " << Max << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> T;
while (T--) {
cin >> n >> d >> c;
input();
findAnswer();
}
return 0;
}
// DFS 망...ㅠㅠ
//#include <iostream>
//#include <vector>
//#include <algorithm>
//
//using namespace std;
//
//int T = 0;
//int n, d, c;
//vector<vector<pair<int, int>>> v;
//vector<bool> visit;
//vector<int> D;
//int hacked = 0;
//int seconds = 0;
//
//// DFS
//void findAnswer(int start, int sum) {
//
// if (visit[start] == true) {
// if (D[start] > sum)
// D[start] = sum;
// return;
// }
// else {
// visit[start] = true;
// hacked++;
// }
//
// if (D[start] > sum)
// D[start] = sum;
//
// for (int i = 0; i < v[start].size(); ++i) {
//
// findAnswer( v[start][i].second, v[start][i].first + sum );
// }
//}
//
//void input() {
//
// v.assign(n + 1, vector<pair<int, int>>());
// visit.assign(n + 1, false);
// D.assign(n + 1, 987654321);
// D[c] = 0;
//
// for (int i = 0; i < d; ++i) {
// int n1; pair<int, int> p;
// cin >> p.second >> n1 >> p.first;
// v[n1].push_back(p);
// }
//
// for (int i = 1; i < v.size(); ++i) {
// if (v[i].empty()) continue;
// sort(v[i].begin(), v[i].end());
// }
//
// hacked = 0;
// seconds = 0;
//}
//
//int main() {
//
// ios::sync_with_stdio(false);
// cin.tie(NULL), cout.tie(NULL);
// cin >> T;
//
// while (T--) {
//
// cin >> n >> d >> c;
// input();
// findAnswer(c, 0);
//
// cout << hacked << " " << seconds << "\n";
// }
//
// return 0;
//}