forked from windynight/InterviewPuzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra_Heap.cpp
More file actions
172 lines (142 loc) · 2.47 KB
/
Copy pathDijkstra_Heap.cpp
File metadata and controls
172 lines (142 loc) · 2.47 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//dijkstra with heap
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
//Heap
struct dis
{
int distant;
int node;
};
//size and array
int HeapSize;
dis Heap[1000000];
void Heap_Up(int position)
{
int father=position>>1;
dis temp=Heap[position];
while (father>0)
{
if (temp.distant<Heap[father].distant) Heap[position]=Heap[father]; else break;
position=father; father= position >>1;
}
Heap[position]=temp;
}
void Heap_Add(dis d)
{
Heap[++HeapSize]=d;
Heap_Up(HeapSize);
}
void Heap_Down(int position)
{
int child = position<<1;
dis temp=Heap[position];
while (child<=HeapSize)
{
if (child<HeapSize && Heap[child+1].distant<Heap[child].distant) child++;
if (Heap[child].distant<temp.distant) Heap[position]=Heap[child]; else break;
position=child; child=position <<1;
}
Heap[position]=temp;
}
dis Heap_Pop()
{
dis top=Heap[1];
Heap[1]=Heap[HeapSize--];
Heap_Down(1);
return top;
}
// 临接链表
struct list
{
// 当前边指向的结点
int targetnode;
// 边权
int weight;
// 下一条边
int nextID;
};
// 当前生成边的数量
int nowedge;
// 每条边
list edges[300000];
// 每个结点的边开始
int edge[50001];
int distant[50001];
bool visited[50001];
const int MaxDis=2000000000;
int N,M;
void dijkstra()
{
int i,j,k;
// init
for (i=1;i<=N;++i)
{
visited[i]=false;
distant[i]=MaxDis;
}
// init
HeapSize=0;
dis temp;
dis addtemp;
temp.distant=0;
temp.node=N;
Heap_Add(temp);
int now;
bool found;
for (i=1;i<=N;++i)
{
if (HeapSize<=0) break;
found=false;
while (true)
{
if (HeapSize<=0) break;
temp=Heap_Pop();
if (!visited[temp.node])
{
found=true;
break;
}
}
if (!found) break;
visited[temp.node]=true;
distant[temp.node]=temp.distant;
if (temp.node==1) break;
now=edge[temp.node];
while (now != 0)
{
j=edges[now].targetnode;
k=edges[now].weight;
if (!visited[j] && distant[temp.node]+k<distant[j])
{
distant[j]=distant[temp.node]+k;
addtemp.distant=distant[j];
addtemp.node=j;
Heap_Add(addtemp);
}
now=edges[now].nextID;
}
}
cout<<distant[1]<<endl;
}
int main()
{
int i,j,k,w;
while (scanf("%d%d",&N,&M)!=EOF)
{
nowedge=0;
for (i=1;i<=N;++i) edge[i]=0;
for (k=1;k<=M;++k)
{
scanf("%d%d%d",&i,&j,&w);
nowedge++;
edges[nowedge].targetnode=i;
edges[nowedge].weight=w;
edges[nowedge].nextID=edge[j];
edge[j]=nowedge;
}
dijkstra();
}
return 0;
}