PAT TOP 1024 Currency Exchange Centers (35)

问题描述:

1024 Currency Exchange Centers (35 分)

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

 

典型的最小生成树变形问题,直接用Kruskal算法带走就行。

注意到,题目所要求的换金所的数目最小,可以转化为,在Kruskal算法选择下一个换金所时,当有多个费用相同的换金所时,尽量选择之前已经选择过的换金所。

(懒懒的博主今天又看了一天直播,而且明天准备还出去玩。。。)

AC代码:

#include<bits/stdc++.h>
using namespace std;
vector<int> v;
set<string> se;
struct edge
{
	int v1;
	int v2;
	string s;
	int c;
	bool operator< (const edge& ee) const
	{
		if(c!=ee.c)
		return c>ee.c;
		else
		{
			auto it=se.find(s);
			return it!=se.end()?false:true;
		}
	}
} e;
struct cmp
{
	bool operator()(const edge& e1,const edge& e2) const
	{
		if(e1.s!=e2.s)
		return e1.s<e2.s;
		else
		return e1.c<e2.c;
	}
};
inline int fr(int i)
{
	int ii=i;
	for(;v[i]!=-1;i=v[i]);
	if(ii!=i)
	v[ii]=i;
	return i;
}
int main()
{
//	freopen("data.txt","r",stdin);
	int n,m,k1,k2;
	char a[4];
	scanf("%d %d",&n,&m);
	v.resize(n,-1);
	priority_queue<edge> pq;
	for(;m--;)
	{
		scanf("%d %d",&e.v1,&e.v2);
		scanf("%s",a);
		e.s=a;
		scanf("%d",&e.c);
		pq.push(e);
	}
	vector<edge> ve;
	m=0;
	for(;ve.size()<n-1;)
	{
		e=pq.top();
		pq.pop();
		k1=fr(e.v1);
		k2=fr(e.v2);
		if(k1!=k2)
		{
			v[max(k1,k2)]=min(k1,k2);
			se.emplace(e.s);
			ve.emplace_back(e);
			m+=e.c;
		}
	}
	sort(ve.begin(),ve.end(),cmp());
	printf("%d %d\n",se.size(),m);
	for(auto i:ve)
	printf("%d %d %s %d\n",i.v1,i.v2,i.s.c_str(),i.c);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值