POJ1251(Kruskal水题)
https://vjudge.net/problem/POJ-1251
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.
Input
Output
Sample Input
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output
216
30
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#define maxn 30
#define ms(x,n) memset(x,n,sizeof x);
const int inf=0x3f3f3f3f;
using namespace std;
int parent[maxn],ranks[maxn];
int V,E;
int finds(int x)
{
if(x!=parent[x])
return parent[x]=finds(parent[x]);
}
void init(int n)
{
for(int i=;i<n;i++)
parent[i]=i,ranks[i]=;
}
void unite(int x,int y)
{
x=finds(x),y=finds(y);
if(x==y)return;
if(ranks[x]>ranks[y])
parent[y]=x;
else
{
parent[x]=y;
if(ranks[x]==ranks[y])
ranks[y]++;
}
}
struct node
{
int u,v,w;
node(int uu,int vv,int ww){u=uu,v=vv,w=ww;}
node(){u=,v=,w=inf;}
}edge[maxn*maxn];
bool cmp(node a,node b)
{
return a.w<b.w;
}
int kruskal()
{
sort(edge,edge+E,cmp);
init(V);
int ans=;
for(int i=;i<E;i++)
{
node a=edge[i];
if(finds(a.u)!=finds(a.v))
{unite(a.u,a.v);
ans+=a.w;
}
}
return ans;
}
int main()
{
int k,w;
char u,v;
while(~scanf("%d",&V),V)
{
E=;
for(int i=;i<maxn;i++)
edge[i]=node();
for(int i=;i<V-;i++)
{
cin>>u>>k;
while(k--)
{
cin>>v>>w;
edge[E++]=node(u-'A',v-'A',w);
}
}
cout<<kruskal()<<endl;
}
return ;
}
题目大意:
给定相互联通的边和权值,求最小生成树。
思路:
因为是稀疏图,推荐用Kruskal。
注意点:
在这道题中学会了给结构体的对象设初值node(){u=0,v=0,w=inf;}和赋值node(int uu,int vv,int ww){u=uu,v=vv,w=ww;}。
POJ1251(Kruskal水题)的更多相关文章
- POJ 水题(刷题)进阶
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- ACM :漫漫上学路 -DP -水题
CSU 1772 漫漫上学路 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
- [poj2247] Humble Numbers (DP水题)
DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...
- gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,
1195: 相信我这是水题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 821 Solved: 219 Description GDUT中有个风云人 ...
- BZOJ 1303 CQOI2009 中位数图 水题
1303: [CQOI2009]中位数图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2340 Solved: 1464[Submit][Statu ...
- 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题
B - 大还是小? Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Description 输入两个实数,判断第一个数大 ...
- ACM水题
ACM小白...非常费劲儿的学习中,我觉得目前我能做出来的都可以划分在水题的范围中...不断做,不断总结,随时更新 POJ: 1004 Financial Management 求平均值 杭电OJ: ...
随机推荐
- 浏览器与Tomcat交互
浏览器与Tomcat交互 Web开发者都知道在Tomcat下部署应用后启动Tomcat即可通过浏览器与Tomcat建立连接. 那么二者之间的连接建立过程是怎么样的呢?(在此,我们不具体讲述关于网络底层 ...
- blfs(systemd版本)学习笔记-配置远程访问和管理lfs系统
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 要实现远程管理和配置lfs系统需要配置以下软件包: 前几页章节脚本的配置:https://www.cnblogs.com/ren ...
- Callable接口--有返回值的线程
Callable java5之前是没有返回值的,Java5新增了Callable接口获得线程的返回值,可返回值的任务必须实现Callable接口,类似的,无返回值的任务必须Runnable接口.Cal ...
- 英文技术Podcast推荐 - 英语技术一起学
Podcast(播客)是现在比较流行的音.视频RSS订阅媒体.跟大家分享一下我所关注的一些不错的英文技术podcast,大家感兴趣可以订阅,在关注国外最前沿的技术资讯的同时更加锻炼英文听力(有很多需要 ...
- loadrunner 场景设计-IP Spoofer-多ip负载生成器(Windows平台)
IP Spoofer-多ip负载生成器 by:授客 QQ:1033553122 1 适用协议 LoadRunner的多ip功能允许运行在单一负载生成器上的Vuser可以通过多ip被识别.服务器和路由 ...
- Google Chrome 下载&绿化&增强
Chrome下载 Google Chrome 已经可以在线更新,虽然比较慢! 国内常用的更新地址有两处:chromedownloads 和 shuax(耍下): https://www.chromed ...
- Android事件总线(三)otto用法全解析
前言 otto 是 Square公司发布的一个发布-订阅模式框架,它基于Google Guava 项目中的event bus模块开发,针对Android平台做了优化和加强.虽然Square已经停止了对 ...
- 百度纯CSS生成菜单
首页我们打看dreamweaver或其它编辑器,创建一个名为nav的导航菜单 <div class="nav"> <ul> <li><a ...
- 计算机硬件基本知识及Linux的常用命令
------------------1. 计算机硬件基本知识------------------ CPU - 寄存器 - L1/L2/L3 - 内存 - 硬盘 - 互联网下载/其他存储介质传输 寄存器 ...
- oracle启动的三个阶段
startup nomount 时,数据库状态为 started; alter database mount 时, 状态为 mounted; alter database open 时,状态为 ope ...