-
Notifications
You must be signed in to change notification settings - Fork 1
/
743+Network Delay Time_Heap_best.cpp
188 lines (138 loc) · 4.15 KB
/
743+Network Delay Time_Heap_best.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
179
180
181
182
183
184
185
186
187
188
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
const int inf = INT_MAX / 2;
vector<vector<pair<int, int>>> g(n + 1);
for (auto &t : times) {
g[t[0]].emplace_back(t[1], t[2]);
}
vector<int> dist(n + 1, inf);
dist[k] = 0;
vector<bool> visited(n + 1, false);
/*queue*/
auto myCmp = [](const auto& e1, const auto& e2) {
auto&& [to1, w1] = e1;
auto&& [to2, w2] = e2;
return w1 > w2; // 小顶堆 返回的是下沉规则,true则下沉 即值大的下沉
};
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(myCmp)> pq(myCmp);
pq.emplace(k, 0);
while (pq.size()) {
auto p = pq.top(); pq.pop();
int x = p.first;
if (visited[x]) continue;
visited[x] = true;
for (auto &e : g[x]) {
int y = e.first, d = dist[x] + e.second;
if (dist[y] > d) {
dist[y] = d;
pq.emplace(y, d);
}
}
}
int res = *max_element(++dist.begin(), dist.end());
return res == inf ? -1 : res;
}
struct HeapNode {
int u;
int d;
HeapNode(int uu, int dd) : u(uu), d(dd) { }
bool operator<(const HeapNode &node) const {
return d > node.d; // 小顶堆
}
};
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
const int inf = INT_MAX / 2;
vector<vector<pair<int, int>>> g(n + 1);
for (auto &t : times) {
g[t[0]].emplace_back(t[1], t[2]);
}
vector<int> dist(n + 1, inf);
dist[k] = 0;
vector<bool> visited(n + 1, false);
/*queue*/
priority_queue<HeapNode> pq;
pq.emplace(k, 0);
while (pq.size()) {
auto p = pq.top(); pq.pop();
int x = p.u;
if (visited[x]) continue;
visited[x] = true;
for (auto &e : g[x]) {
int y = e.first, d = dist[x] + e.second;
if (dist[y] > d) {
dist[y] = d;
pq.emplace(y, d);
}
}
}
int res = *max_element(++dist.begin(), dist.end());
return res == inf ? -1 : res;
}
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
const int inf = INT_MAX / 2;
vector<vector<int>> g(n + 1, vector<int>(n + 1, inf));
for (auto &t : times) {
g[t[0]][t[1]] = t[2];
}
vector<int> dist(n + 1, inf);
dist[k] = 0;
vector<bool> visited(n + 1, false);
/*queue*/
auto myCmp = [](const auto& e1, const auto& e2) {
auto&& [to1, w1] = e1;
auto&& [to2, w2] = e2;
return w1 > w2; // 小顶堆
};
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(myCmp)> pq(myCmp);
pq.emplace(k, 0);
while (pq.size()) {
auto p = pq.top(); pq.pop();
int x = p.first;
if (visited[x]) continue;
visited[x] = true;
for (int y = 1; y <= n; ++y) {
int d = dist[x] + g[x][y];
if (dist[y] > d) {
dist[y] = d;
pq.emplace(y, d);
}
}
}
int res = *max_element(++dist.begin(), dist.end());
return res == inf ? -1 : res;
}
struct HeapNode {
int u;
int d;
HeapNode(int uu, int dd) : u(uu), d(dd) { }
bool operator<(const HeapNode &node) const {
return d > node.d; // 小顶堆
}
};
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
const int inf = INT_MAX / 2;
vector<vector<int>> g(n + 1, vector<int>(n + 1, inf));
for (auto &t : times) {
g[t[0]][t[1]] = t[2];
}
vector<int> dist(n + 1, inf);
dist[k] = 0;
vector<bool> visited(n + 1, false);
/*queue*/
priority_queue<HeapNode> pq;
pq.push(HeapNode(k, 0));
while (pq.size()) {
auto p = pq.top(); pq.pop();
int x = p.u;
if (visited[x]) continue;
visited[x] = true;
for (int y = 1; y <= n; ++y) {
int d = dist[x] + g[x][y];
if (dist[y] > d) {
dist[y] = d;
pq.push(HeapNode(y, d));
}
}
}
int res = *max_element(++dist.begin(), dist.end());
return res == inf ? -1 : res;
}