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 ...
随机推荐
- PHP运行方式对比
文章内容来自以下站点http://www.cnblogs.com/xia520pi/p/3914964.html 关于PHP目前比较常见的五大运行模式: 1.CGI(通用网关接口 / Common G ...
- h5 web模板
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --><html lang="zh-cmn-Hans"> ...
- php编写简单的页面跳转功能
不多说,直接上. //确保magic_quotes_gpc在php.ini中移开启function CheckInput($value){ //去除反斜杠 if(get_magic_quotes_gp ...
- struts2结构图
- Waring:This LinearLayout layout or its FrameLayout parent is useless; transfer the background attribute to the other view
解决方法请参考: You have a single component (row) vertical linear layout, containing another linear layout. ...
- Sublime Text博客插件 --- iblog
iblog是一款 sublime 博客插件,目前只支持cnblog. 项目地址:https://github.com/iskeeter/iblog 功能介绍 新建和更新cnblog的博客 支持mark ...
- Linux驱动开发之开篇--HelloWorld
Linux驱动的编写,大致分为两个过程,第一个过程为测试阶段,即为某一具体的设备,添加必要的驱动模块,为了节省编译时间,需要将代码单独放在一处,在编译时,只需要要调用内核的头文件即可:第二个过程为布置 ...
- pthread属性设置
Posix线程中的线程属性pthread_attr_t主要包括scope属性.detach属性.堆栈地址.堆栈大小.优先级.在pthread_create中,把第二个参数设置为NULL的话,将采用默认 ...
- Fragment inner class should be static
package com.example.fragmenttest; import android.annotation.SuppressLint; import android.app.Activit ...
- OC的@property 和 @synthesize id
学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...