-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Souravirus/master
added floyd warshal algorithm
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |