-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSSP.cpp
More file actions
88 lines (80 loc) · 2.73 KB
/
Copy pathSSSP.cpp
File metadata and controls
88 lines (80 loc) · 2.73 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
#include <iostream>
#include <vector>
#include <set>
#include <iterator>
#include <queue>
using namespace std;
class Edge{
public:
int destination;
int weight;
Edge(int A, int B){
destination = A;
weight = B;
}
};
class Node
{
public:
int nodeIndex;
int precessor;
int distance;
int isComplete;
Node(){
distance = -1;
isComplete = 0;
}
bool operator<( Node const &B) const{
return distance > B.distance;
}
bool operator==(Node const &B) const{
return nodeIndex == B.nodeIndex;
}
};
int main(void){
int nodes, edges, destinations, source, terminal;
cin >> nodes >> edges >> destinations >> source;
while(nodes != 0 || edges != 0 || destinations != 0|| source != 0){
vector < vector<Edge> > graph(nodes);
for(int i = 0; i < edges; i++){
int A, B, weight;
cin >> A >> B >> weight;
graph[A].push_back(Edge(B, weight));
}
priority_queue<Node> minDistTree;
vector<Node> completedNodes(nodes);
int startingPoint = source;
completedNodes[startingPoint].distance = 0;
completedNodes[startingPoint].precessor = startingPoint;
completedNodes[startingPoint].isComplete = 1;
for(int i = 0; i < destinations; i ++){
cin >> terminal;
while(completedNodes[terminal].isComplete == 0){
vector<Edge>::iterator it;
int currentDist = completedNodes[startingPoint].distance;
for(it= graph[startingPoint].begin(); it != graph[startingPoint].end(); it++){
int destPoint = it->destination;
if(completedNodes[destPoint].distance == -1 || completedNodes[destPoint].distance > currentDist + it->weight){
completedNodes[destPoint].distance = currentDist + it->weight;
completedNodes[destPoint].precessor = startingPoint;
completedNodes[destPoint].nodeIndex = destPoint;
minDistTree.push(completedNodes[destPoint]);
}
}
if(minDistTree.empty()){
break;
}
startingPoint = minDistTree.top().nodeIndex;
completedNodes[startingPoint].isComplete = 1;
minDistTree.pop();
}
if(completedNodes[terminal].isComplete == 1)
cout << completedNodes[terminal].distance << endl;
else
cout << "Impossible" << endl;
}
cin >> nodes >> edges >> destinations >> source;
if(nodes != 0 || edges != 0 || destinations != 0|| source != 0)
cout << endl;
}
}