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 ...
随机推荐
- <hash命令:显示、添加或清除哈希表>
linux系统下的hash指令: 说明:linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表会记录下这条命令的路径,就相当于缓存一样.第一次执行命令s ...
- JQuery之proxy实现绑定代理
在javascript中,this指代的对象时常会变化,这会造成程序,混乱,一般做法就是先将this保存在一个变量中,就不怕她变了,我们先看一个小例子 var A = function(){ this ...
- mysql命令语句来去除掉字段中空格字符的方法
mysql有什么办法批量去掉某个字段字符中的空格?不仅是字符串前后的空格,还包含字符串中间的空格,答案是 replace,使用mysql自带的 replace 函数,另外还有个 trim 函数. ...
- DATE,DATETIME,DATETIME2等日期时间数据类型
日期范围广 0001-01-01 到 9999-12-31.时间范围广 00:00:00 到 23:59:59.9999999. -----------------DATE --只存储日期 selec ...
- c语言调试开关
上一篇转载的没看懂,参考别人的代码,自己又琢磨了一个调试技巧,挺好用,姑且就叫调试开关吧,欢迎指正!!! /*功能:调试开关 *描述:if条件成立,则打印调试信息,否则不打印() * */ #incl ...
- mouse_driver
1:function.h #ifndef FUNCTION_H#define FUNCTION_H #define DRIVER_FUNCTION_ADD_DEVICE#define DRIVER_F ...
- 关于IOS中的delegate必须知道的事情
当你开始写iOS程式不久,应该开始面对到很多的delegate, 不管是用别人的library或是自己写library,可能都逃不了delegate. 为了怕有些人不知道什么是delegate,在这边 ...
- VBS基础篇 - 数组
数组变量:有时需要向一个单一的变量赋于多个值,那么您可以创建一个可包含一系列值的变量,这种变量被称为数组. '静态数组 ) a()="George" a()="john& ...
- ios的UIImage的两种不同的图片加载方式 tom猫
在ios的UI交互设计时,对图片的处理是难免的:不同的处理方式会对内存有不同的影响: ********************************************************* ...
- 两行代码搞定UITableView无数据无网络显示-b
不知是否有像我一样的,每次写TableView在监听网络和无数据源时逻辑处理提示视图都是一堆代码,很繁琐也很重复的垃圾代码(可能就只有我这样