Topic 1417: Variant King Kong

Source: Internet
Author: User

Title Description:

People who have seen Transformers must remember a scene in which robots can heaven and hushed when attacking humans.

The wise man soon thought that it would be possible to use the underground offensive to deal with robots. Soon, humans built several bases underground. Now there is a need for cooperation between these bases, and there must be a channel between the bases, so that it is much easier to transport supplies or communicate. It is a good way to establish a channel between every two bases, and communication between the bases becomes extremely convenient. However, it is also necessary to take into account the manpower, material and time spent in repairing the underpass. The time is now very tight and some channels must be chosen for construction. According to the analysis and study of the topography, humans have identified some alternative passages suitable for construction. Believe that smart you will soon think that these channels are not all built, as long as the construction of some channels, so that any two of the base between each other can be reached. After the passage has been repaired, there is one last job to be done, which is to lay a railroad track between each repaired channel, which costs the same length as the channel. At the same time, with the reality of the railway, the underground railway can only buy a fixed length of rail each time, each channel with a rail, if the length of the rail is greater than the length of the channel, the rest can be discarded. Now humans will determine the number of alternative channels, each connecting the two base names, and the length of this channel tells you. What is the minimum length of this batch of rails to be purchased to meet the requirements?

Input:

The first line of input includes the number of bases N (1<=n<=100), and the number of alternative channels m (1<=m<=10000).

the next M-line, each line represents an alternative channel, which includes two strings base1 and Base2, representing the names of two bases (the length of the string 1<=len<=100), and an integer w (1<=w<=10000000). Represents the distance between the two bases.  

Output:

The minimum length required for the output of this batch of rails purchased. If the alternative channel given in the title, no matter how you choose, can not make any of the two base between each other, then please output "My God" can be.

Sample input:
5 8a B 2a C 3b D 4a D 2b C 1d E 3a e 2c e-5a C 3a D 2d E 3a E 2c e 5
Sample output:
2My God
Tips:

1. Due to the different construction schemes, the distance between the two bases given in the data may be multiple, whichever is the shortest.




--------------------------------------------------------------------------------------------------------------- -----------

Thought: Because the input is a string, you should combine the string with the tree array subscript, so using map is just right. The problem is normal when the "name" of the base is changed from a string to an integral type.

Code:
#include <iostream>#include<algorithm>#include<map>using namespacestd;structstation{stringbase1; stringBase2; intint_base1; intInt_base2; intCost ;}; Map<string,int>Basetransfer; StationBase[10010];inttree[ the]; voidInittree (intlength) {     for(intI=0; i<=length;++i) tree[i]=-1;} intFind_root (intx) {    if(tree[x]==-1)        returnx; Else{        inttmp=Find_root (tree[x]); TREE[X]=tmp; returntmp; }} BOOLCMP (station a,station b) {returna.cost<=B.cost;} intMain () {intN,m,index;  while(cin>>n>>m)        {basetransfer.clear (); Index=1;  for(intI=0; i<m;++i) {cin>>Base[i].base1>>Base[i].base2>>Base[I].cost; if(Basetransfer.find (Base[I].BASE1] = =basetransfer.end ()) Basetransfer.insert (Make_pair (Base[i].base1,index++)); if(Basetransfer.find (Base[I].base2] = =basetransfer.end ()) Basetransfer.insert (Make_pair (Base[i].base2,index++)); }         for(intI=0; i<m;++i) {            Base[i].int_base1=basetransfer[Base[I].BASE1]; Base[i].int_base2=basetransfer[Base[I].BASE2]; }        if(Basetransfer.size () < (unsignedint) (n) {cout<<"My God"<<Endl; Continue;        } inittree (n); Sort (Base,Base+m,cmp); intMaxlength=0;  for(intI=0; i<m;++i) {            intRoot_a=find_root (Base[I].INT_BASE1]; intRoot_b=find_root (Base[I].INT_BASE2]; if(root_a!=root_b) {Tree[root_a]=Root_b; if(Base[i].cost>maxlength) maxlength=Base[I].cost; }        }        intCnt=0;  for(intI=1; i<=n;++i)if(tree[i]==-1)                ++CNT; if(cnt==1) cout<<maxlength<<Endl; Elsecout<<"My God"<<Endl; }   return 0;} /************************************************************** problem:1417 User:lcyvino language:c++ Re sult:accepted time:330 Ms memory:3832 kb****************************************************************/



Topic 1417: Variant King Kong

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.