Minimum spanning tree bare title, lazy to write, direct template
#include <iostream> #include <vector> #include <cmath> #include <cstdio> #define INF 1<<30 #define MAXN 30using namespace Std;int N; Vector<int>mapp[maxn];int vaule[maxn][maxn];int visit[maxn];void Prim () {int D[maxn];fill (d,d+maxn,inf); Fill ( visit,visit+maxn,0); int Re=0;d[0]=0;while (1) {int v=-1;for (int i=0;i<n;i++) {if (!visit[i]&& | D[I]<D[V]) v=i;} if (v==-1) break;re+=d[v];visit[v]=1;for (int i=0;i<mapp[v].size (); i++) {int x=mapp[v][i];d [X]=min (d[x],vaule[v][ x]);}} Cout<<re<<endl;} int main () {while (cin>>n&&n) {char a,b;int m,x,z;for (int. i=0;i<maxn;i++) Mapp[i].clear (); for (int i=0; i<n-1;i++) {cin>>a>>m;for (int i=0;i<m;i++) {cin>>b>>z;int x=a-' a ', y=b-' a '; mapp[x]. Push_back (y); mapp[y].push_back (x); vaule[x][y]=z;vaule[y][x]=z;}} Prim ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1301 Jungle Roads