Title Link: Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66965#problem/M
Test instructions: A country in the tropics or something, need to maintain the network, to ask for minimal maintenance costs, is almost the minimum spanning tree meaning.
Ideas: Kruskal solution, there is no bright spot, you can use the map to facilitate the character of the whole type. You can also subtract ' A ' from the character.
Code:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <map
> Using namespace std;
struct edge{int u,v,w;
BOOL operator< (const edge &a) const{return w<a.w;
}}e[900];
Map<char,int> mp;//the character integer int n,tot;
int pre[30];
int find (int x) {int t=x;
while (T!=pre[t]) t=pre[t];
while (x!=t) pre[x]=t,x=pre[x];
return t;
} void Kruskal () {for (int i=0;i<=26;i++) pre[i]=i;
int sum=0,cnt=1;
for (int i=0;i<tot;i++) {int u=e[i].u,v=e[i].v,w=e[i].w;
int Fu=find (U), Fv=find (v);
if (FU==FV) continue;
pre[fu]=fv;sum+=w;cnt++;
if (cnt==n) break;
} cout<<sum<<endl;
} int main () {//freopen ("D:\\in.txt", "R", stdin);
for (int i=0;i<26;i++) mp[' A ' +i]=i;
while (Cin>>n && N) {char ch;
int k,t;
tot=0;
for (int i=0;i<n-1;i++) {cin>>ch>>k; for (int j=0;j<k;j++) {cin>>ch>>t;
E[tot++]= (Edge) {i,mp[ch],t};
}} sort (E,e+tot);
Kruskal ();
} return 0;
}