POJ - 1251 Jungle Roads (最小生成树&并查集
#include<iostream>
#include<algorithm>
using namespace std;
int ans=,tot=;
const int N = 1e5;
int f[];
struct ac{
int v,u,w;
}edge[N];
bool cmp(ac a,ac b){
return a.w<b.w;
}
inline int find(int x){
if(x!=f[x])f[x]=find(f[x]);return f[x];
}
inline int join(int x,int y,int w){
int fx=find(x),fy=find(y);
if(fx!=fy){
f[fx]=fy;ans+=w;tot++;
}
}
int main()
{
int n;string a,b;int m,w,cnt=;
while(cin>>n&&n){
//cin.ignore();
ans=tot=cnt=;
for(int i = ;i <=n;++i)f[i]=i;
for(int i = ;i <n;++i){
cin>>a;cin>>m;
while(m--){
cin>>b;cin>>w;
edge[cnt].u=a[]-'A'+,edge[cnt].v=b[]-'A'+,edge[cnt].w=w;
cnt++;
}
}
sort(edge,edge+cnt,cmp);
for(int i = ;i < cnt;++i){
join(edge[i].u,edge[i].v,edge[i].w);
if(tot==n-)break;
}
cout<<ans<<endl;
}
return ;
}
AC代码
https://vjudge.net/problem/POJ-1251
读题读的有点难受,其余很简单
POJ - 1251 Jungle Roads (最小生成树&并查集的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 Jungle Roads - C语言 - Kruskal算法
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题六 最小生成树 POJ 1251 Jungle Roads
题意: 有n个点 每个点上有一些道路 求最小生成树 解释下输入格式 A n v1 w1 v2 w2 A点上有n条边 A到v1权值是w1 A到v2权值是w2 思路: 字符串处理之后跑kruskal求最小 ...
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- POJ1251 Jungle Roads(Kruskal)(并查集)
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23882 Accepted: 11193 De ...
- POJ 1251 Jungle Roads (最小生成树)
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- POJ 1251 Jungle Roads(Kruskal算法求解MST)
题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...
随机推荐
- jquery visible 选择器 语法
jquery visible 选择器 语法 作用::visible 选择器选取每个当前是可见的元素.除以下几种情况之外的元素即是可见元素:设置为 display:none type="h ...
- 计蒜客 window画图
在 Windows 的"画图"工具里,可以绘制各种各样的图案.可以把画图当做一个标准的二维平面,在其上先后绘制了 nn 条颜色互不相同的线段. 输出格式 输出 qq 行,每行一个整 ...
- 【杂题】[ARC070F] Honest Or Unkind【交互】
Description 这是一道交互题 有A+B个人,编号从0~A+B-1,其中有A个人是诚实的,B个人是居心叵测的. 你想知道每个人是诚实的还是居心叵测的. 询问可以用二元组(i,j)表示,代表问编 ...
- MCMC & 贝叶斯
用MCMC做参数估计
- sh_02_del关键字
sh_02_del关键字 name_list = ["张三", "李四", "王五"] # (知道)使用 del 关键字(delete)删除 ...
- cmake 简单操作
实例一: main.c #include <stdio.h> int main( int argc, char *argv[] ) { printf("hello cmake!\ ...
- 15.Python bool布尔类型
Python 提供了 bool 类型来表示真(对)或假(错),比如常见的5 > 3比较算式,这个是正确的,在程序世界里称之为真(对),Python 使用 True 来代表:再比如4 > 2 ...
- postgres 使用psql连接
输入:sudo su - postgres 在bash下输入:psql,下文出现postgres字样 在postgres后:输入:\c 数据库名称,连接数据库 在数据库名#:输入查询语句或者其他sql ...
- MySQL学习笔记(cmd模式下的操作)
1.登入MySQL 1.1 登入MySQL 1.1.1命令如下: C:\Users\zjw>mysql -hlocalhost -uroot -p Enter password: ****** ...
- 191121HTML
一.HTML 1.web server import socket def handle_request(client): buf = client.recv(1024) client.send(by ...