<pre name="code" class="cpp"><pre name="code" class="cpp">#include<cstdio> #include<iostream> #define inf 88888888 using namespace std; int map[500][500],path[505]; int n; void prim(int x) { int dis[101],i,j,k,minn; for(i=1;i<=n;i++) {dis[i]=map[i][x];path[i]=x;} for(i=1;i<=n-1;i++) { minn=inf; for(j=1;j<=n;j++) if((dis[j]!=0)&&(dis[j]<minn)) {minn=dis[j];k=j;}; dis[k]=0; for(j=1;j<=n;j++) if(dis[j]>map[j][k]) {dis[j]=map[j][k];path[j]=k;} } } int main(){ int i,j,x,y,len,tot=0,m; char ch; cin>>n; for(i=1;i<=n;i++)//初始化 for(j=1;j<=n;j++) if(i!=j)map[i][j]=inf; for(i=1;i<=n-1;i++)//init { cin>>ch;scanf("%d",&m); x=int(ch)-int('A')+1; for(j=1;j<=m;j++) { cin>>ch;scanf("%d",&len); y=int(ch)-int('A')+1; map[x][y]=len; map[y][x]=len; } } prim(1); for(i=1;i<=n;i++) { if(path[i]!=i) { tot=tot+map[i][path[i]]; } } cout<<tot; }