/** 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<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)