-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFloyd_Warshal.cpp
69 lines (65 loc) · 1.5 KB
/
Floyd_Warshal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Floyd Warshal code
Written by: Ankush Mitra
Date: 28.10.9 Time: 4.03 P.M*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
map<string,int> Encoder;
int main()
{
ll no_of_node,no_of_edges:
cin>>no_of_node>>no_of_edges;
count=0;
while(no_of_node--)
{
string node;
cin>>node;
Encoder[node]=count;//Encode the string to some numeric value
count=count+1;
}
int ** AdjacencyMatrix=new int*[no_of_node];
for(int i=0;i<no_of_node;i++)
{
AdjacencyMatrix[i]=new int [no_of_node];
}
for(int i=0;i<no_of_node;i++)
{
for(int j=0;j<no_of_node;j++)
{
AdjacencyMatrix[i][j]=INT_MAX;
}
}
for(int i=0;i<no_of_node;i++)
{
AdjacencyMatrix[i][i]=0;//Diagonal Element distance is 0
}
while(no_of_edges--)
{
string sou,des;
ll weight;
cin>>sou>>des>>weight;
AdjacencyMatrix[Encoder[sou]][Encoder[des]]=weight;// Making the adjacency matrix
AdjacencyMatrix[Encoder[des]][Encoder[sou]]=weight;// for undirected graph
}
for(ll k=0;k<no_of_node;k++)
{
for(ll i=0;i<no_of_node;i++)
{
for(ll j=0;j<no_of_node;j++)
{
if(AdjacencyMatrix[i][j]>AdjacencyMatrix[i][k]+AdjacencyMatrix[k][j])
{
AdjacencyMatrix[i][j]=AdjacencyMatrix[i][k]+AdjacencyMatrix[k][j];
}
}
}
}
int no_of_test;
cin>>no_of_test;
while(no_of_test--)
{
string sou,des;
cin>>sou>>des;
cout<<AdjacencyMatrix[Encoder[sou]][Encoder[des]]<<endl;
}
}