Jungle Roads hdu1301

来源:互联网 发布:风暴大陆战盾进阶数据 编辑:程序博客网 时间:2024/05/22 08:02

http://acm.hdu.edu.cn/showproblem.php?pid=1301

#include<iostream>//有是一个纯最小生成树的问题 ,一开始题目没有看了,看懂了,原来是这么简单的
#include<algorithm>//题意:输入一个n,n=0时break 在输入n-1行  每行输入大写字母表示村庄和组数k 在输入k行一个字母表示与这个村庄
#include<cmath>    //相连和一个数字表示修路的费用,输入连通全部村庄的最小值
#include<cstdio>
using namespace std;
int s[500];

 struct stu
 {
 // char v[2];
  int x;
  int y;
  int dis;
 } df[100];
 
 int cmp(stu a, stu b)
 {
  return a.dis < b.dis ;
 }
 
 int find (int a)
 {
  int r = a;
  while(r != s[r])
   r = s[r];
  return r;
 }
 
 void merge(int a, int b)
 {
  if(a > b )
   s[a] = b;
  else
   s[b] = a;
   
 }

int main()
{
 int n, m, distance, j, count, k, i;
 char str[2],str1[2];
 while(scanf("%d", &n) != EOF && n != 0)
 {
  count = 0;
  for(i = 1 ; i < n ;i++)
  {
   scanf("%s%d", str, &m);
   while(m--)
   {
    scanf("%s%d",str1, &k);
    df[count].x = (int)(str[0] - 64);
    df[count].y = (int)(str1[0] - 64);
    df[count].dis = k;
    count++;
    
   }
  }
  sort(df, df + count, cmp);
  distance  = 0;
  for(i = 1; i<= 500; i++)
   s[i] = i;
  for(i = 0; i < count; i++)
  {
   int a = find(df[i].x);
   int b = find(df[i].y);
   if(a != b)
   {
    distance += df[i].dis;
    merge(a, b);
   }
  }
  printf("%d/n",distance);
 }
 
}

原创粉丝点击