Skip to content

1024 Currency Exchange Centers

Statement

Metadata

  • 作者: CHEN, Yue
  • 单位: 浙江大学
  • 代码长度限制: 16 KB
  • 时间限制: 250 ms
  • 内存限制: 64 MB

There are currently 168 internationally recognized unique national currencies in this world. But let us assume that we have business with other species in the entire universe… To change one currency to another, we need currency exchange centers, and they charge fees for it. Now given a list of informations of these centers, you are supposed to find a collection of the centers so that we can make change between any two currencies (directly or indirectly) through them. Since such a kind of collection may not be unique, you must find the one with the minimum total fees; and if there are still more than one solution, find the one with the minimum number of centers – it is guaranteed that such a solution is unique.

Input Specification

Each input file contains one test case. For each case, the first line gives two positive integers N and M (both no more than 10^4), being the total numbers of currencies and the size of the list of currency exchange center informations, respectively. Then M lines follow, each describes a piece of information in the following format:

C1 C2 Center Fee
where C1 and C2 are the indices (from 0 to N-1) of the two currencies; Center is a string of 3 capital English letters representing the name of a center; and Fee is a positive integer no more than 10^4. It is guaranteed that at most one exchange center is given for each pair of different currencies.

Output Specification

Print in the first line the total number of currency exchange centered being collected, and the total amount of fees they charge, separated by a space. Then in the following lines, print the centers which must be collected in the same format as the input. The centers must be output in alphabetical order of their names, and if there is a tie, in ascending order of their fees. It is guaranteed that such a solution exists and is unique.

Sample Input

6 9
4 3 CBC 32
1 5 HSB 43
1 0 HSB 32
0 2 CTB 28
4 2 CBC 19
2 3 CBC 28
0 4 ABC 28
1 2 ABC 32
3 1 CTB 19

Sample Output

3 137
4 2 CBC 19
2 3 CBC 28
3 1 CTB 19
0 2 CTB 28
1 5 HSB 43

Solution

#include <bits/stdc++.h>
using namespace std;
using pII = pair<int, int>;
#define fi first
#define se second
#define SZ(x) (int(x.size()))
const int N = 1e4 + 10;

set<string> se;
int n, m;
struct E {
    string name;
    int u, v, w;
    bool operator<(const E &other) const {
        if (w == other.w)
            return se.find(name) == se.end();
        return w > other.w;
    }
} e[N];

struct UFS {
    int fa[N];
    void init() {
        memset(fa, 0, sizeof fa);
    }
    int find(int x) {
        return fa[x] == 0 ? x : fa[x] = find(fa[x]);
    }
    bool merge(int x, int y) {
        x = find(x), y = find(y);
        if (x != y)
            return fa[x] = y, true;
        return false;
    }
} ufs;

void out(pII it, vector<E> &vec) {
    cout << it.fi << " " << it.se << endl;
    for (auto &it : vec) {
        cout << it.u << " " << it.v << " " << it.name << " " << it.w << "\n";
    }
}

void gao() {
    vector<E> vec;
    ufs.init();
    int tot = 0;
    priority_queue<E> pq;
    for (int i = 1; i <= m; ++i) pq.push(e[i]);
    while (!pq.empty()) {
        E e = pq.top();
        pq.pop();
        if (ufs.merge(e.u, e.v)) {
            tot += e.w;
            vec.push_back(e);
            se.insert(e.name);
        }
    }
    sort(vec.begin(), vec.end(), [&](E x, E y) {
        if (x.name != y.name)
            return x.name < y.name;
        return x.w < y.w;
    });
    out(pII(SZ(se), tot), vec);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        cin >> e[i].u >> e[i].v >> e[i].name >> e[i].w;
    }
    gao();
    return 0;
}

Last update: May 4, 2022
Back to top