Reducing Delivery Cost (Codeforces 1433G)
Tags: Others, shortest paths

You are a mayor of Berlyatov. There are $$$n$$$ districts and $$$m$$$ two-way roads between them. The $$$i$$$-th road connects districts $$$x_i$$$ and $$$y_i$$$. The cost of travelling along this road is $$$w_i$$$. There is some path between each pair of districts, so the city is connected.There are $$$k$$$ delivery routes in Berlyatov. The $$$i$$$-th route is going from the district $$$a_i$$$ to the district $$$b_i$$$. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district $$$a_i$$$ to the district $$$b_i$$$ to deliver products.The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with $$$0$$$).Let $$$d(x, y)$$$ be the cheapest cost of travel between districts $$$x$$$ and $$$y$$$.Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with $$$0$$$. In other words, you have to find the minimum possible value of $$$\sum\limits_{i = 1}^{k} d(a_i, b_i)$$$ after applying the operation described above optimally.

Input: The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 \le n \le 1000$$$; $$$n - 1 \le m \le min(1000, \frac{n(n-1)}{2})$$$; $$$1 \le k \le 1000$$$) — the number of districts, the number of roads and the number of courier routes.The next $$$m$$$ lines describe roads. The $$$i$$$-th road is given as three integers $$$x_i$$$, $$$y_i$$$ and $$$w_i$$$ ($$$1 \le x_i, y_i \le n$$$; $$$x_i \ne y_i$$$; $$$1 \le w_i \le 1000$$$), where $$$x_i$$$ and $$$y_i$$$ are districts the $$$i$$$-th road connects and $$$w_i$$$ is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.The next $$$k$$$ lines describe courier routes. The $$$i$$$-th route is given as two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le n$$$) — the districts of the $$$i$$$-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).

Output: Print one integer — the minimum total courier routes cost you can achieve (i.e. the minimum value $$$\sum\limits_{i=1}^{k} d(a_i, b_i)$$$, where $$$d(x, y)$$$ is the cheapest cost of travel between districts $$$x$$$ and $$$y$$$) if you can make some (at most one) road cost zero.

Note: The picture corresponding to the first example:There, you can choose either the road $$$(2, 4)$$$ or the road $$$(4, 6)$$$. Both options lead to the total cost $$$22$$$.The picture corresponding to the second example:There, you can choose the road $$$(3, 4)$$$. This leads to the total cost $$$13$$$.

Sample Inputs:
6 5 2
1 2 5
2 3 7
2 4 4
4 5 2
4 6 8
1 6
5 3

5 5 4
1 2 5
2 3 4
1 4 3
4 3 7
3 5 2
1 5
1 3
3 3
1 5

Sample Outputs:
22

13

Solution (Python):
(n, m, k) = map(int, input().split())
inf = 1000001
g = [[] for _ in range(n)]
e = []
for i in range(m):
    (a, b, w) = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append((b, w))
    g[b].append((a, w))
    e.append((a, b))

def f(s, g):
    d = [inf] * n
    d[s] = 0
    q = [s]
    i = 0
    while i < len(q):
        (w, v) = divmod(q[i], n)
        i += 1
        if d[v] != w:
            continue
        for (u, w) in g[v]:
            if d[v] + w < d[u]:
                d[u] = d[v] + w
                q.append(u + n * d[u])
    return d
p = [f(s, g) for s in range(n)]
(t, s) = (0, [0] * len(e))
for i in range(k):
    (x, y) = map(int, input().split())
    if x == y:
        continue
    x -= 1
    y -= 1
    t += p[x][y]
    for (i, (a, b)) in enumerate(e):
        d = min(p[x][a] + p[b][y], p[x][b] + p[a][y])
        if p[x][y] > d:
            s[i] += p[x][y] - d
print(t - max(s))

Solution (Python):
(n, m, k) = map(int, input().split())
inf = 1000001

def ReadGraph():
    g = [[] for _ in range(n)]
    e = []
    for i in range(m):
        (a, b, w) = map(int, input().split())
        a -= 1
        b -= 1
        g[a].append((b, w))
        g[b].append((a, w))
        e.append((a, b))
    return (g, e)

def Dijkstra(s, g):
    d = [inf] * n
    d[s] = 0
    q = [s]
    i = 0
    while i < len(q):
        (w, v) = divmod(q[i], n)
        i += 1
        if d[v] != w:
            continue
        for (u, w) in g[v]:
            if d[v] + w < d[u]:
                d[u] = d[v] + w
                q.append(u + n * d[u])
    return d

def FindEdge():
    (g, e) = ReadGraph()
    p = [Dijkstra(s, g) for s in range(n)]
    s = [0] * len(e)
    t = 0
    for i in range(k):
        (x, y) = map(int, input().split())
        if x == y:
            continue
        x -= 1
        y -= 1
        t += p[x][y]
        for (i, (a, b)) in enumerate(e):
            d = min(p[x][a] + p[b][y], p[x][b] + p[a][y])
            if p[x][y] > d:
                s[i] += p[x][y] - d
    return t - max(s)
print(FindEdge())

Solution (Python):
def ri():
    return int(input().strip())

def rl():
    return list(map(int, input().strip().split()))
(n, m, k) = rl()
e = [[] for i in range(n)]
dp = [[] for i in range(n)]
for i in range(m):
    (x, y, z) = rl()
    x -= 1
    y -= 1
    e[x].append((y, z))
    e[y].append((x, z))

def SPFA(s):
    que = [s]
    head = 0
    vis = [0] * n
    dis = [int(1000000000.0)] * n
    dis[s] = 0
    while head < len(que):
        x = que[head]
        head += 1
        vis[x] = 0
        for (y, z) in e[x]:
            if dis[y] > dis[x] + z:
                dis[y] = dis[x] + z
                if vis[y] == 0:
                    que.append(y)
                    vis[y] = 1
    dp[s] = dis
for i in range(n):
    SPFA(i)
p = [rl() for i in range(k)]
ans = 1e+18
for x in range(n):
    for (y, z) in e[x]:
        res = 0
        for (a, b) in p:
            a -= 1
            b -= 1
            res += min(dp[a][b], min(dp[a][x] + dp[y][b], dp[a][y] + dp[x][b]))
        ans = min(ans, res)
print(ans)