반응형

https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/description/

 

Minimum Fuel Cost to Report to the Capital - LeetCode

Minimum Fuel Cost to Report to the Capital - There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads. The capital city is city 0. You are given a

leetcode.com

 

DFS를 이용하여 풀 수 있었습니다.

0번 도시에서부터 모든 도시를 방문하고 돌아오면서, 방문했던 도시의 개수(각 도시에서 0번 도시로 향하면서 거친 도시의 개수)를 기억해줍니다.

각 도시에서 거쳐온 도시와 좌석의 개수를 이용하여, 다음 도시로 이동하는데 필요한 연료의 리터 수를 구해줍니다.

DFS 수행 이후, 0번 도시가 도착 지점이므로, 0번 도시에서 다음 도시로 이동하기 위해 필요했던 리터 수는 제외해줍니다.

class Solution {
public:
    vector<int> graph[100001];
    long long res = 0;

    long long minimumFuelCost(vector<vector<int>>& roads, int seats) {
        for(auto& road : roads) {
            graph[road[0]].push_back(road[1]);
            graph[road[1]].push_back(road[0]);
        }

        dfs(0, seats, -1);
        return res - (roads.size() + seats) / seats;
    }

    int dfs(int node, int seats, int prv) {
        int cnt = 1;
        for(int nxt : graph[node]) {
            if(nxt != prv)
                cnt += dfs(nxt, seats, node);
        }

        res += (cnt + seats - 1) / seats;

        return cnt;
    }
};
반응형

'Algorithm' 카테고리의 다른 글

LeetCode 67 - Add Binary  (0) 2023.02.14
LeetCode 1523 - Count Odd Numbers in an Interval Range  (0) 2023.02.13
LeetCode 2306 - Naming a Company  (0) 2023.02.09
LeetCode 45 - Jump Game II  (0) 2023.02.08
LeetCode 904 - Fruit Into Baskets  (0) 2023.02.07

+ Recent posts