您的位置:首页 > 其它

Uva 6437 - Power Plant 裸最小生成树

2013-12-01 22:49 253 查看
题意:

一个无向图中(至多100个点),..每条边有其费用...有些点是发电站..现在要求所有的点都可以达到至少一个发电站..所需的最小费用..

题解:

先就把发电站的点放到一个集合中..然后裸的kruskal了...

Program:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAXN 205
using namespace std;
struct node
{
       int u,v,d;
}edge[MAXN*MAXN];
int father[MAXN];
int getfather(int x)
{
      if (father[x]==x) return x;
      return father[x]=getfather(father[x]);
}
bool cmp(node a,node b)
{
      return a.d<b.d;
}
int main()
{
      int C,cases,N,K,M,i,u,v,f,ans; 
      scanf("%d",&C);
      for (cases=1;cases<=C;cases++)
      {
               scanf("%d%d%d",&N,&M,&K);
               for (i=1;i<=N;i++) father[i]=i;
               scanf("%d",&f);
               for (i=2;i<=K;i++) scanf("%d",&u),father[u]=f;
               for (i=1;i<=M;i++)
                   scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].d);
               sort(edge+1,edge+1+M,cmp);
               ans=0;
               for (i=1;i<=M;i++)
               {
                        u=edge[i].u,v=edge[i].v;
                        if (getfather(u)==getfather(v)) continue;
                        ans+=edge[i].d;
                        father[father[u]]=father[v];
               }
               printf("Case #%d: %d\n",cases,ans);
      }
      return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: