题目描述:

有n个小岛,其中有的小岛之间没有通路,要修这样一条通路需要花费一定的钱,还有一些小岛之间是有通路的。现在想把所有的岛都连通起来,求最少的花费是多少。

输入:

第一行输入T,代表多少组数据。

第二行输入三个整数n , m , k,分别代表一共有n个小岛,m条路径可供选择,k表示有连通岛的个数。

接下来的m行,每行三个整数p , q , c ,代表建设p到q的通路需要花费的钱为c。

接下的k行,每一行的数据输入如下:先输入Q,代表这个连通分量里面有Q个小岛,然后输入Q个小岛,代表这Q个小岛是连通的。

输出:

输出最少花费为多少,如果无法修建出此路,输出"-1"。


其实这道题属于并查集的入门题目,解法如下:

先用并查集把还在连通的k个分量给合并起来,此时记录好连通分量的个数。然后把Q条路径排一遍序,然后从最小的路径开始枚举,对每一条路径判断起点和终点是否在同一个连通分量内,如果不在的话将这两点合并,连通分量的个数减一,加上修建这条路的花费,如此枚举,知道连通分量的个数为1。如果最后的连通分量大于1,则说明无法连接,输出-1。

可能是解法不太好吧,最后890ms险过,这几天做的并查集都是险过QAQ...

 #include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
using namespace std;
int k , n , m , father[];
struct P{
int start , end , cost;
}p[+]; //路径
bool cmp(P a , P b)
{
return b.cost > a.cost;
}
void init()
{
for(int i = ; i <= n ; i++) {
father[i] = i;
}
}
int getf(int v)
{
return father[v] = (v == father[v]) ? v : getf(father[v]);
}
void merge(int x , int y)
{
int a , b;
a = getf(x);
b = getf(y);
if(a != b)
father[b] = a;
}
int main()
{
int T , i , j;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d",&n,&m,&k);
for(i = ; i <= m ; i++)
scanf("%d %d %d",&p[i].start,&p[i].end,&p[i].cost);
init();
for(i = ; i <= k ; i++) {
int cnt;
scanf("%d",&cnt);
int *tmp = new int[cnt];
for(j = ; j < cnt ; j++) {
scanf("%d",&tmp[j]);
if(j != )
merge(tmp[j-] , tmp[j]);
}
delete []tmp;
}
sort(p+ , p+m+ , cmp);
int sum = ; //连通分量的个数
int ans = ;
for(i = ; i <= n ; i++)
if(father[i] == i)
sum++;
for(i = ; i <= m ; i++) {
if(sum == ) //连通分量的个数为1时,说明这时候已经完成
break;
if(getf(p[i].start) != getf(p[i].end)) {
merge(p[i].start , p[i].end);
ans += p[i].cost;
sum--; //连上一条路后连通分量-1
}
}
if(sum > ) {
printf("-1\n");
} else {
printf("%d\n",ans);
}
}
return ;
}

HDU3371 Connect the Cities的更多相关文章

  1. hdu3371 Connect the Cities (MST)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  3. Connect the Cities(hdu3371)并查集(附测试数据)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. Connect the Cities(MST prim)

    Connect the Cities Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  6. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  7. hdoj 3371 Connect the Cities

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. Connect the Cities(prime)

    Connect the Cities Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  9. Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. Java学习笔记(一)语法

    基本语法 大小写敏感 类名:对于所有的类来说,类名的首字母应该大写 方法名:所有的方法名都应该以小写字母开头.如果方法名含有若干单词,则后面的每个单词首字母大写. 源文件名:源文件名必须和类名相同.当 ...

  2. haml

    创建: 2019/05/23 文档: http://haml.info/docs/yardoc/file.REFERENCE.html 安装  安装 gem "haml"     ...

  3. 用SQL数据库做多表关联应怎样设计库结构20170527

    http://77857.blog.51cto.com/67857/143872/ 多表关联的话表之间必须得存在关系才行呢,这样建立外键约束就行了, 关系表中插入主表的主键做外键. 假设表1学生表st ...

  4. [Django笔记] uwsgi + nginx 配置

    django 和 nginx 通过 uwsgi 来处理请求,类似于 nginx + php-fpm + php 安装nginx 略 安装配置uwsgi pip install uwsgi 回想php- ...

  5. 洛谷P2055 [ZJOI2009]假期的宿舍

    P2055 [ZJOI2009]假期的宿舍 题目描述 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A ...

  6. MCP|DYM|Quantitative mass spectrometry to interrogate proteomic heterogeneity in metastatic lung adenocarcinoma and validate a novel somatic mutation CDK12-G879V (利用定量质谱探究转移性肺腺瘤的蛋白质组异质性及验证新体细胞突变)

    文献名:Quantitative mass spectrometry to interrogate proteomic heterogeneity in metastatic lung adenoca ...

  7. unique within an element

    从tomcat 6 升到 tomcat-7.0.12 jsp页面报: org.apache.jasper.JasperException: /XXX/XXX.jsp(59,55) Attribute ...

  8. EOS多主机多节点环境配置

    本文使用了四台同网段的主机,第一台做为eosio创世用户使用,另外三台做为出块者节点使用,最终实现了EOS多主机多节点的配置.最后EOSIO创世用户不再出块,由选举出来的各个节点轮流出块,下面将介绍具 ...

  9. elasticsearch常用工具及使用

    1. 数据库工具:https://github.com/jprante/elasticsearch-jdbc 2.数据展示:https://github.com/mobz/elasticsearch- ...

  10. ECharts 上手

    一.获取 ECharts 你可以通过以下几种方式获取 ECharts. 从官网下载界面选择你需要的版本下载,根据开发者功能和体积上的需求,我们提供了不同打包的下载,如果你在体积上没有要求,可以直接下载 ...