NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

// C++ code for printing Minimum Cost
// Simple Path between two given nodes
// in a directed and weighted graph
#include <bits/stdc++.h>
using namespace std;

// Define number of vertices in
// the graph and infinite value
#define V 5
#define INF INT_MAX

// Function to do DFS through the nodes
int minimumCostSimplePath(int u, int destination,
bool visited[], int graph[][V])
{

// check if we find the destination
// then further cost will be 0
if (u == destination)
return 0;

// marking the current node as visited
visited[u] = 1;

int ans = INF;

// traverse through all
// the adjacent nodes
for (int i = 0; i < V; i++) {
if (graph[u][i] != INF && !visited[i]) {

// cost of the further path
int curr = minimumCostSimplePath(i,
destination, visited, graph);

// check if we have reached the destination
if (curr < INF) {

// Taking the minimum cost path
ans = min(ans, graph[u][i] + curr);
}
}
}

// unmarking the current node
// to make it available for other
// simple paths
visited[u] = 0;

// returning the minimum cost
return ans;
}

// driver code
int main()
{

// initialising the graph
int graph[V][V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
graph[i][j] = INF;
}
}

// marking all nodes as unvisited
bool visited[V] = { 0 };

// initialising the edges;
graph[0][1] = -1;
graph[0][3] = 1;
graph[1][2] = -2;
graph[2][0] = -3;
graph[3][2] = -1;
graph[4][3] = 2;

// source and destination
int s = 0, t = 2;

// marking the source as visited
visited[s] = 1;

cout << minimumCostSimplePath(s, t,
visited, graph);

return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.