HDU 1301 Jungle Roads

Source: Internet
Author: User

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

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.