Minimal time to go to all nodes of given Graph a minimum of as soon as

[ad_1]

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int minimizeTime(vector<vector<int> >& graph)

{

    lengthy lengthy n = graph.measurement();

  

    

    vector<vector<lengthy lengthy> > adj(n);

  

    for (int i = 0; i < n; i++) {

        for (auto j : graph[i]) {

  

            adj[i].push_back(j);

        }

    }

  

    

    lengthy lengthy finalMask = (lengthy lengthy)(1 << n) - 1;

  

    

    

    queue<pair<lengthy lengthy, lengthy lengthy> > q;

  

    

    

    vector<vector<bool> > visited(

        n, vector<bool>(finalMask + 1));

  

    

    

    for (int i = 0; i < n; i++) {

        q.push({ i, (lengthy lengthy)(1 << i) });

    }

  

    

    

    lengthy lengthy timeCount = 0;

  

    

    whereas (q.measurement() > 0) {

        int measurement = q.measurement();

  

        

        for (int i = 0; i < measurement; i++) {

  

            

            auto curr = q.entrance();

            q.pop();

  

            

            

            if (curr.second == finalMask)

                return timeCount;

  

            

            for (auto youngster : adj[curr.first]) {

  

                

                lengthy lengthy newVisitedBit

                    = curr.second | (1 << youngster);

  

                

                

                

                

                

                if (visited[child][newVisitedBit]

                    == false) {

  

                    q.push({ youngster, newVisitedBit });

                    visited[child][newVisitedBit] = true;

                }

            }

        }

  

        

        timeCount++;

    }

  

    

    return -1;

}

  

int most important()

{

    vector<vector<int> > graph = {

        { 1, 2, 3 }, { 2, 0 }, { 0, 1 }, { 0, 4 }, { 3 }

    };

  

    

    int minTime = minimizeTime(graph);

    cout << minTime << endl;

  

    return 0;

}

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *