Tangled in cables (Kruskal+map container processing string)

Source: Internet
Author: User

/** Test Instructions:     gives you the road between two cities (no map) to find the necessary      cables. If it is greater than the supplied, the output not enough  ...      otherwise the cable length required for the output.       input:n  (given total cable length)            m1  (number of cities-)           str1           str2          str3           str4           :          ; (name of the city)      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;M2 (equivalent to M2 edge)            a  b  c  (a city to B city Distance is c)            :          :          :          :      the minimum length required for the output:              if it is greater than the given length     Output  Not enough cable  Analysis:  simple minimum spanning tree + string processing           Map Container mapping process simple   */   #include <stdio.h> #include <map> #include < algorithm> #include <string> #include <string.h> #include <iostream>using namespace  std;const int max=1e4;///1*10  's 4-Time Square int vest[max];int n,m;double leng=0;map< String,int >mp;struct node{    int u,v;    double  w;} &NBSP;BIAN[MAX];BOOL&NBSP;&NBSP;CMP (node a,node b) {&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;A.W&LT;B.W;} Void init (int n) {    for (int i=0; i<=n; i++)         &nBsp;vest[i]=i;} Int find (int t) {        if (vest[t]==t) return t;      return  find (Vest[t]);  }bool merge (int a,int b) {     int x=find (a);     int y=find (b);     if (x!= Y)     {        vest[x]=y;         return true;    }    return false;} Double kruskal () {    double sum=0;    int cnt=0;     for (int i=0; i<m; i++)     {         if (merge (BIAN[I].U,BIAN[I].V))         {             sum+=bian[i].w;             ++cnt;        }         if (cnt>=n-1) Break;    }    return  sum;} Int main () {    scanf ("%lf", &leng);    {          mp.clear (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &n);         init (n);         char a[25];        int k=1;         for (int i=0; i<n; i++)         &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", a);             if (mp[a]==0) mp[a]=k++;///to the first occurrence of the city number          &NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &m);         char b[25];        double c;         for (int i=0; i<m; i++)       &NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s%s%lf", A, B , &c);            bian[i].u=mp[a];             bian[i].v=mp[b];             bian[i].w=c;        }         sort (BIAN,BIAN+M,CMP);         double sum=kruskal ();         if (Sum>=leng) printf ("Not  enough cable\n ");        else printf ("need %.1lf miles of cable\n", sum);     }    return 0;}

Tangled in cables (Kruskal+map container processing string)

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.