-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3970.cpp
More file actions
42 lines (42 loc) · 1.69 KB
/
Copy path3970.cpp
File metadata and controls
42 lines (42 loc) · 1.69 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
class Solution {
public:
int hash(int node, int label, int cnt) {
return node * 26 * 51 + label * 51 + cnt;
}
int shortestPath(int n, vector<vector<int>>& edges, string labels, int k) {
vector<vector<pair<int, int>>> graph(n);
for (auto& edge : edges) {
graph[edge[0]].push_back({edge[1], edge[2]});
}
unordered_map<int, int> mp;
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;
pq.push({0, 0, labels[0] - 'a', 1}); // cost, node, label, k
mp[hash(0, labels[0] - 'a', 1)] = 0;
while (!pq.empty()) {
vector<int> info = pq.top();
pq.pop();
int cost = info[0];
int node = info[1];
int label = info[2];
int cnt = info[3];
if (node == n - 1) return cost;
for (auto& neighborInfo : graph[node]) {
int neighbor = neighborInfo.first;
int neighborCost = neighborInfo.second;
int neighborLabel = labels[neighbor] - 'a';
if (neighborLabel == label && cnt + 1 > k) continue;
int newCost = cost + neighborCost;
int newCnt = 1;
if (neighborLabel == label) {
newCnt = cnt + 1;
}
int minCost = INT_MAX;
if (mp.count(hash(neighbor, neighborLabel, newCnt))) minCost = mp[hash(neighbor, neighborLabel, newCnt)];
if (newCost >= minCost) continue;
pq.push({newCost, neighbor, neighborLabel, newCnt});
mp[hash(neighbor, neighborLabel, newCnt)] = newCost;
}
}
return -1;
}
};