This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |