-
Notifications
You must be signed in to change notification settings - Fork 5
/
ShortestPath.cpp
73 lines (60 loc) · 1.45 KB
/
ShortestPath.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
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include "FibHeap.h"
using namespace fib_heap;
using namespace std;
#define INFI 1e9
typedef int Vertex; /* vertices are numbered from 0 to MaxVertexNum-1 */
typedef int WeightType;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
Vertex Vnum, Enum; // The number of vertexes and edges
cout << "Please input the information of the DAG, firstly the number of vertex and edges"
<< " then the information of edges, finally the start point" << endl;
cin >> Vnum >> Enum;
vector<vector<pair<int, int>>> G(Vnum);
for (int i = 0; i < Enum; i++){
Vertex start, end;
WeightType weight;
cin >> start >> end >> weight;
G[start].emplace_back(end, weight);
}
vector<FibHeapNode*> ptr(Vnum, nullptr);
FibHeap fibheap;
int Startid;
cin >> Startid;
for (int i = 0; i < Vnum; i++){
if(i == Startid){
ptr[i] = fibheap.insert(0);
ptr[i]->id = i;
continue;
}
ptr[i] = fibheap.insert(INFI);
ptr[i]->id = i;
}
int cnt = 0;
vector<bool> vis(Vnum, 0);
vector<int> dis(Vnum, INFI);
while(cnt < Vnum){
int v = fibheap.m_minNode->id;
int Dis = fibheap.extract_min();
dis[v] = Dis;
vis[v] = 1;
for (auto [end, weight]: G[v]){
if(!vis[end]){
if(weight + Dis < ptr[end]->key){
fibheap.decrease_key(ptr[end], weight + Dis);
}
}
}
cnt++;
}
for (int i = 0; i < Vnum; i++){
cout << dis[i] << " \n"[i == Vnum - 1];
}
return 0;
}