-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathwormhole Floyd Warshall.cpp
97 lines (83 loc) · 1.99 KB
/
wormhole Floyd Warshall.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
using namespace std;
struct node{
int x, y;
};
int abs(int x){
return (x<0) ? -x : x;
}
int finddist(struct node *first,struct node *second){
int dstx = abs(first->x - second->x);
int dsty = abs(first->y - second->y);
return dstx + dsty;
}
int main(){
int test;
cin >> test;
for(int t=1;t<=test;t++){
int warmholes;
cin>>warmholes;
/* Initialise Code */
int nodes = 2 * warmholes + 2;
struct node *location[nodes];
int cost[nodes][nodes];
for(int i=0;i<nodes;i++){
for(int j=0;j<nodes;j++){
cost[i][j]=-1;
}
}
//source
int sx, sy;
cin>>sx>>sy;
struct node *src=new struct node;
src->x=sx;
src->y=sy;
location[0]=src;
//destination
int dox,doy;
cin>>dox>>doy;
struct node *temp=new node;
temp->x=dox;
temp->y=doy;
location[nodes-1]=temp;
//warmhole
for(int i=0;i<warmholes;i++){
int six, siy;
cin>>six>>siy;
struct node *temp=new node;
temp->x=six;
temp->y=siy;
int dix,diy;
cin>>dix>>diy;
struct node *temp1=new node;
temp1->x=dix;
temp1->y=diy;
int w;
cin>>w;
location[2*i+1]=temp;
location[2*i+2]=temp1;
cost[2*i+1][2*i+2]=w;
cost[2*i+2][2*i+1]=w;
}
// Initialise cost matrix
for(int i=0;i<nodes;i++){
for(int j=0;j<nodes;j++){
if(cost[i][j] == -1){
cost[i][j] = finddist(location[i],location[j]);
}
}
}
// Floyd Warshall
for(int k=0;k<nodes;k++){
for(int i=0;i<nodes;i++){
for(int j=0;j<nodes;j++){
if(i==k||j==k)
continue;
cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
}
}
}
cout << "#" << t << " : " << cost[0][nodes-1] << "\n";
}
return 0;
}