Untitled
unknown
plain_text
a year ago
2.5 kB
4
Indexable
#include <bits/stdc++.h> using namespace std; typedef long long int ll; #define endl "\n" const double PI = 3.14159265358979; const ll INF = 1e9 + 7; const ll MOD = 1e9 + 7; const ll nax = 2505; const int LOG = 25; int dijkstra(int start, int end, int capacity, int n, vector<pair<int, int> > adj[], vector<int> &prices) { vector<vector<int> > dist(n + 1, vector<int> (capacity + 1, INF)); vector<vector<bool> > vis(n + 1, vector<bool> (capacity + 1, false)); priority_queue<pair<int, pair<int, int> > > pq; dist[start][0] = 0; pq.push({-dist[start][0], {start, 0}}); while(!pq.empty()) { auto topElement = pq.top(); pq.pop(); int currCost = -topElement.first; int currNode = topElement.second.first; int currFuel = topElement.second.second; if (vis[currNode][currFuel]) continue; vis[currNode][currFuel] = true; // 1. go to a neighbour using a route for (auto &v: adj[currNode]) { int neigh = v.first; int pathLength = v.second; if (currFuel >= pathLength) { if (dist[neigh][currFuel - pathLength] > currCost + 0) { dist[neigh][currFuel - pathLength] = currCost + 0; pq.push({-dist[neigh][currFuel - pathLength], {neigh, currFuel - pathLength}}); } } } // 2. refill by 1 unit at currNode if (currFuel < capacity) { if (dist[currNode][currFuel + 1] > currCost + prices[currNode]) { dist[currNode][currFuel + 1] = currCost + prices[currNode]; pq.push({-dist[currNode][currFuel + 1], {currNode, currFuel + 1}}); } } } int ans = INF; for (int i = 0; i <= capacity; i++) { ans = min(ans, dist[end][i]); } return ans; } void solve() { int n, m; cin >> n >> m; vector<pair<int, int> > adj[n + 1]; for (int i = 1; i <= m; i++) { int u, v, d; cin >> u >> v >> d; adj[u].push_back({v, d}); adj[v].push_back({u, d}); } vector<int> prices(n + 1); for (int i = 1; i <= n; i++) { cin >> prices[i]; } int start, end, capacity; cin >> start >> end >> capacity; int minCost = dijkstra(start, end, capacity, n, adj, prices); cout << minCost; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); // int t; cin >> t; while(t--) solve(); return 0; }
Editor is loading...
Leave a Comment