hdu1301Jungle Roads
http://acm.hdu.edu.cn/showproblem.php?pid=1301
最小生成树模板题
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int N=;
struct stu{
int u;
int v;
int w;
}p[N];
int k,t;
int father[N];
int cmp(const void *a,const void *b)
{
return (*(struct stu*)a).w > (*(struct stu*)b).w ?:-;
}
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
int make(int a,int b)
{
int h=;
int f1=find(a);
int f2=find(b);
if(f1>f2)
{
father[f2]=f1;
h=;
}
else if(f1<f2)
{
father[f1]=f2;
h=;
}
return h;
}
int kruskal()
{
int h=;
int s=;
for(int i=;i<k;i++)
{
if(make(p[i].u,p[i].v))
{
h++;
s+=p[i].w;
}
if(h==t-)
return s;
}
return s;
} int main()
{
//freopen("in.txt","r",stdin);
int n,m;
char a,c;
while(~scanf("%d",&t))
{
if(!t)
break;
for(int i=;i<=t;i++)//刚开始t的位置我写的是常量N,就是过不去,我也是醉了!
father[i]=i; //不知道咋回事
k=;
getchar(); for(int i=;i<t;i++)
{
scanf("%c %d",&a,&n);
for(int j=;j<n;j++,k++)
{
scanf(" %c %d",&c,&m);
p[k].u=a-'A'+;
p[k].v=c-'A'+;
p[k].w=m;
}
getchar();
}
qsort(p,k,sizeof(struct stu),cmp);
//for(int i=0;i<k;i++)
//printf("%d %d %d\n",p[i].u,p[i].v,p[i].w);
printf("%d\n",kruskal());
}
return ;
}
hdu1301Jungle Roads的更多相关文章
- HDU--1301--Jungle Roads(最小生成树)
Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
随机推荐
- Python的类变量和对象变量声明解析
Python的类和C++一样,也都是存在两种类型的变量,类变量和对象变量!前者由类拥有,被所有对象共享,后者由每个对象独有.这里我主要想讨论一下他们的声明办法. 首先说的是对象变量: 只要是声明在类的 ...
- 杂项一之js,<select>标签
一.在aspx页面中实现 修改与删除页面的跳转 前台js部分: 在上部的js部分中写,根据传过来的id,来经行页面的跳转,并把id传过去 js部分就是实现了一个页面跳转的功能 (还有确认框confir ...
- Template、ItemsPanel、ItemContainerStyle、ItemTemplate
先来看一张图(网上下的图,加了几个字) 1.Template是指控件的样式 在WPF中所有继承自contentcontrol类的控件都含有此属性,(继承自FrameworkElementdl类的Tex ...
- JS遇到的问题解决
1.input 里使用onclick事件,整整花了1个半小时,onclick里面直接用Location.href不需要加<script></script> <?php / ...
- Hibernate之HQL总结
hibernate运行过程: Hibernate作用 1.hibernate是java应用和关系数据库之间的桥梁,她负责java对象和关系数据库之间的映射. 2.hibernate内部封装了通过JDB ...
- Oracle定义常量和变量
1.定义变量 变量指的就是可变化的量,程序运行过程中可以随时改变其数据存储结构 标准语法格式:<变量名><数据类型>[(长度):=<初始值>] 示例: declar ...
- linux文件目录下各文件简介
/bin:存放最常用命令: /boot:启动Linux的核心文件: /dev:设备文件: /etc:存放各种配置文件: /home:用户主目录: /lib:系统最基本的动态链接共享库: /mnt:一般 ...
- docker 感性介绍
Docker 允许开发者们将他们的应用打包放在云端的“容器”中,无需再修改就可以发布到任何流行的 Linux 机器上.由于采用沙盒机制,各应用之间没有任何接口,所以不用担心它们会相互干扰.也同样因为这 ...
- Java异常的深入研究与分析
前言 本文是异常内容的集大成者,力求全面,深入的异常知识研究与分析.本文由金丝燕网独家撰写,参考众多网上资源,经过内容辨别取舍,文字格式校验等步骤编辑而成,以飨读者.对于本文的内容,建议小白需要多多思 ...
- 微软职位内部推荐-Enterprise Architect - BDE - BJ
微软近期Open的职位: Enterprise ArchitectCloud, HTML5, Big Data and Mobile are technology trends driving pro ...