# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
544674 | pokmui9909 | 두 로봇 (KOI18_robot) | C++17 | 114 ms | 13180 KiB |
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 <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> tree[100005];
int cost1[100005], cost2[100005];
int visited[100005];
int n, x, y;
void dfs1(int node, int c)
{
visited[node] = 1;
cost1[node] = c;
for(int i = 0; i < tree[node].size(); i++)
{
int next = tree[node][i].first;
if(visited[next]) continue;
dfs1(next, c + tree[node][i].second);
}
}
void dfs2(int node, int c)
{
visited[node] = 1;
cost2[node] = c;
for(int i = 0; i < tree[node].size(); i++)
{
int next = tree[node][i].first;
if(visited[next]) continue;
dfs2(next, c + tree[node][i].second);
}
}
int main()
{
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
cin >> n >> x >> y;
if(x == y)
{
cout << 0;
return 0;
}
for(int i = 1; i <= n - 1; i++)
{
int a, b, c; cin >> a >> b >> c;
tree[a].push_back({b, c});
tree[b].push_back({a, c});
}
dfs1(x, 0);
for(int i = 1; i <= n; i++)
visited[i] = 0;
dfs2(y, 0);
//for(int i = 1; i <= n; i++)
//cout << cost1[i] << ' ';
int ans = INT_MAX;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j < tree[i].size(); j++)
{
ans = min(ans, cost1[i] + cost2[tree[i][j].first]);
}
}
cout << ans;
}
Compilation message (stderr)
# | 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... |