✅C++ || Bellman Ford || Easy Explanation || 🗓️ Daily LeetCoding Challenge May, Day 14
Please Upvote If It Helps\n\n\nclass Solution {\npublic:\n    int networkDelayTime(vector<vector<int>>& times, int n, int k) \n    {\n        // just created a 
**Please Upvote If It Helps**\n\n```\nclass Solution {\npublic:\n    int networkDelayTime(vector<vector<int>>& times, int n, int k) \n    {\n        // just created a dis vector that will contain the distance for reaching nodes\n        // and make dik[k] initially 0 because we will start dearching from k \n        vector<int>dis(n+1,INT_MAX);\n        dis[k]=0;\n        \n        // we are using here bellman ford algorithm\n        // we will update our dis vector upto (n-1) times according to bellman\'s ford algo\n        for(int i=1;i<n;i++)\n        {\n            // just checking for all the nodes that are available in our graph\n            for(int j=0;j<times.size();j++)\n            {\n                // extracting the information form the current value\n                int u=times[j][0];     // source\n                int v=times[j][1];     // destination\n                int w=times[j][2];     // weight\n                 \n                // current source is not finite value then we can not pick that path \n                \n                // so just check current source is finite or not\n                // and we are having better path for reaching V from U by using W\n                if(dis[u]!=INT_MAX && dis[u]+w<dis[v])\n                {\n                    // if we are having better path available then just update our dis\n                    dis[v]=w+dis[u];\n                }\n            }\n        }\n\t\t \n        // take out the best value to reach n from k\n        int ans=0;\n        for(int i=1;i<=n;i++)\n        {\n            // If any node is not reachable from k\n            if(dis[i]==INT_MAX) \n                return -1;  \n            \n            // else take best\n            ans=max(ans,dis[i]);\n        }\n        return ans;\n        \n    }\n};\n```
[]