-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_flights_by_using_graph.cpp
More file actions
125 lines (117 loc) · 2.96 KB
/
06_flights_by_using_graph.cpp
File metadata and controls
125 lines (117 loc) · 2.96 KB
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
118
119
120
121
122
123
124
125
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
struct Graph
{
int data[MAX][MAX];
int vertices;
map<pair<string,string>, int> hash;
void read()
{
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
{
data[i][j] = 0;
}
}
cout << "Enter the number of cities-\n";
cin >> vertices;
string cities[vertices];
for (int i = 0; i < vertices; i++)
{
string city;
cout << "Enter the name of the city-\n";
cin >> cities[i];
}
int edges;
cout << "Enter the number of edges-\n";
cin >> edges;
for (int i = 0; i < vertices; i++)
{
int fuel;
for (int j = 0; j < vertices; j++)
{
if (i != j)
{
cout << "Enter the amount of fuel required for the journey from city " + cities[i] + " to " << cities[j] << endl;
cin >> fuel;
data[i][j] = fuel;
pair<string,string> p = make_pair(cities[i], cities[j]);
hash[p] = fuel;
}
else
{
pair<string,string> p = make_pair(cities[i], cities[j]);
hash[p] = 0;
}
}
}
}
void display()
{
for (int i = 0; i < vertices; i++)
{
for (int j = 0; j < vertices; j++)
{
cout << data[i][j] << " ";
}
cout << endl;
}
}
void check_path(pair<string,string> p)
{
if (hash[p] != 0)
{
cout << "The fuel required from city " << p.first << " to " << p.second << " is " << hash[p] << endl;
}
else
{
cout << "There is no path from city " << p.first << " to " << p.second << endl;
}
}
void is_connected(int source, vector<bool> &visited)
{
visited[source] = true;
for (int i = 0; i < vertices; i++)
{
if (data[source][i] != 0 and visited[i] != true)
{
is_connected(i, visited);
}
}
}
};
int main()
{
Graph g;
g.read();
g.display();
string source, destination;
cout << "Enter the source city-\n";
cin >> source;
cout << "Enter the destination city-\n";
cin >> destination;
pair<string,string> p = make_pair(source, destination);
g.check_path(p);
vector<bool> visited(MAX, false);
g.is_connected(0, visited);
bool flag = true;
for (int i = 0; i < g.vertices; i++)
{
if (visited[i] == false)
{
flag = false;
break;
}
}
if (flag == true)
{
cout << "The graph is connected!" << endl;
}
else
{
cout << "The graph is not connected!" << endl;
}
return 0;
}