1024 Currency Exchange Centers (35分)(C++)

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

題目大意:給出一些center,這些center可以換貨幣,現在要選出最少的center使得你可以換所有的貨幣。如果有兩種解法就取費用少的那個。

解題思路:最小生成樹

代碼:

#include <bits/stdc++.h>
using namespace std;
unordered_set<string> centers;
struct Edge{
	int c1, c2, fee;
	string id;
	bool operator < (const Edge&a)const{
		return fee > a.fee || (fee == a.fee && centers.find(id) == centers.end());
	}
}; 
vector<Edge> ans;
int n, m, father[10005], sum;
int findfather(int x){
	if(x == father[x])
		return x;
	int a = findfather(father[x]);
	father[x] = a;
	return a;
}
int main(){
	scanf("%d %d", &n, &m);
	iota(father, father+n, 0);
	priority_queue<Edge>edges;
	Edge temp;
	for(int i = 0; i < m; ++ i){
		cin >> temp.c1 >> temp.c2 >> temp.id >> temp.fee;
		edges.push(temp);
	}
	while(!edges.empty()){
		Edge e = edges.top();
		edges.pop();
		int ua = findfather(e.c1), ub = findfather(e.c2);
		if(ua != ub){
			father[ua] = ub;
			ans.push_back(e);
			sum += e.fee;
			centers.insert(e.id);
		}
	}
	sort(ans.begin(), ans.end(), [](Edge a, Edge b){return a.id<b.id || (a.id==b.id&&a.fee<b.fee);});
	printf("%d %d\n", centers.size(), sum);
	for(Edge e : ans)
		printf("%d %d %s %d\n", e.c1, e.c2, e.id.c_str(), e.fee);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章