-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkurs.cpp
More file actions
109 lines (80 loc) · 1.97 KB
/
Copy pathkurs.cpp
File metadata and controls
109 lines (80 loc) · 1.97 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
#include <bits/stdc++.h>
#include <string>
#include <fstream>
using namespace std;
#define INF 0x3f3f3f3f
class Graph
{
int V;
list< pair<int, int> > *adj;
public:
Graph(int V);
void addEdge(int u, int v, int w);
void shortestPath(int s);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list< pair<int, int> >[V];
}
void Graph::addEdge(int u, int v, int w)
{
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
void Graph::shortestPath(int src)
{
set< pair<int, int> > setds;
vector<int> dist(V, INF);
setds.insert(make_pair(0, src));
dist[src] = 0;
while (!setds.empty())
{
pair<int, int> tmp = *(setds.begin());
setds.erase(setds.begin());
int u = tmp.second;
list< pair<int, int> >::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
{
int v = (*i).first;
int weight = (*i).second;
if (dist[v] > dist[u] + weight)
{
if (dist[v] != INF)
setds.erase(setds.find(make_pair(dist[v], v)));
dist[v] = dist[u] + weight;
setds.insert(make_pair(dist[v], v));
}
}
}
for (int i = 0; i < V; ++i)
cout << src << " -> " << i << " " << dist[i] << endl;
}
int main()
{
int V;
int max;
string str1,str2,str3,str4;
cout << "enter the number of nodes: ";
cin >> V;
Graph g(V);
const string str;
int node1;
int node2;
int value;
int i;
ifstream file("text.txt");
if(file.is_open())
{
while(file >> str1 >> str2 >> node1 >> str3 >> node2 >> str4 >> value)
{
g.addEdge(node1, node2, value);
}
file.close();
}
else cout << "file is not open" << endl;
cout << "choose the node: ";
cin >> i;
g.shortestPath(i);
return 0;
}