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-Day4 Python基础进阶之生成器/迭代器/装饰器/Json & pickle 数据序列化
一.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面 ...
- elipse+pydev+python开发arcgis脚本程序
环境配置参考:http://www.cnblogs.com/halfacre/archive/2012/07/22/2603848.html 添加arcpy类库.arctoolbox.arcgis-b ...
- docker 感性介绍
Docker 允许开发者们将他们的应用打包放在云端的“容器”中,无需再修改就可以发布到任何流行的 Linux 机器上.由于采用沙盒机制,各应用之间没有任何接口,所以不用担心它们会相互干扰.也同样因为这 ...
- 升级 CentOS git 1.7.1 到 1.7.12
CentOS 源里的 git 版本是 1.7.1,如果远程创建的库所用 git 的版本比它高,在 pull 的时候,如果本地有修改,就会永久阻塞:在 push 的时候就会失败. CentOS 源里的 ...
- floor舍去法取整
$int = 0.99999999999999999; echo floor($int); // returns 1 $int = 0.9999999999999999; echo floor($in ...
- php字符串函数和数组函数
/验证码$str="abcdefghijklmnopqrstuvwxyz0123456789";$a=substr($str,rand(0,35),1);$b=substr($st ...
- cxf简单实例
CXF是一个基于 Servlet 技术的 SOA 应用开发框架,简单来说,就是WebService的轻量级实现. 1.下载开发包:http://cxf.apache.org/download.html ...
- [转载]非常完善的Log4net详细说明
前言 此篇文章是我见过写得最好的一片关于Log4Net的文章,内容由简入难,而且面面俱到,堪称入门和精通的佳作,特从懒惰的肥兔的转载过来. 1.概述 log4net是.Net下一个非常优秀的开源日志记 ...
- C# XML - XmlDocument
http://msdn.microsoft.com/zh-cn/library/System.Xml.XmlDocument(v=vs.80).aspx 1.member http://msdn.mi ...
- Maven--(一个坑)在settings.xml文件中添加mirrors导致无法新建Maven项目
这是用新电脑第一次创建Maven项目--当然是一个测试项目.已经差不多忘了该怎样做,所以参考我的博客:http://www.cnblogs.com/wql025/p/4996486.html,这应该是 ...