感觉是因为自己的能力不够,这三题真的写了很久也还没思路。对C++也不是很熟悉,真的尽力了。有大佬有思路欢迎交流。
用ChatGPT把图片转换成文本,方便阅读。明天思考一下来写代码。
There are n
servers in the network arranged in ascending order of their capacity. In the array capacity
, the capacity of the ith server is capacity[i]
, where 0 ≤ i < n
.
The distance between two servers, i and j, is defined as the absolute difference in their capacities: |capacity[i] - capacity[j]|
. For each server i, the closest server j is the one with the smallest distance to i, and this closest server is unique.
To manage the network, the following operations can be done to server x:
|capacity[x] - capacity[y]|
units.Given m
queries, each defined by two integers fromServer[i]
and toServer[i]
, find the minimum cost required to connect from fromServer[i]
to toServer[i]
for each query. The connection can be either direct or routed through a server z.
Note:
capacity
array are distinct.n = 3, m = 3
capacity = [2, 7, 10]
fromServer = [0, 1, 2]
toServer = [2, 2, 1]
Starting server | Ending server | Path | Cost |
---|---|---|---|
0 | 2 | 0 → 1 → 2 | 1 + 1 = 2 |
1 | 2 | 1 → 2 | 1 |
2 | 1 | 2 → 1 | 1 |
|2 - 7| < |2 - 10|
|7 - 10| < |7 - 2|
|10 - 7| < |10 - 2|
下面是我的理解。
这题看起来是在有向图里找最短路径,可以用Dijkastra算法。只不过有一个特殊之处,那就是距离最近的两个节点之间的权重会被固定成1. 我觉得可以在构建有向图的过程中把这个特殊要求算进去,也就是说如果距离是最近的,就把权重设为1. 然后再用Dijkastra算法去求最短路径。
让大语言模型写了一个C++的版本,凑合着看看吧。之后有空我会来写点测试。
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <limits>
#include <unordered_map>
using namespace std;
const int INF = numeric_limits<int>::max();
// Build the graph with special closest-server rule
vector<vector<pair<int, int>>> buildGraph(const vector<int>& capacity) {
int n = capacity.size();
vector<vector<pair<int, int>>> graph(n);
for (int i = 0; i < n; ++i) {
int minDiff = INF, closest = -1;
// Find closest server to i
for (int j = 0; j < n; ++j) {
if (i == j) continue;
int diff = abs(capacity[i] - capacity[j]);
if (diff < minDiff) {
minDiff = diff;
closest = j;
}
}
for (int j = 0; j < n; ++j) {
if (i == j) continue;
int cost = abs(capacity[i] - capacity[j]);
if (j == closest) cost = 1; // Apply discount
graph[i].push_back({j, cost});
}
}
return graph;
}
// Dijkstra from one source
int dijkstra(int n, int src, int dst, const vector<vector<pair<int, int>>>& graph) {
vector<int> dist(n, INF);
dist[src] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
pq.push({0, src});
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if (u == dst) return d;
for (auto [v, cost] : graph[u]) {
if (dist[u] + cost < dist[v]) {
dist[v] = dist[u] + cost;
pq.push({dist[v], v});
}
}
}
return dist[dst];
}
int main() {
// Example input
vector<int> capacity = {2, 7, 10};
vector<int> fromServer = {0, 1, 2};
vector<int> toServer = {2, 2, 1};
int n = capacity.size();
auto graph = buildGraph(capacity);
for (int i = 0; i < fromServer.size(); ++i) {
int cost = dijkstra(n, fromServer[i], toServer[i], graph);
cout << "Cost from server " << fromServer[i]
<< " to server " << toServer[i] << " = " << cost << endl;
}
return 0;
}