poj1251 Jungle Roads
来源:互联网 发布:各算法时间复杂度 编辑:程序博客网 时间:2024/05/01 13:25
题目链接: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;}
阅读全文
0 0
- poj1251 Jungle Roads
- poj1251 - Jungle Roads
- POJ1251-Jungle Roads
- poj1251 Jungle Roads
- poj1251 Jungle Roads
- POJ1251 Jungle Roads(kru)
- poj1251 Jungle Roads
- Jungle Roads(POJ1251)(primer)
- POJ1251 Jungle Roads
- POJ1251 Jungle Roads
- HDU1301&&POJ1251 Jungle Roads
- POJ1251 Jungle Roads
- poj1251 Jungle Roads
- poj1251 Jungle Roads
- poj1251 Jungle Roads
- POJ1251 Jungle Roads Prim算法
- poj1251 hdu 1301 Jungle Roads
- POJ1251- Jungle Roads(Kruskal)
- IIS下开启php扩展失效?
- 解决request中文乱码
- Java虚拟机:类加载
- mysql数据库初次登录时无法登录问题以及修改密码的问题
- python学习笔记--推导式(Comprehension)
- poj1251 Jungle Roads
- linux增加新的磁盘空间 采用lvm方式
- STM32标准外设库(有收获有进步)
- 无穷小放飞互联网,第一本袖珍电子书
- hexo博客安装RSS插件
- linux 11
- poj1753 Flip Game
- GemFire 8.2 在CentOS的安装步骤
- shell变量