题目描述:

有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. Window Live Writer在Win7下安装提示错误“OnCatalogResult:0x80190194”

    1.在C:\Users\All Users\Microsoft\WLSetup\Logs下(All Users默认是隐藏文件夹),有两个Window Live的安装日记记录文件,打开第一个找到ERRO ...

  2. [Xcode 实际操作]四、常用控件-(4)UILabel文本标签的自动换行

    目录:[Swift]Xcode实际操作 本文将演示标签控件的换行功能, 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class Vie ...

  3. 课堂笔记 layout 布局、手风琴accordion、选项卡tabs

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. Java基础笔记(七)—— 成员变量、静态变量、局部变量

    public class Test { int c; //成员变量(实例变量) static int s1; //静态变量(类变量)(全局变量) public static void main(Str ...

  5. 洛谷2018寒假集训tg第二次比赛第二题Princess Principal题解

    这算不算泄题啊...被kkk发现会咕咕咕吧. 题目大意:给定一个数列a,与常数n,m,k然后有m个询问,每个询问给定l,r.问在a[l]到a[r]中最少分成几段,使每段的和不超过k,如果无解,输出Ch ...

  6. Flask&&人工智能AI --1

    Flask初识,Response三剑客,jsonify以及send_file.Request,模板语言 Jinja2,用户登录例子,内置Sessio 一.Flask初识 首先,要看你学没学过Djang ...

  7. Tomcat从socket到java Servlet

    整体架构图 一. 启动阶段 BootStrap的main方法加载server.xml配置文件,封装成Server,Service,Connector,Engine等java对象 Server初始化== ...

  8. 配置IIS Web服务器

    配置IIS Web服务器 1.1 控制面板中找到“程序”并打开 1.2 程序界面找到“启用或关闭Windows功能”并打开 1.3 上面两步也可以简化为一步:按[Win + R]快捷键打开运行对话框, ...

  9. @Inherited:允许子类继承父类的注解。

    在看定义注解的相关文章的时候,看到这个@Inherited注解,简单的说明并没有真正搞懂是什么意思.在网上搜索了一些相关的内容,现在把一篇文章转载过来.以便后面使用. 文章出处,转载地址:(http: ...

  10. centos服务器nginx相关命令

    1.找到nginx路径: ps aux | grep nginx -> /usr/local/nginx/sbin/nginx 2.nginx配置检查: /usr/local/nginx/sbi ...