hdu1301 Jungle Roads 基础最小生成树
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
int n, m;
int fa[];
struct node
{
int x, y;
int cost;
}arr[maxn]; void init()
{
for (int i = ; i <= maxn; i++)
{
fa[i] = i;
}
} int find(int x)
{
if (x != fa[x])
{
return find(fa[x]);
}
else
return fa[x];
} bool cmp(node a, node b)
{
return a.cost<b.cost;
} int main()
{
char c1, c2;
int k, c;
while (cin >> n)
{
if (n == ) break;
int j = ; //表示边的数量
init();
for (int i = ; i<n; i++)
{
cin >> c1 >> k;
while (k--)
{
cin >> c2 >> c;
arr[j].x = c1 - 'A';
arr[j].y = c2 - 'A';
arr[j].cost = c;
j++;
}
}
sort(arr, arr + j, cmp); //排序
int ans = ;
for (int i = ; i<j; i++)
{
int fx, fy;
fx = find(arr[i].x);
fy = find(arr[i].y);
if (fx != fy)
{
ans += arr[i].cost;
fa[fy] = fx;
}
}
cout << ans << endl;
}
return ;
}
hdu1301 Jungle Roads 基础最小生成树的更多相关文章
- HDU-1301 Jungle Roads(最小生成树[Prim])
Jungle Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- HDU1301 Jungle Roads
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- POJ1251 Jungle Roads 【最小生成树Prim】
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19536 Accepted: 8970 Des ...
- Jungle Roads(最小生成树)
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- hdu1301 Jungle Roads 最小生成树
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- POJ 1251 Jungle Roads (最小生成树)
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
随机推荐
- LA4043 - Ants(二分图完备最佳匹配KM)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=2044">https://icpcarch ...
- no matching provisioning profiles found
问题:真机连上,执行这个提示. 解决: 项目->targets->Bulid Settings-> 1,Provisioning Profile->选择配置Bundle Ide ...
- Python - scrapy安装中libxml2问题
先到 http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下载下面三个库的 whl,然后用pip install 来安装即可 pycurl,lxml,lib ...
- C++结构体中使用函数与类中使用函数小结
#include <iostream>#include <string.h>using namespace std;struct stud//学生信息结构体{ char ...
- object-c iOS 教程 git for mac
本文转载至 http://blog.csdn.net/u011728347/article/details/10035191 http://rypress.com/tutorials/object ...
- HDU 6044 Limited Permutation 读入挂+组合数学
Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...
- JAVA运行时异常及常见的5中RuntimeExecption
最近在抽时间看面试题,很多面试题都提出了写出java常见的5个运行时异常.现在来总结一下, java运行时异常是可能在java虚拟机正常工作时抛出的异常. java提供了两种异常机制.一种是运行时异常 ...
- Principle of least astonishment
Principle of least astonishment - Wikipedia https://en.wikipedia.org/wiki/Principle_of_least_astonis ...
- CWnd中PreCreateWindow、PreSubclassWindow、SubclassWindow
原文链接:http://blog.chinaunix.net/uid-14607221-id-2794642.html 1. PreCreateWindow: Called by the framew ...
- jdbc navcat for mysql 连不上远程服务器的原因(安全组设置)
如果你权限,防火墙什么都设置好了,但是还是连不上远程数据库, 那么你就必须要看看你的服务器上安全组的设置(很重要) 这里以阿里云为例子(之前用阿里云服务都没设置),现在阿里云的服务器租的时候就要求配置 ...