题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3371

题目大意:

给n个城市,m条路,k组已知路,求最小费用联通所有城市;

解题思路:

kruskal求MST,这道题目有毒,很容易超时,改了一下并查集才过,而且同一个代码有时过又是超时

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
int p[maxn];
int tot;
void init()
{
for(int i = ; i < maxn; i++)p[i] = i;
}
int f(int x)
{
return x == p[x] ? x : p[x] = f(p[x]);
}
void Union(int x, int y)
{
x = f(x);
y = f(y);
if(x != y)
{
p[x] = y;//这里改成p[y] = x;有时过,有时会超时
tot--;//不联通的数目减一
}
}
struct edge
{
int u, v, w;
bool operator < (const edge& a)
{
return w < a.w;
}
}a[maxn * maxn];
int main()
{
int T, n, m, k, t, x, y;
scanf("%d", &T);
while(T--)
{
init();
scanf("%d%d%d", &n, &m, &k);
tot = n - ;//最开始,不连通块数目为n-1
for(int i = ; i <= m; i++)
{
scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
}
sort(a + , a + + m);
while(k--)
{
scanf("%d%d", &t, &x);
t--;
while(t--)
{
scanf("%d", &y);
Union(x, y);
}
}
int ans = ;/*
for(int i = 1; i <= n; i++)cout<<p[i]<<" ";
cout<<endl;*/
for(int i = ; i <= m; i++)
{
int x = a[i].u;
int y = a[i].v;
x = f(x), y = f(y);
if(x != y)
{
p[x] = y;
tot--;//不连通数目减一
ans += a[i].w;
}
}
if(tot == )//全部连通
printf("%d\n", ans);
else
printf("-1\n");
}
return ;
}

hdu-3371 Connect the Cities---kruskal的更多相关文章

  1. hdu 3371 Connect the Cities

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

  2. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  3. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  4. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  5. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  6. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  7. HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

  8. hdu oj 3371 Connect the Cities (最小生成树)

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

  9. hdoj 3371 Connect the Cities

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

  10. POJ:3371 Connect the Cities(最小生成树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...

随机推荐

  1. Scrapy框架初探

    Scrapy 貌似是 Python 最出名的爬虫框架 0. 文档 中文文档:https://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.ht ...

  2. 《OD学spark》20160924scala基础

    拓展: Hadoop 3.0 NameNode HA NameNode是Active NameNode是Standby可以有多个 HBase Cluster 单节点故障? HBaster -> ...

  3. django的models字段介绍

    #增加数据库数据方法#方法1CalcData.objects.create(var1 ='21',var2='22')#方法2obj =CalcData(var1 ='21',var2='22')jo ...

  4. ThinkSNS+ PHP开发概述

    Plus (读音:[plʌs],全称:ThinkSNS+ [θɪŋk es en es plʌs],是 ThinkSNS 系列产品一个重要版本,其软件识别名称为 Plus 即 +) 是一个基于 Lat ...

  5. K8S上的ELK和应用日志上报实战

    来源:DevOps ID:Idevops168 本次实战的基础结构如下图所示: 一共有两个Pod:ELK和web应用: ELK的Pod会暴露两个服务,一个暴露logstash的5044端口,给file ...

  6. checkbox,不选中传值

    根据W3C的规则未选中的checkbox和禁用的控件不是有效控件,不会被POST.因此如果要未选中的checkbox表示值0的话,就不得不曲线完成了. 最近研究Zend Framework时候,发现其 ...

  7. input 上传文件的判断

    <html> <head> <meta charset='utf-8'> <meta name="viewport" content=&q ...

  8. 1、python简单介绍

    写在前面:曾经与java擦肩而过,现在懊悔很深,希望自己通过学习python,熟练掌握python,来弥补曾经的愚蠢.python简单介绍 python 1989年年底诞生,截止2017年,已经是IT ...

  9. spring retry注解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  10. Java集合——集合框架Set接口

    1.Set接口 一个不包含重复元素的collecyion.更确切的讲,set不包含满足e1.equals(e2)的元素e1和e2,并且最多包含一个null元素. 2.HashSet 类实现Set接口, ...