-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.cpp
178 lines (141 loc) · 3.84 KB
/
dijkstra.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
int g_n;
class Node;
class Edge{
Node *targetNode;
int weight;
public:
Edge(){
targetNode = nullptr;
weight = 0;
}
Edge(Node *p,double weight){
targetNode = p;
this->weight = weight;
}
~Edge(){}
void setWeight(int weight){ this->weight = weight;}
int getWeight(){ return weight;}
void setTargetNode(Node *m){ this->targetNode = m;}
Node *getTargetNode(){ return targetNode;}
};
class Node{
string name = "Node ";
bool seen = false;
int distance;
Edge *v = new Edge[g_n];
static int count;
public:
vector<string> mark;
Node(){
count++;
name = name + to_string(count);
distance = 0;
// for (int i = 0; i < g_n; i ++){
// v[i]= nullptr;
// }
}
Node(string name){
count++;
this->name = name;
distance = 0;
// for (int i = 0; i < g_n; i ++){
// v[i]= nullptr;
// }
}
~Node(){
count--;
mark.clear();
// for (int i = 0; i < g_n; i ++){
// delete v[i];
// }
delete []v;
}
void setNodeName(string m){ this->name = m;}
string getNodeName(){ return name;}
void setDistance(int m){ this->distance = m;}
int getDistance(){ return distance;}
bool getSeen(){ return seen;}
void setSeen(bool seen){ this->seen = seen;}
Edge *getListEdge(){ return v;}
// void addEdge(Node *node,int weight){
// Edge *p = new Edge(node,weight);
// for (int i = 0; i < g_n; i ++){
// if( v[i] != nullptr) v[i] = p;
// break;
// }
// }
};
int Node::count = 0;
Node *readFile(const char *input){
int n;
ifstream f(input);
if(f.fail()){
cout<<"Fail!"<<endl;
exit(0);
}
f>>g_n;
Node *a = new Node[g_n];
for( int i = 0; i < g_n; i++){
Edge *p = a[i].getListEdge();
for(int j = 0; j < g_n; j++,p++){
f>>n;
p->setWeight(n);
p->setTargetNode((a+j));
}
}
f.close();
return a;
}
void Dijkstra(Node *startNode){
startNode->setSeen(true);
int d;
startNode->mark.push_back(startNode->getNodeName());
Edge *p = startNode->getListEdge();
for(int i = 0; i < g_n; i++,p++)
{
if(p != nullptr)
{
d = startNode->getDistance()+p->getWeight();
if(!p->getTargetNode()->getSeen())
{
p->getTargetNode()->setDistance(d);
for(int j = 0 ; j < startNode->mark.size(); j++)
{
p->getTargetNode()->mark.push_back(startNode->mark[j]);
}
Dijkstra(p->getTargetNode());
}
else
{
if(d<p->getTargetNode()->getDistance())
{
p->getTargetNode()->setDistance(d);
p->getTargetNode()->mark.clear();
for(int j = 0 ; j < startNode->mark.size(); j++)
{
p->getTargetNode()->mark.push_back(startNode->mark[j]);
}
Dijkstra(p->getTargetNode());
}
}
}
}
}
int main(int argc, char *argv[]){
Node *a = readFile("input.txt");
//Edge *p = a->getListEdge();
Dijkstra(a);
for(int i = 0; i < g_n; i++){
for (int j = 0; j < a[i].mark.size(); j++){
cout<<a[i].mark[j]<<" ";
}
cout<<endl;
}
delete []a;
return 0;
}