hdu 3371
#include<stdio.h>
#include<stdlib.h>
#define N 501
struct node {
int x,y,dis;
}road[N*N];
int pre[N];
int find (int n ) {
return pre[n]=n==pre[n]?n:find(pre[n]);
}
int cmp(const void *a,const void *b) {
return (*(struct node *)a).dis>(*(struct node *)b).dis?1:-1;
}
int main() {
int n,m,k,t,a,b,c,cnt,sum,flag,i,j,len,f1,f2;
scanf("%d",&t);
while(t--) {
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=n;i++)
pre[i]=i;
for(i=0;i<m;i++) {
scanf("%d%d%d",&a,&b,&c);
road[i].x=a;
road[i].y=b;
road[i].dis=c;
}
sum=0;
cnt=0;
qsort(road,m,sizeof(road[0]),cmp);
while(k--) {
scanf("%d%d",&len,&a);
f1=find(a);
len--;
while(len--) {
scanf("%d",&b);
f2=find(b);
if(f1!=f2) {
cnt++;
pre[f2]=f1;
}
}
}
for(i=0;cnt<n&&i<m;i++) {
f1=find(road[i].x);
f2=find(road[i].y);
if(f1!=f2) {
cnt++;
sum+=road[i].dis;
pre[f2]=f1;
}
}
if(cnt==n-1)
printf("%d\n",sum);
else
printf("-1\n");
}
return 0;
}
hdu 3371的更多相关文章
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu 3371(prim算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Jav ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- HDU 3371(城市联通 最小生成树-Kruskal)
题意是求将所有点联通所花费的最小金额,如不能完全联通,输出 -1 直接Kruskal,本题带来的一点教训是 rank 是algorithm头文件里的,直接做变量名会导致编译错误.没查到 rank 的具 ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
随机推荐
- 代码审查的艺术:Dropbox 的故事
Dropbox 的 iOS 应用中的每一行代码,都是开始于被添加到 Maniphest 中的一个 bug 或者功能任务,Maniphest 是我们的任务管理系统.当一位工程师在上面接受一个任务,那么在 ...
- canvas基础绘制-倒计时(上)
效果: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- CocoaPods安装遇到的坑。
//官方推荐地址 CocoaPods :http://code4app.com/article/cocoapods-install-usage cooped的安装 $(inherited) 报pod ...
- PMP项目管理学习笔记(6)——整合管理之制订项目管理计划
制订项目管理计划 输入:项目章程.组织过程资产.企业环境要素.计划过程的输出(): 工具:专家判断 输出:项目管理计划 项目管理计划使你在问题发生之前做出规划 你要在计划过程组中明确如何完成项目——因 ...
- 【HEVC简介】ALF-Adative Loop Filter
由于HEVC在HM4.0之后,就把ALF去掉,所以ALF的介绍是基于AVS2. <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com ...
- Linux环境下手动配置sbt
一.下载sbt安装包 从sbt官网下载地址:http://www.scala-sbt.org/download.html下载安装包,以sbt-0.13.13.tgz为例. 二.安装 1.将下载的二进制 ...
- SQL的top 100 percent用法
sql="select top 30 * from data where title='"&title1&"' order by id desc" ...
- COGS 1361. 树
★ 输入文件:treed.in 输出文件:treed.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 在一个凉爽的夏夜,xth和rabbit来到花园里砍树.为啥 ...
- Unity3D_最简单的开始界面_结束界面
开始界面1.创建一个新的场景添加button 2.C#脚本LoadingGame.cs using System.Collections;using System.Collections.Generi ...
- exportfs - 管理NFS共享文件系统列表
概述 (SYNOPSIS) /usr/sbin/exportfs [-avi] [-o options,..] [client:/path ..] /usr/sbin/exportfs -r [-v] ...