• Currency Exchange Centers


    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 1), 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 N1) 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 1. 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
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 vector<int> v;
     4 set<string> se;
     5 struct edge
     6 {
     7     int v1;
     8     int v2;
     9     string s;
    10     int c;
    11     bool operator< (const edge& ee) const
    12     {
    13         if(c!=ee.c)
    14         return c>ee.c;
    15         else
    16         {
    17             auto it=se.find(s);
    18             return it!=se.end()?false:true;
    19         }
    20     }
    21 } e;
    22 struct cmp
    23 {
    24     bool operator()(const edge& e1,const edge& e2) const
    25     {
    26         if(e1.s!=e2.s)
    27         return e1.s<e2.s;
    28         else
    29         return e1.c<e2.c;
    30     }
    31 };
    32 inline int fr(int i)
    33 {
    34     int ii=i;
    35     for(;v[i]!=-1;i=v[i]);
    36     if(ii!=i)
    37     v[ii]=i;
    38     return i;
    39 }
    40 int main()
    41 {
    42 //    freopen("data.txt","r",stdin);
    43     int n,m,k1,k2;
    44     char a[4];
    45     scanf("%d %d",&n,&m);
    46     v.resize(n,-1);
    47     priority_queue<edge> pq;
    48     for(;m--;)
    49     {
    50         scanf("%d %d",&e.v1,&e.v2);
    51         scanf("%s",a);
    52         e.s=a;
    53         scanf("%d",&e.c);
    54         pq.push(e);
    55     }
    56     vector<edge> ve;
    57     m=0;
    58     for(;ve.size()<n-1;)
    59     {
    60         e=pq.top();
    61         pq.pop();
    62         k1=fr(e.v1);
    63         k2=fr(e.v2);
    64         if(k1!=k2)
    65         {
    66             v[max(k1,k2)]=min(k1,k2);
    67             se.emplace(e.s);
    68             ve.emplace_back(e);
    69             m+=e.c;
    70         }
    71     }
    72     sort(ve.begin(),ve.end(),cmp());
    73     printf("%d %d
    ",se.size(),m);
    74     for(auto i:ve)
    75     printf("%d %d %s %d
    ",i.v1,i.v2,i.s.c_str(),i.c);
    76     return 0;
    77 
    78 }
  • 相关阅读:
    mysql 7.5.8 服务无法启动 服务没有报告任何错误
    Ubuntu 16.04 php卸载
    函数式编程(3)-匿名函数
    函数式编程(2)-返回函数
    函数式编程(1)-高阶变成(3)-sorted
    函数式编程(1)-高阶变成(2)-filter
    函数式编程(1)-高阶变成(1)-map/reduce
    高级特性(4)-生成器
    高级特性(3)-列表生成式
    高级特性(2)-迭代
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285766.html
Copyright © 2020-2023  润新知