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

本文探讨了在一个假设的宇宙环境中,如何构建最小的货币兑换网络,以便于在各种外星货币间进行兑换。通过使用最小生成树算法,文章提供了一种解决方案,确保网络覆盖所有货币类型的同时,费用达到最低。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值