fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef pair<int, int> pii;
  5. const int INF = 1e9;
  6.  
  7. vector<int> dijkstra(int n, vector<int>& capacity, int source, int target) {
  8. vector<int> dist(n, INF); // Stores shortest distance
  9. priority_queue<pii, vector<pii>, greater<pii>> pq; // Min-heap (cost, node)
  10.  
  11. dist[source] = 0;
  12. pq.push({0, source});
  13.  
  14. while (!pq.empty()) {
  15. auto [cost, node] = pq.top();
  16. pq.pop();
  17.  
  18. if (node == target) break; // Early exit if target is reached
  19.  
  20. // Move to next/previous server (direct connection)
  21. if (node > 0) {
  22. int new_cost = cost + abs(capacity[node] - capacity[node - 1]);
  23. if (new_cost < dist[node - 1]) {
  24. dist[node - 1] = new_cost;
  25. pq.push({new_cost, node - 1});
  26. }
  27. }
  28. if (node < n - 1) {
  29. int new_cost = cost + abs(capacity[node] - capacity[node + 1]);
  30. if (new_cost < dist[node + 1]) {
  31. dist[node + 1] = new_cost;
  32. pq.push({new_cost, node + 1});
  33. }
  34. }
  35.  
  36. // Move to closest server (fixed cost = 1)
  37. if (node > 0 && dist[node - 1] > cost + 1) {
  38. dist[node - 1] = cost + 1;
  39. pq.push({cost + 1, node - 1});
  40. }
  41. if (node < n - 1 && dist[node + 1] > cost + 1) {
  42. dist[node + 1] = cost + 1;
  43. pq.push({cost + 1, node + 1});
  44. }
  45. }
  46. return dist;
  47. }
  48.  
  49. int main() {
  50. int n, m;
  51. cin >> n >> m;
  52. vector<int> capacity(n);
  53. for (int i = 0; i < n; i++) cin >> capacity[i];
  54.  
  55. vector<int> fromServer(m), toServer(m);
  56. for (int i = 0; i < m; i++) cin >> fromServer[i] >> toServer[i];
  57.  
  58. for (int i = 0; i < m; i++) {
  59. cout<<"afsdfasfa";
  60. }
  61.  
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0.01s 5288KB
stdin
3,3
2,7,10
0,2,1,2,2,1 
stdout
Standard output is empty