-
Notifications
You must be signed in to change notification settings - Fork 13
/
FlyodWarshall(Dj).cpp
117 lines (112 loc) · 1.86 KB
/
FlyodWarshall(Dj).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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cmath>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <fstream>
#define PII pair<int,int>
#define PIVLLD pair<int, vector<long long int>
#define PIVI pair<int, vector<int> >
#define PIS pair<int, string>
#define VI vector<int>
#define VLLD vector<long long int>
#define VS vector<string>
#define OUT(it,x) for(auto (it)=(x).begin();(it)!=(x).end();(it)++) cout<<*(it)<<" "; cout<<"\n"
#define POUT(it,Begin,End) for(auto (it)=(Begin);(it)!=(END);(it)++) cout<<*(it)<<" "; cout<<"\n"
#define FOR(i,b,e) for(int (i)=(b); (i)<=(e); (i)++)
#define RFOR(i,b,e) for(int (i)=(b); (i)<=(e); (i)--)
#define ALL(x) (x).begin(),(x).end()
#define INF 0xfffffff
using namespace std;
class Graph
{
int v;
int **cost;
public:
Graph(int v)
{
this->v=v;
cost = new int* [v+1];
FOR(i,1,v)
cost[i]=new int[v];
FOR(i,1,v)
{
FOR(j,1,v)
cost[i][j]=INF;
}
}
addCost(int u,int v, int w)
{
cost[u][v]=cost[v][u]=w;
}
void distance();
void print()
{
FOR(i,1,v)
{
FOR(j,1,v)
{
cout<<cost[i][j]<<" ";
}
cout<<"\n";
}
}
};
void Graph::distance()
{
FOR(k,1,v)
{
VI dist(v+1,INF);
vector<bool> visited(v+1,false);
int source=k;
dist[source]=0;
FOR(i,1,v)
{
int max=INF;
FOR(j,1,v)
{
if(dist[j]<max && !visited[j])
{
max=dist[j];
source=j;
}
}
visited[source]=true;
FOR(j,1,v)
{
if(dist[j]>(dist[source]+cost[source][j]) && !visited[j])
{
dist[j]=(dist[source]+cost[source][j]);
}
}
}
FOR(j,1,v)
{
cost[k][j]=cost[j][k]=dist[j];
}
}
}
int main()
{
cout<<"No of vertices\t";
int n;
cin>>n;
Graph g(n);
cout<<"No of edges\t";
int e;
cin>>e;
FOR(i,1,e)
{
int x,y,z;
cin>>x>>y>>z;
g.addCost(x,y,z);
}
g.distance();
g.print();
}