Skip to content

Commit

Permalink
Merge pull request srbcheema1#23 from Souravirus/master
Browse files Browse the repository at this point in the history
added floyd warshal algorithm
  • Loading branch information
srbcheema1 authored Oct 1, 2017
2 parents 4d9c469 + d77f0f3 commit 0bc1393
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions maths/floydWarshal/FloydWarshall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//Floyd Warshall Algorithm for All Pair Shortest Path Problem
#include <bits/stdc++.h>
#define FOR(i,n) for(size_t i=0;i<n;++i)
#define endl '\n'
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;

ll gcd(ll a , ll b){return a==0?b:gcd(b%a,a);}

int main(){
//Floyd Warshall algorithm for all pair shortest path algorithm
//NOTE - Vertices in this are from 0 to V-1. For 1 to V, modify the code!!
//Complexity is O(V*V*V). Thi
int e,v,a,b,weight, floyd_warshall [1000][1000],i,j,k,temp;
cin>>v>>e;
FOR(i,v){
FOR(j,v)
{
if(i!=j)
floyd_warshall[i][j] = 99999999;
else floyd_warshall[i][j] = 0;
}
}

FOR(i,e){
cin>>a>>b>>weight;
floyd_warshall[a][b] = weight;
}

FOR(k,v){
FOR(i,v){
FOR(j,v){
temp = floyd_warshall[i][k] + floyd_warshall[k][j];
if(temp < floyd_warshall[i][j])
floyd_warshall[i][j] = temp;
}
}
}
cout<<floyd_warshall[2][3];
return 0;
}

0 comments on commit 0bc1393

Please sign in to comment.