|
| 1 | +typedef array<int,3> AI3; |
| 2 | +class Solution { |
| 3 | + int visited[100][100]; |
| 4 | + vector<pair<int,int>> next[100]; |
| 5 | +public: |
| 6 | + int electricCarPlan(vector<vector<int>>& paths, int cnt, int start, int end, vector<int>& charge) |
| 7 | + { |
| 8 | + priority_queue<AI3, vector<AI3>, greater<>>pq; // {cost, i, battery} |
| 9 | + pq.push({0, start, 0}); |
| 10 | + int n = charge.size(); |
| 11 | + |
| 12 | + for (auto path: paths) |
| 13 | + { |
| 14 | + next[path[0]].push_back({path[1], path[2]}); |
| 15 | + next[path[1]].push_back({path[0], path[2]}); |
| 16 | + } |
| 17 | + |
| 18 | + while (!pq.empty()) |
| 19 | + { |
| 20 | + auto [cost, cur, battery] = pq.top(); |
| 21 | + pq.pop(); |
| 22 | + if (visited[cur][battery]==1) continue; |
| 23 | + visited[cur][battery] = 1; |
| 24 | + |
| 25 | + if (cur==end) return cost; |
| 26 | + |
| 27 | + for (int b=battery+1; b<=cnt; b++) |
| 28 | + { |
| 29 | + if (visited[cur][b]==1) continue; |
| 30 | + pq.push({cost+(b-battery)*charge[cur], cur, b}); |
| 31 | + } |
| 32 | + |
| 33 | + for (auto [nxt, len]: next[cur]) |
| 34 | + { |
| 35 | + if (battery < len) continue; |
| 36 | + if (visited[nxt][battery-len]) continue; |
| 37 | + pq.push({cost + len, nxt, battery - len}); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return 0; |
| 42 | + } |
| 43 | +}; |
0 commit comments