Tuesday 9 February 2021

Shortest Path? Toph Criterion 2021 Round 9 Explanation with code

Problem link

First of all we need to find the shortest paths from a source node S to destination node T of the given graph.

And secondly a node will be given say X and we need to find the minimum distance from node X to any node of the shortest paths.



For find that a node belongs to the shortest path or not? here is the observations,

we will run a bfs from source node and find the distances of all node from source. And again we will run another bfs from destination node and find the distance of all node from destination. If you have any doubt on bfs check the link

A node will be belong to any shortest path of the graph if distance from source + distance from destination of the node equal to the shortest path distance, in another word,

source_distance[i]+destination_distance[i] == source_distance[destination]

Now we had to find distance from any shortest path to the asking node. We can precalculate all the distances by running a bfs one more time. In this case we will push the nodes into the queue which are belong to the shortest paths and initialize the distance of these nodes as 0. This is also called Multisource bfs .

Code:


    #include<bits/stdc++.h>
    using namespace std;
    #define pb push_back
    #define highest(x) numeric_limits<x>::max()
    #define IOS ios::sync_with_stdio(false);
    const int maxn = 500005;
    vector<int>adj[maxn];
    int st[2][maxn],vis[maxn];
    ///solution
    void bfs(int index, int source){
        for(int i=0;i<maxn;i++){st[index][i]=highest(int);vis[i]=0;}
        st[index][source]=0;
        queue<int>q;
        vis[source]=1;
        q.push(source);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for(auto it:adj[u]){
                if(vis[it]==0){
                    vis[it]=1;
                    st[index][it]=st[index][u]+1;
                    q.push(it);
                }
            }
        }
    }
    void solution(){
        int n,m,q;
        cin>>n>>m>>q;
        for(int i=0;i<m;i++){
            int u,v;
            cin>>u>>v;
            adj[u].pb(v);
            adj[v].pb(u);
        }
        int s,t;
        cin>>s>>t;
        ///from source
        bfs(0,s);
        ///from destination
        bfs(1,t);
        int dist[maxn];
        for(int i=0;i<maxn;i++)dist[i]=highest(int);
        queue<int>path;
        for(int i=1;i<=n;i++){
            //cout << i << " "<< st[0][i] << " "<< st[1][i] << endl;
            if(st[0][i]+st[1][i]==st[1][s]){
                path.push(i);
                dist[i]=0;
            }
        }
        //cout << endl;
        while(!path.empty()){
            int u=path.front();
            path.pop();
            for(auto it:adj[u]){
                if(dist[it]==highest(int)){
                    dist[it]=dist[u]+1;
                    path.push(it);
                }
               // cout << dist[it] << " "<< it << endl;
            }
        }
        while(q--){
            int x;
            cin>>x;
            cout << dist[x] << endl;
        }
    }
    signed main()
    {
        IOS
        solution();
       
return 0;
    }





 

No comments:

Post a Comment

If you have any doubts, let me know through comments

Monkey Banana Problem lightoj 1004

  In this problem we will check which adjacent cell will benefits the monkey best. For this, we will check all possible solution of this pro...