Submission #544674

#TimeUsernameProblemLanguageResultExecution timeMemory
544674pokmui9909두 로봇 (KOI18_robot)C++17
100 / 100
114 ms13180 KiB
#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)

robot.cpp: In function 'void dfs1(int, int)':
robot.cpp:13:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for(int i = 0; i < tree[node].size(); i++)
      |                    ~~^~~~~~~~~~~~~~~~~~~
robot.cpp: In function 'void dfs2(int, int)':
robot.cpp:25:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for(int i = 0; i < tree[node].size(); i++)
      |                    ~~^~~~~~~~~~~~~~~~~~~
robot.cpp: In function 'int main()':
robot.cpp:59:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         for(int j = 0; j < tree[i].size(); j++)
      |                        ~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...