Server#10456

Server (Editorial)

The graph contains up to 10^4 vertices and up to 4 \cdot 10^4 edges, so we store it as an adjacency list. All edge weights are positive, so shortest distances can be computed with Dijkstra’s algorithm.

Problem statement in graph terms. Let denote the length of the shortest path between vertices and . If the server is located in city , then a guest from city c_i experiences a latency of d(c_i, v). The total latency of all guests is

S(v) = d(c_1, v) + d(c_2, v) + ... + d(c_k, v)

We need to find the vertex with the minimum value of . In case of a tie, choose the vertex with the smallest index.

Why we run Dijkstra from the guests' cities. We could run Dijkstra from each candidate vertex and sum up the distances to the guests' cities. However, there are up to 10^4 candidates, and that many runs would be too slow. Note that the graph is undirected, so d(c_i, v) = d(v, c_i). Therefore, a single run of Dijkstra from a guest's city c_i immediately gives this guest's latency for every possible server location. There are at most guests, so runs are enough.

How the sum is accumulated. Let's create an array , initially filled with zeros. The element will store the total latency of all guests if the server is placed at vertex .

Next, for each guest in turn:

  1. Run Dijkstra's algorithm from vertex c_i and obtain the array dist[1..n], where dist[v] = d(c_i, v). This is the latency of the -th guest if the server is located in city .

  2. Iterate over all vertices v = 1, 2, ..., n and perform sum[v] += dist[v]. This adds the -th guest's contribution to the total latency of every possible server location.

After all guests have been processed, each element contains exactly terms: the latencies of all guests to vertex . Thus, sum[v] = S(v).

Choosing the answer. Iterate over the vertices v = 1, 2, ..., n in increasing order of their indices and keep track of the vertex with the minimum sum. The answer is updated only on strict inequality sum[v] < best. Then, if several vertices share the same minimum sum, the first of them, that is, the one with the smallest index, is kept.

The minimum is taken over all vertices, not only over the guests' cities, since the optimal location may be a city where no guests live.

Example

The graph given in the example is as follows:

Run Dijkstra's algorithm from the vertices where the guests reside: , and . For each of these vertices, determine the shortest distances to all other cities.

Next, for each vertex of the graph, compute the sum of distances to all the cities with guests. The minimum sum of distances is , and it is achieved at the vertex with the smallest number .

Therefore, the server should be placed in city . The total distance from it to the cities where the guests reside is 5 + 0 + 8 = 13.

Algorithm implementation

Declare a constant for infinity.

#define INF 0x3F3F3F3F

The structure will store information about an edge: the vertex that the edge leads to, and the weight of the edge.

struct edge
{
  int node, dist;
  edge(int node, int dist) : node(node), dist(dist) {}
};

Declare a comparator for sorting pairs (node, dist) in descending order of the value.

bool operator< (edge a, edge b)
{
  return a.dist > b.dist;
}

Declare an adjacency list to represent the graph.

vector<vector<edge> > g;

The Dijkstra function implements Dijkstra's algorithm. It takes the graph and the starting vertex as input. The return value is an array , where contains the shortest distance from the vertex to vertex .

void Dijkstra(vector<vector<edge> >& g, vector<int>& d, int start)
{
  priority_queue<edge> pq;
  pq.push(edge(start, 0));

  d = vector<int>(n + 1, INF);
  d[start] = 0;

  while (!pq.empty())
  {
    edge e = pq.top(); pq.pop();
    int v = e.node;

    if (e.dist > d[v]) continue;

    for (auto e : g[v])
    {
      int to = e.node;
      int cost = e.dist;
      if (d[v] + cost < d[to])
      {
        d[to] = d[v] + cost;
        pq.push(edge(to, d[to]));
      }
    }
  }
}

The main part of the program. Read the input data.

scanf("%d %d %d", &n, &m, &k);
c.resize(k);
for (i = 0; i < k; i++)
  scanf("%d", &c[i]);

Read the input weighted graph.

g.resize(n + 1);
for (i = 0; i < m; i++)
{
  scanf("%d %d %d", &a, &b, &w);
  g[a].push_back(edge(b, w));
  g[b].push_back(edge(a, w));
}

Run Dijkstra's algorithm from the cities where the guests live.

dp.resize(n + 1);
for (i = 0; i < k; i++)
{
  Dijkstra(g, dist, c[i]);

  for (j = 1; j <= n; j++)
   dp[j] += dist[j];
}

Currently, contains the sum of the shortest distances from vertex to all the cities where the guests are located. The next step is to find the minimum value among the elements of the array and print the corresponding result. The variable stores the smallest vertex number where the server should be placed.

res = INF;
for (i = 1; i <= n; i++)
{
  if (dp[i] < res)
  {
    res = dp[i];
    ptr = i;
  }
}

Print the answer.

printf("%d %d\n", ptr, dp[ptr]);