题目描述:

有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. vue 绑定属性(index)

    <el-menu-item v-for="item in links" :key="item.id" v-bind:index="item.id ...

  2. 利用css实现鼠标经过元素,下划线由中间向两边展开

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  3. KONG 安装 (在 CentOS 7 中)

    1. 下载安装包:  https://bintray.com/kong/kong-community-edition-rpm/download_file?file_path=centos/7/kong ...

  4. Nginx + Tomcat7 + redis session一致性问题

    Nginx 作负载均衡时,由于是每次都需要把请求分发到不同的机器,同一个用户在一台机器上创建了 session,下一次的请求很有可能会转发到另外一台机器,会造成 session 丢失.我们可以使用 R ...

  5. c# 衍生类和基类的构造顺序

    public class MyDeriveClass :MyBaseClass { public MyDeriveClass() :base() { } int derive_int = 1; } p ...

  6. 浮点数与快速log2

    请先于浮点数的文章:http://blog.jobbole.com/86371/ 先贴一张关于float和double的图: float: double: 快速log2长这样: int flog2(f ...

  7. 2017-10-17 NOIP模拟赛

    Reverse #include<iostream> #include<cstdio> #include<cstring> using namespace std; ...

  8. web综合案例04

    web综合案例02 web综合案例02 web综合案例04 待补充 ... ...

  9. AT2161 シャッフル / Shuffling

    传送门 其实有一个显然的性质嘛:对于每个数,其实只要考虑它最右能被换到的位置就好了 然后设\(f[i][j]\)表示已经处理完了前\(i-1\)位,当前还有\(j\)个\(1\)可以自由支配(注意这里 ...

  10. git教程1-gitlab部署

    https://about.gitlab.com/install/#centos-7 https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/ gitla ...