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 ...
随机推荐
- fpm打包redis3.0.7
1.安装redis tar -xf redis-3.0.7.tar.gz -C /usr/local cd /usr/local && mv redis-3.0.7 redis cd ...
- linux命令详解之chkconfig命令使用方法
介绍一个linux常用命令,chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:c ...
- 数据库操作类util
package util; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; imp ...
- Python开发【第一篇】Python模块中特殊变量
模块中特殊变量 生产环境中,常用的就是__name__和__file__ __doc__ __package__ __cached__ __name__ __file__ 一. __doc__ #获 ...
- Oracle定义常量和变量
1.定义变量 变量指的就是可变化的量,程序运行过程中可以随时改变其数据存储结构 标准语法格式:<变量名><数据类型>[(长度):=<初始值>] 示例: declar ...
- WPF-控件-ControlTemplate生成的控件
<Window x:Class="由ControlTemplate生成的控件.MainWindow" xmlns="http://schemas.microsoft ...
- 【BZOJ 1188】 [HNOI2007]分裂游戏
Description 聪聪和睿睿最近迷上了一款叫做分裂的游戏. 该游戏的规则试: 共有 n 个瓶子, 标号为 0,1,2.....n-1, 第 i 个瓶子中装有 p[i]颗巧克力豆,两个人轮流取豆子 ...
- iPhone OS 开发 - 了解并解决代码签名问题
译者:Jestery 发表时间:2010-04-24浏览量:21082评论数:0挑错数:0 了解并解决代码签名问题 (为保持跟开发环境以及APPLE开发者社区网站结构对应,一些名词未作翻译) 绝大多数 ...
- Ubuntu多系统安装注意事项
1. 安装 选择分区时一定要全设置成逻辑分区,不能是主分区! 2.多系统引导向修复 利用LiveCD制作U盘启动进入Ubuntu系统,若挂载点为: /dev/sda9 swap ...
- 仿微博视频边下边播之滑动TableView自动播放-b
Tips:这次的内容分为两篇文章讲述01.[iOS]仿微博视频边下边播之封装播放器 讲述如何封装一个实现了边下边播并且缓存的视频播放器.02.[iOS]仿微博视频边下边播之滑动TableView自动播 ...