Sort by

recency

|

98 Discussions

|

  • + 0 comments

    WTF this question is way too hard for a 40 score medium question. had to run dynamic programming on subsets of fishes, with a run of dijkstra at each subset, to get the answer

    O(2^k * (k*V + (V+E)*log(V)))

    void dijkstra(int N, const vector<vector<pair<int, int>>>& edges, vector<vector<long>>& distance, int subset) {
        set<pair<long, int>> S;
        for (int i=1; i <= N; ++i) {
            if (distance[subset][i] != LONG_MAX) S.insert({distance[subset][i], i});
        }
        while (!S.empty()) {
            int shortest = S.begin()->second;
            S.erase(S.begin());
            for (auto e : edges[shortest]) {
                if (distance[subset][e.first] > distance[subset][shortest] + e.second) {
                    S.erase({ distance[subset][e.first], e.first});
                    distance[subset][e.first] = distance[subset][shortest] + e.second;
                    S.insert({ distance[subset][e.first], e.first });
                }
            }
        }
    }
    
    int shop(int N, int k, const vector<vector<int>>& fishes, const vector<vector<pair<int, int>>>& roads) {
        int totalSubsets = pow(2, k);
        vector<vector<long>> distance(totalSubsets, vector<long>(N + 1, LONG_MAX));
        distance[0][1] = 0;
        dijkstra(N, roads, distance, 0);
        for (int subset = 1; subset < totalSubsets; ++subset) {
            for (int fish = 1, bit = 1; fish <= k; ++fish, bit <<= 1) {
                if ((subset & bit) == 0) continue;
                for (int shop : fishes[fish]) distance[subset][shop] = min(distance[subset][shop], distance[subset^bit][shop]);
            }
            dijkstra(N, roads, distance, subset);
        }
        long answer = LONG_MAX;
        for (int subset = 0; subset < totalSubsets; ++subset) {
            answer = min(answer, max(distance[subset][N], distance[subset^(totalSubsets - 1)][N]));
        }
        return answer;
    }
    
    int main()
    {
        int n, m, k, fishTypes, fish, x, y, w;
        cin >> n >> m >> k;
        vector<vector<int>> fishes(k + 1);
        vector<vector<pair<int, int>>> roads(n + 1);
        for (int shop = 1; shop <= n; ++shop) {
            cin >> fishTypes;
            for (int type = 1; type <= fishTypes; ++type) {
                cin >> fish;
                fishes[fish].push_back(shop);
            }
        }
        for (int j=1; j <= m; ++j) {
            cin >> x >> y >> w;
            roads[x].push_back({y, w});
            roads[y].push_back({x, w});
        }
        cout << shop(n, k, fishes, roads);
    }
    
  • + 1 comment

    C++ https://github.com/IhorVodko/Hackerrank_solutions/blob/master/Algorithms/synchronousShopping.cpp , feel free to give a star:) )

  • + 0 comments

    The is a powerful tool that ensures the recovery of all your accidentally deleted files including music, images, emails etc.

  • + 1 comment

    Recuva Pro Crack is a powerful tool that ensures the recovery of all your accidentally deleted files including music, images, emails etc.

  • + 0 comments

    Cpp solution (some tle test cases i dont know how to optimize, the complexity maybe O(n * m * n))

    #include<bits/stdc++.h>
    using namespace std;
    
    #define Max 100000
    
    void dijsktra(int n, vector<int> &fishtypes, vector<vector<pair<int, int>>> &adj, vector<vector<int>> &totalweight)
    {
        //create set of [distance, shop, types of fish]
        set<pair<int, pair<int, int>>> st;
        st.insert({0, {0, fishtypes[0]}});
        while(st.size() != 0)
        {
            pair<int, pair<int, int>> node = *(st.begin());
            st.erase(st.begin());
            int curdist = node.first;
            int curshop = node.second.first;
            int curTypeOfFish = node.second.second;
            //we will skip when we already go to that shop with the same types of fish collected but with smaller total weight
            if(curdist >= totalweight[curshop][curTypeOfFish]) continue;
            totalweight[curshop][curTypeOfFish] = curdist;
            for(int i = 0; i < adj[curshop].size(); i++)
            {
                int newdist = curdist + adj[curshop][i].second;
                int nextshop = adj[curshop][i].first;
                int newtype = curTypeOfFish | fishtypes[nextshop];
                pair<int, pair<int, int>> nextnode(newdist, {nextshop, newtype});
                st.insert(nextnode);
            }
        }
    }
    int main()
    {
        int n, m, k;
        cin >> n >> m >> k;
        vector<int> fishtypes(n, 0);
        //bit-masking for types of fish sold by i-th shop
        for(int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            for(int j = 0; j < x; j++)
            {
                int type;
                cin >> type;
                fishtypes[i] = fishtypes[i] | (1 << (type - 1));
            }
        }
        //create adj list
        vector<vector<pair<int, int>>> adj(n, vector<pair<int, int>>());
        for(int i = 0; i < m; i++)
        {
            int shop1;
            int shop2;
            int weight;
            cin >> shop1 >> shop2 >> weight;
            adj[shop1 - 1].push_back({shop2 - 1, weight});
            adj[shop2 - 1].push_back({shop1 - 1, weight});
        }
        vector<bool> visited(n, false);
        vector<vector<int>> totalweight(n, vector<int>(1 << k, INT_MAX));
        dijsktra(n, fishtypes, adj, totalweight);
        int res = INT_MAX;
        for(int i = 0; i < (1 << k); i++)
        {
            if(totalweight[n - 1][i] == INT_MAX) continue;
            for(int j = i; j < (1 << k); j++)
            {
                if(totalweight[n - 1][j] == INT_MAX) continue;
                if((i | j) == (1 << k) - 1)
                {
                    res = min(res, max(totalweight[n - 1][i], totalweight[n - 1][j]));
                }
            }
        }
        cout << res;
        return 0;
    }