hdu 1301 Jungle Roads
http://acm.hdu.edu.cn/showproblem.php?pid=1301
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 500
using namespace std;
const int inf=<<; int g[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int n,x,mm,sum;
char ch,ch1; void prim()
{
memset(vis,false,sizeof(vis));
for(int i=; i<n; i++) dis[i]=g[][i];
dis[]=;
vis[]=true;
for(int i=; i<n; i++)
{
int m=inf,x;
for(int y=; y<n; y++) if(!vis[y]&&dis[y]<m) m=dis[x=y];
vis[x]=true;
sum+=m;
for(int y=; y<n; y++) if(!vis[y]&&dis[y]>g[x][y]) dis[y]=g[x][y];
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==) break;
getchar();
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
if(i==j) g[i][j]=;
else g[i][j]=inf;
}
}
for(int i=; i<=n-; i++)
{
scanf("%c %d%*c",&ch,&x);
for(int j=; j<x; j++)
{
scanf("%c %d%*c",&ch1,&mm);
g[ch-'A'][ch1-'A']=g[ch1-'A'][ch-'A']=mm;
}
}
sum=;
prim();
printf("%d\n",sum);
}
return ;
}
hdu 1301 Jungle Roads的更多相关文章
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- Hdu 1301 Jungle Roads (最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...
- hdu 1301 Jungle Roads krusckal,最小生成树,并查集
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...
- POJ 1251 & HDU 1301 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 + HDU 1301 Jungle Roads 【最小生成树】
题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解 Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...
- 最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...
- HDOJ 1301 Jungle Roads
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 //HDOJ1301 #include<iostream>#include<c ...
随机推荐
- WebApi限制IP地址请求
, ); } } } ? true : false; } ...
- ASP.NET MVC 3 Razor Nested foreach with if statements
You need to write code this way. @Html.Raw("<tr>") Copy the below code and paste it ...
- HDU_2030——统计文本中汉字的个数
Problem Description 统计给定文本文件中汉字的个数. Input 输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本. Output 对于每一段文本,输出其中的汉 ...
- HDOJ 2191
多重背包. 模版. #include <iostream> #include <stdio.h> #include <stdlib.h> #include < ...
- openstacks
- java--对象比较器
在实际的项目中,经常会遇到排序问题,对于基本数据类型java支持Arrays.sort()和Collection.sort()对集合进行排序,但是对用户自定义类型排序呢?java给我们提供了两种解决方 ...
- CentOS 设置mysql的远程访问
好记性不如烂笔头,记录一下. 安装了MySQL默认是拒绝远程连接的. 首先进入数据库,使用系统数据库mysql. mysql -u root -p mysql #回车,然后输入则使用了系统数据库 接着 ...
- String path = request.getContextPath();这段什么用
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+ ...
- Android设备中实现Orientation Sensor(图)兼谈陀螺仪
设备中的三自由度Orientation Sensor就是一个可以识别设备相对于地面,绕x.y.z轴转动角度的感应器(自己的理解,不够严谨).智能手机,平板电脑有了它,可以实现很多好玩的应用,比如说指南 ...
- Java基础知识强化57:经典排序之希尔排序(ShellSort)
1. 希尔排序的原理: 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该方法因DL.Shell于1959年提出 ...