Cheapest Cost to All Cities - Google Top Interview Questions


Problem Statement :


You are given two lists of integers costs_from and costs_to of the same length where each index i represents a city. 

Building a one-way road from city i to j costs costs_from[i] + costs_to[j]. 

You are also given a two-dimensional list of integers edges where each list contains [x, y] meaning there's already a one-way road from city x to y.

Given that we want to be able to go to any city from city 0 (not necessarily in one path), return the minimum cost to build the necessary roads.

Constraints

n ≤ 1,000 where n is the length of costs_from and costs_to

m ≤ 100,000 where m is the length of edges

Example 1

Input

costs_from = [5, 1, 1, 10]

costs_to = [1, 1, 2, 1]

edges = [

    [0, 3]

]

Output

9

Explanation

We can go 0 to 2 for a cost of 7. Then, we can go 2 to 1 for a cost of 2. We already have the ability to go 
to 3 from 0 for free.



Solution :



title-img




                        Solution in C++ :

const int M = 1000000;
const int N = 1000;

struct edg {
    int u, v;
    int cost;
} E[M], E_copy[M];

int In[N], ID[N], vis[N], pre[N];

int Directed_MST(int root, int NV, int NE) {
    for (int i = 0; i < NE; i++) {
        E_copy[i] = E[i];
    }
    int ret = 0;
    int u, v;
    while (true) {
        for (int i = 0; i < NV; i++) {
            In[i] = 2000000000;
        }
        for (int i = 0; i < NE; i++) {
            u = E_copy[i].u;
            v = E_copy[i].v;
            if (E_copy[i].cost < In[v] && u != v) {
                In[v] = E_copy[i].cost;
                pre[v] = u;
            }
        }

        int cnt = 0;
        for (int i = 0; i < NV; i++) {
            ID[i] = -1;
            vis[i] = -1;
        }
        In[root] = 0;

        // check for cycles
        for (int i = 0; i < NV; i++) {
            ret += In[i];
            int v = i;
            while (vis[v] != i && ID[v] == -1 && v != root) {
                vis[v] = i;
                v = pre[v];
            }
            if (v != root && ID[v] == -1) {
                for (u = pre[v]; u != v; u = pre[u]) {
                    ID[u] = cnt;
                }
                ID[v] = cnt++;
            }
        }
        // check if there are no cycles
        if (cnt == 0) {
            break;
        }

        // condense cycles
        for (int i = 0; i < NV; i++) {
            if (ID[i] == -1) {
                ID[i] = cnt++;
            }
        }
        for (int i = 0; i < NE; i++) {
            v = E_copy[i].v;
            E_copy[i].u = ID[E_copy[i].u];
            E_copy[i].v = ID[E_copy[i].v];
            if (E_copy[i].u != E_copy[i].v) {
                E_copy[i].cost -= In[v];
            }
        }
        NV = cnt;
        root = ID[root];
    }
    return ret;
}

int solve(vector<int>& costs_from, vector<int>& costs_to, vector<vector<int>>& edges) {
    int n = costs_from.size();
    vector<pair<int, int>> v;
    for (auto& e : edges) {
        v.emplace_back(e[0], e[1]);
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            pair<int, int> key = make_pair(i, j);
            auto it = lower_bound(v.begin(), v.end(), key);
            if (it != v.end() && *it == key) {
                E[i * n + j].cost = 0;
            } else {
                E[i * n + j].cost = costs_from[i] + costs_to[j];
            }
            E[i * n + j].u = i;
            E[i * n + j].v = j;
        }
    }
    return Directed_MST(0, n, n * n);
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(int[] costs_from, int[] costs_to, int[][] edges) {
        int n = costs_to.length;
        ArrayList<Integer>[] es = new ArrayList[n];
        int[] rd = new int[n], low = new int[n], st = new int[n], en = new int[n];
        for (int i = 0; i < n; ++i) {
            rd[i] = -1;
            st[i] = costs_from[i];
            en[i] = costs_to[i];
        }
        Arrays.fill(low, n + 1);
        boolean[] path = new boolean[n], seen = new boolean[n];
        for (int[] e : edges) {
            if (es[e[0]] == null)
                es[e[0]] = new ArrayList();
            es[e[0]].add(e[1]);
        }
        int from = mark(es, 0, seen, costs_from), res = 0;
        for (int i = 1; i < n; ++i) {
            if (!seen[i]) {
                ArrayList<Integer> al = new ArrayList();
                int[] ms = new int[1];
                ms[0] = costs_from[i];
                int t = costs_to[i];
                traverse(es, st, rd, 0, low, new Stack<Integer>(), al, i, seen, path, ms, i);
                for (int a : al) {
                    rd[a] = i;
                    st[a] = ms[0];
                    t = Math.min(t, costs_to[a]);
                }
                for (int a : al) {
                    en[a] = t;
                }
            }
        }
        ArrayList<int[]> al = new ArrayList();
        for (int i = 1; i < n; ++i) {
            int t = find(rd, i);
            if (t >= 0) {
                rd[t] = -1;
                al.add(new int[] {st[t], en[t]});
            }
        }
        if (al.size() == 0)
            return 0;
        Collections.sort(al, (a, b) -> (a[0] - b[0]));
        for (int[] d : al) {
            res += from + d[1];
            from = Math.min(from, d[0]);
        }
        return res;
    }
    private int mark(ArrayList<Integer>[] es, int idx, boolean[] seen, int[] fr) {
        seen[idx] = true;
        int res = fr[idx];
        if (es[idx] != null) {
            for (int a : es[idx]) {
                if (!seen[a]) {
                    int t = mark(es, a, seen, fr);
                    res = Math.min(res, t);
                }
            }
        }
        return res;
    }
    private int find(int[] rd, int idx) {
        while (idx >= 0 && idx != rd[idx]) {
            int t = rd[idx];
            if (t >= 0)
                rd[idx] = rd[t];
            idx = rd[idx];
        }
        return idx;
    }
    private void traverse(ArrayList<Integer>[] es, int[] st, int[] rd, int h, int[] low,
        Stack<Integer> sta, ArrayList<Integer> al, int idx, boolean[] seen, boolean[] path,
        int[] ms, int root) {
        seen[idx] = true;
        low[idx] = h;
        path[idx] = true;
        sta.push(idx);
        if (es[idx] != null) {
            for (int a : es[idx]) {
                ms[0] = Math.min(ms[0], st[a]);
                int t = find(rd, a);
                if (t >= 0)
                    rd[t] = -1;
                if (!seen[a]) {
                    traverse(es, st, rd, h + 1, low, sta, al, a, seen, path, ms, root);
                    low[idx] = Math.min(low[idx], low[a]);
                } else if (path[a]) {
                    low[idx] = Math.min(low[idx], low[a]);
                }
            }
        }
        if (low[idx] == h) {
            while (!sta.isEmpty()) {
                int t = sta.pop();
                if (idx == root)
                    al.add(t);
                path[t] = false;
                if (t == idx)
                    break;
            }
        }
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, costs_from, costs_to, edges):
        adj = defaultdict(list)
        for u, v in edges:
            if v != 0:
                adj[u].append(v)

        idx = -1
        start = {}
        lowlink = {}
        s = []
        in_stack = set()
        scc = {}
        root = {}

        def tarjan(v):
            nonlocal idx
            idx += 1
            start[v] = idx
            lowlink[v] = idx
            s.append(v)
            in_stack.add(v)

            for n in adj[v]:
                if n not in start:
                    tarjan(n)
                    lowlink[v] = min(lowlink[v], lowlink[n])
                elif n in in_stack:
                    lowlink[v] = min(lowlink[v], start[n])

            if lowlink[v] == start[v]:
                new_scc = []
                while s[-1] != v:
                    in_stack.remove(s[-1])
                    root[s[-1]] = v
                    new_scc.append(s.pop())

                in_stack.remove(s[-1])
                root[s[-1]] = v
                new_scc.append(s.pop())

                scc[v] = new_scc

        for i in range(len(costs_from)):
            if i not in start:
                tarjan(i)

        new_adj = defaultdict(set)
        new_from = {}
        new_to = {}

        for r in scc:
            to = fr = float("inf")
            for n in scc[r]:
                to = min(to, costs_to[n])
                fr = min(fr, costs_from[n])
                new_adj[r].update(root[k] for k in adj[n])

            new_from[r] = fr
            new_to[r] = to

        visited = set()
        sources = {}

        def dfs(cur):
            m = new_from[cur]
            for n in new_adj[cur]:
                if n in sources:
                    m = min(m, sources[n])
                    del sources[n]
                elif n not in visited:
                    visited.add(n)
                    m = min(m, dfs(n))

            return m

        for v in new_to.keys():
            if v not in visited:
                visited.add(v)
                sources[v] = dfs(v)

        src = min(sources.keys(), key=sources.get)
        res = sources[0] - sources[src]
        for v in sources:
            if v != 0:
                res += sources[src] + new_to[v]

        return res
                    


View More Similar Problems

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →

Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

View Solution →

Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

View Solution →