Cod sursa(job #2661144)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 21 octombrie 2020 14:34:36
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
// TEST C++17

#include <bits/stdc++.h>
using namespace std;

class Graph {
  private:
    int n;
    vector<vector<pair<int, int>>> ad;

  public:
    Graph(int n) :
        n(n), ad(n + 1) { }

    void addEdge(int x, int y, int z) {
        ad[x].emplace_back(y, z);
        ad[y].emplace_back(x, z);
    }

    vector<int> dijkstra(int src) {
        vector<int> dp(n + 1, 1e9);
        dp[src] = 0;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        pq.emplace(0, src);
        vector<bool> vis(n + 1);
        while (!pq.empty()) {
            int node = pq.top().second; pq.pop();
            if (!vis[node]) {
                vis[node] = true;
                for (auto [nghb, cost] : ad[node])
                    if (dp[node] + cost < dp[nghb]) {
                        dp[nghb] = dp[node] + cost;
                        pq.emplace(dp[nghb], nghb);
                    }
            }
        }
        return dp;
    }
};

int main() {
    int n, m, k; cin >> n >> m >> k;
    Graph graph(n);
    vector<pair<int, int>> edg(m);
    for (auto& [x, y] : edg) {
        int z; cin >> x >> y >> z;
        graph.addEdge(x, y, z);
    }
    vector<pair<int, int>> pth(k);
    for (auto& [x, y] : pth)
        cin >> x >> y;

    vector<vector<int>> dp(n + 1);
    for (int i = 1; i <= n; i++)
        dp[i] = graph.dijkstra(i);

    int ans = 1e9;
    for (auto [x, y] : edg) {
        int now = 0;
        for (auto [s, t] : pth)
            now += min({dp[s][t], dp[s][x] + dp[y][t], dp[s][y] + dp[x][t]});
        ans = min(ans, now);
    }
    cout << ans << '\n';
    return 0;
}