poj1251 Jungle Roads
  DlHu7OdRxfZ9 2023年11月02日 49 0


题目链接:​​http://poj.org/problem?id=1251​​​
题意:看题面的图,求最小生成树
解析:最小生成树裸题,注意一下输入就好

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+100;
struct node
{
int u,v,c;
node() {}
node(int _u,int _v,int _c)
{
u = _u;
v = _v;
c = _c;
}
bool operator < (const node &b)const
{
return c<b.c;
}
}a[maxn];
int fa[maxn];
int getfa(int x)
{
if(fa[x]==x)
return fa[x];
return fa[x] = getfa(fa[x]);
}
int main(void)
{
int n;
while(~scanf("%d",&n)&&n)
{
int cnt = 0,ans = 0;
for(int i=0;i<30;i++)
fa[i] = i;
for(int i=1;i<n;i++)
{
char u,v;
int m,x;
cin>>u>>m;
for(int i=0;i<m;i++)
{
cin>>v>>x;
a[cnt++] = node(u-'A'+1,v-'A'+1,x);
}
}
sort(a,a+cnt);
for(int i=0;i<cnt;i++)
{
int t1 = getfa(a[i].u);
int t2 = getfa(a[i].v);
if(t1!=t2)
{
ans += a[i].c;
fa[t1] = t2;
}
}
printf("%d\n",ans);
}
return 0;
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  wD98WYW8hiWJ   2023年11月20日   21   0   0 #include
  v0MZS93bOvwU   2023年11月02日   36   0   0 #include
  Fv5flEkOgYS5   2023年11月02日   36   0   0 i++javaide
  Mqh2iumZ9USt   2023年11月02日   30   0   0 #includei++ios
DlHu7OdRxfZ9
作者其他文章 更多