#include<cstdio>
#include<vector>
#include<queue>
#include<string.h>
#include<algorithm>
#define INF 1000000000
using namespace std;
vector<pair<int, int> > v[100001];
int dist[100001];
pair<int, int> pa[100001];
int main()
{
int n, s, e, a, b, c;
scanf("%d %d %d", &n, &s, &e);
for(int i = 1;i < n;i++)
{
scanf("%d %d %d", &a, &b, &c);
v[a].push_back({b, c});
v[b].push_back({a, c});
}
if(s == e)
{
printf("0");
return 0;
}
priority_queue<pair<int, pair<int, pair<int, int> > >, vector<pair<int, pair<int, pair<int, int> > > >, greater<pair<int, pair<int, pair<int, int> > > > > pq;
fill(dist, dist + n + 1, INF);
pq.push({0, {s, {s, 0}}});
dist[s] = 0;
bool visited[100001] = {0};
while(!pq.empty())
{
int curr, prev, d;
do{
curr = pq.top().second.first;
prev = pq.top().second.second.first;
d = pq.top().second.second.second;
pq.pop();
} while(!pq.empty() && visited[curr]);
visited[curr] = true;
pa[curr] = {prev, d};
for(int i = 0;i < v[curr].size();i++)
{
int next = v[curr][i].first;
int d = v[curr][i].second;
if(dist[next] > dist[curr] + d)
{
dist[next] = dist[curr] + d;
pq.push({dist[next], {next, {curr, d}}});
}
}
}
int x = e, m = 0;
while(x != pa[x].first)
{
m = max(m, pa[x].second);
x = pa[x].first;
}
printf("%d", dist[e] - m);
}
Compilation message
robot.cpp: In function 'int main()':
robot.cpp:44:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i < v[curr].size();i++)
~~^~~~~~~~~~~~~~~~
robot.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &n, &s, &e);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
robot.cpp:19:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
8776 KB |
Output is correct |
2 |
Correct |
67 ms |
9304 KB |
Output is correct |
3 |
Correct |
56 ms |
11592 KB |
Output is correct |
4 |
Correct |
5 ms |
11592 KB |
Output is correct |
5 |
Correct |
10 ms |
11592 KB |
Output is correct |
6 |
Correct |
12 ms |
11592 KB |
Output is correct |
7 |
Correct |
4 ms |
11592 KB |
Output is correct |
8 |
Correct |
15 ms |
11592 KB |
Output is correct |
9 |
Correct |
5 ms |
11592 KB |
Output is correct |
10 |
Correct |
20 ms |
11592 KB |
Output is correct |
11 |
Correct |
4 ms |
11592 KB |
Output is correct |
12 |
Correct |
4 ms |
11592 KB |
Output is correct |
13 |
Correct |
20 ms |
11592 KB |
Output is correct |
14 |
Correct |
22 ms |
11592 KB |
Output is correct |
15 |
Correct |
6 ms |
11592 KB |
Output is correct |
16 |
Correct |
25 ms |
11964 KB |
Output is correct |
17 |
Correct |
28 ms |
12856 KB |
Output is correct |
18 |
Correct |
33 ms |
14020 KB |
Output is correct |
19 |
Correct |
7 ms |
14020 KB |
Output is correct |
20 |
Correct |
39 ms |
15340 KB |
Output is correct |
21 |
Correct |
47 ms |
16708 KB |
Output is correct |
22 |
Correct |
43 ms |
18192 KB |
Output is correct |
23 |
Correct |
8 ms |
18192 KB |
Output is correct |
24 |
Correct |
50 ms |
20152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
23724 KB |
Output is correct |
2 |
Correct |
54 ms |
23724 KB |
Output is correct |
3 |
Correct |
4 ms |
23724 KB |
Output is correct |
4 |
Correct |
4 ms |
23724 KB |
Output is correct |
5 |
Correct |
16 ms |
23724 KB |
Output is correct |
6 |
Correct |
12 ms |
23724 KB |
Output is correct |
7 |
Correct |
4 ms |
23724 KB |
Output is correct |
8 |
Correct |
16 ms |
23724 KB |
Output is correct |
9 |
Correct |
24 ms |
23724 KB |
Output is correct |
10 |
Correct |
4 ms |
23724 KB |
Output is correct |
11 |
Correct |
24 ms |
23724 KB |
Output is correct |
12 |
Correct |
31 ms |
23724 KB |
Output is correct |
13 |
Correct |
6 ms |
23724 KB |
Output is correct |
14 |
Correct |
6 ms |
23724 KB |
Output is correct |
15 |
Correct |
44 ms |
24836 KB |
Output is correct |
16 |
Correct |
32 ms |
24836 KB |
Output is correct |
17 |
Correct |
34 ms |
26024 KB |
Output is correct |
18 |
Correct |
55 ms |
28544 KB |
Output is correct |
19 |
Correct |
42 ms |
28660 KB |
Output is correct |
20 |
Correct |
96 ms |
30756 KB |
Output is correct |
21 |
Correct |
9 ms |
30756 KB |
Output is correct |
22 |
Correct |
10 ms |
30756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
30756 KB |
Output is correct |
2 |
Correct |
5 ms |
30756 KB |
Output is correct |
3 |
Correct |
5 ms |
30756 KB |
Output is correct |
4 |
Correct |
4 ms |
30756 KB |
Output is correct |
5 |
Correct |
5 ms |
30756 KB |
Output is correct |
6 |
Correct |
5 ms |
30756 KB |
Output is correct |
7 |
Correct |
5 ms |
30756 KB |
Output is correct |
8 |
Correct |
4 ms |
30756 KB |
Output is correct |
9 |
Correct |
5 ms |
30756 KB |
Output is correct |
10 |
Correct |
6 ms |
30756 KB |
Output is correct |
11 |
Correct |
6 ms |
30756 KB |
Output is correct |
12 |
Correct |
6 ms |
30756 KB |
Output is correct |
13 |
Correct |
6 ms |
30756 KB |
Output is correct |
14 |
Correct |
7 ms |
30756 KB |
Output is correct |
15 |
Correct |
7 ms |
30756 KB |
Output is correct |
16 |
Correct |
4 ms |
30756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
34444 KB |
Output is correct |
2 |
Correct |
111 ms |
36016 KB |
Output is correct |
3 |
Correct |
54 ms |
36016 KB |
Output is correct |
4 |
Correct |
52 ms |
36016 KB |
Output is correct |
5 |
Correct |
55 ms |
38652 KB |
Output is correct |
6 |
Correct |
92 ms |
40152 KB |
Output is correct |
7 |
Correct |
5 ms |
40152 KB |
Output is correct |
8 |
Correct |
5 ms |
40152 KB |
Output is correct |
9 |
Correct |
4 ms |
40152 KB |
Output is correct |
10 |
Correct |
11 ms |
40152 KB |
Output is correct |
11 |
Correct |
5 ms |
40152 KB |
Output is correct |
12 |
Correct |
16 ms |
40152 KB |
Output is correct |
13 |
Correct |
16 ms |
40152 KB |
Output is correct |
14 |
Correct |
12 ms |
40152 KB |
Output is correct |
15 |
Correct |
4 ms |
40152 KB |
Output is correct |
16 |
Correct |
8 ms |
40152 KB |
Output is correct |
17 |
Correct |
15 ms |
40152 KB |
Output is correct |
18 |
Correct |
5 ms |
40152 KB |
Output is correct |
19 |
Correct |
16 ms |
40152 KB |
Output is correct |
20 |
Correct |
22 ms |
40152 KB |
Output is correct |
21 |
Correct |
22 ms |
40152 KB |
Output is correct |
22 |
Correct |
6 ms |
40152 KB |
Output is correct |
23 |
Correct |
5 ms |
40152 KB |
Output is correct |
24 |
Correct |
4 ms |
40152 KB |
Output is correct |
25 |
Correct |
6 ms |
40152 KB |
Output is correct |
26 |
Correct |
21 ms |
40152 KB |
Output is correct |
27 |
Correct |
7 ms |
40152 KB |
Output is correct |
28 |
Correct |
24 ms |
40620 KB |
Output is correct |
29 |
Correct |
31 ms |
41604 KB |
Output is correct |
30 |
Correct |
30 ms |
42180 KB |
Output is correct |
31 |
Correct |
7 ms |
42180 KB |
Output is correct |
32 |
Correct |
6 ms |
42180 KB |
Output is correct |
33 |
Correct |
6 ms |
42180 KB |
Output is correct |
34 |
Correct |
26 ms |
42768 KB |
Output is correct |
35 |
Correct |
7 ms |
42768 KB |
Output is correct |
36 |
Correct |
50 ms |
44664 KB |
Output is correct |
37 |
Correct |
41 ms |
45472 KB |
Output is correct |
38 |
Correct |
29 ms |
45472 KB |
Output is correct |
39 |
Correct |
8 ms |
45472 KB |
Output is correct |
40 |
Correct |
4 ms |
45472 KB |
Output is correct |
41 |
Correct |
33 ms |
46356 KB |
Output is correct |
42 |
Correct |
11 ms |
46356 KB |
Output is correct |
43 |
Correct |
41 ms |
47704 KB |
Output is correct |
44 |
Correct |
61 ms |
50332 KB |
Output is correct |
45 |
Correct |
67 ms |
51200 KB |
Output is correct |
46 |
Correct |
46 ms |
51200 KB |
Output is correct |
47 |
Correct |
48 ms |
52412 KB |
Output is correct |
48 |
Correct |
90 ms |
54652 KB |
Output is correct |
49 |
Correct |
94 ms |
55636 KB |
Output is correct |
50 |
Correct |
8 ms |
55636 KB |
Output is correct |
51 |
Correct |
10 ms |
55636 KB |
Output is correct |
52 |
Correct |
14 ms |
55636 KB |
Output is correct |
53 |
Correct |
50 ms |
56968 KB |
Output is correct |