-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (132 loc) · 3.82 KB
/
main.cpp
File metadata and controls
152 lines (132 loc) · 3.82 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <sstream>
#include <functional>
#include <iomanip>
#include <cstdio>
using namespace std;
struct edge{
unsigned int destination;
double distance;
};
struct vertex{
double distance = INFINITY;
bool visited = false;
unsigned int parent = 0;
bool in_path = false;
vector<edge> neighbors;
};
typedef vector<vertex> Graph;
double dijkstra(Graph &graph, unsigned int source, unsigned int destination)
{
auto cmp = [](pair<unsigned int, double> left, pair<unsigned int, double> right) { return (left.second > right.second);};
priority_queue<pair<unsigned int, double>, vector<pair<unsigned int, double>>, decltype(cmp)> q(cmp);
graph.at(source).distance=0;
q.push(make_pair(source, graph.at(source).distance));
unsigned int current;
while(!q.empty())
{
current = q.top().first;
if (current == destination)
{
return graph.at(current).distance;
}
q.pop();
if (graph.at(current).visited)
{
continue;
}
graph.at(current).visited = true;
for(const edge& e : graph.at(current).neighbors)
{
if(!(graph.at(e.destination).visited))
{
if(graph.at(e.destination).distance > graph.at(current).distance+e.distance)
{
graph.at(e.destination).distance = graph.at(current).distance+e.distance;
graph.at(e.destination).parent = current;
q.push(make_pair(e.destination, graph.at(e.destination).distance));
}
}
}
}
return INFINITY;
}
int main(int argc, char* argv[])
{
if (argc<2)
{
cerr<<"dijkstra 0 123 <drukuj>< data.txt"<<endl;
cerr<<"<drukuj> = 0 lub 1, 1: drukuj ścieżkę (dla kwadratowych grafów o sqrt(n)<200)"<<endl;
return -1;
}
istringstream arg_source(argv[1]);
unsigned int source;
if (!(arg_source >> source))
cerr << "Invalid number " << argv[1] << '\n';
unsigned int destination;
istringstream arg_destination(argv[2]);
if (!(arg_destination >> destination))
cerr << "Invalid number " << argv[2] << '\n';
unsigned int print;
istringstream arg_print(argv[3]);
if (!(arg_print >> print))
cerr << "Invalid number " << argv[3] << '\n';
string line;
getline(cin, line);
stringstream ss(line);
int graph_size;
ss>>graph_size;
Graph graph(graph_size);
/*
wczytanie danych o krawędziach
*/
while(getline(cin, line))
{
stringstream ss(line);
unsigned int a, b;
double d;
ss>>a;
ss>>b;
ss>>d;
edge edge_a;
edge_a.destination=b;
edge_a.distance=d;
graph.at(a).neighbors.push_back(edge_a);
edge edge_b;
edge_b.destination=a;
edge_b.distance=d;
graph.at(b).neighbors.push_back(edge_b);
}
cout<<"Wczytałem."<<endl;
double dist = dijkstra(graph, source, destination);
cout<<"Odległość między #"<<source<<" a #"<<destination<<" wynosi "<<dist<<endl;
int dimension = round(sqrt(graph_size));
cout << setfill(' ') << setw(5);
unsigned int temp = destination;
do {
graph.at(temp).in_path = true;
temp = graph.at(temp).parent;
}while(graph.at(temp).parent != 0);
graph.at(temp).in_path = true;
if (!print)
{
return 0;
}
for (int j=0; j< dimension; j++)
{
for (int i=0; i< dimension; i++)
{
if(graph.at(i+j*dimension).in_path)
{
printf("\033[31m");
}
cout << setfill(' ') << setw(5)<<i+j*dimension;
printf("\033[0m");
}
cout<<endl;
}
return 0;
}