题目链接:

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. HTML 代码格式

     <code> 元素不保留多余的空格和折行: 如需解决该问题,您必须在 <pre> 元素中包围代码:           

  2. Java实例——基于jsoup的简单爬虫实现(从智联获取工作信息)

    这几天在学习Java解析xml,突然想到Dom能不能解析html,结果试了半天行不通,然后就去查了一些资料,发现很多人都在用Jsoup解析html文件,然后研究了一下,写了一个简单的实例,感觉还有很多 ...

  3. hdu 2147 kiki's game(巴什博弈)

    kiki's game HDU - 2147 题意:一个n*m的表格,起始位置为右上角,目标位置为左下角,甲先开始走,走的规则是可以向左,向下或者向左下(对顶的)走一格.谁先走到目标位置谁就胜利.在甲 ...

  4. JavaWeb:Cookie处理和Session跟踪

    JavaWeb:Cookie处理和Session跟踪 Cookie处理 什么是Cookie Cookie 是存储在客户端计算机上的文本文件,保留了各种跟踪信息.因为HTTP协议是无状态的,即服务器不知 ...

  5. web综合案例02

    web综合案例02 web综合案例02 web综合案例02 ... ... 内容待添加

  6. JSON 的使用方法

    JSON--JavaScript Object Notation,是一种轻量级的数据交互格式,本质是特定格式的字符串,相比xml更简洁,现在是客户端与服务器端交互的最常用选择,已经很少用xml了 JS ...

  7. [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  8. 洛谷P2184 贪婪大陆

    题目背景 面对蚂蚁们的疯狂进攻,小FF的\(Tower\) \(defence\)宣告失败--人类被蚂蚁们逼到了\(Greed\) \(Island\)上的一个海湾.现在,小FF的后方是一望无际的大海 ...

  9. oracle数据库的导入导出命令

    说明:将以下命令复制到cmd命令行中运行即可,file代表文件名数据导入imp zzbweb/zzbweb@orcl file=e:\zzbweb.dmp fromuser=zzbweb touser ...

  10. View转换为Bitmap及getDrawingCache

    View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有 void  setDrawingCacheEnabled(boolean flag),    Bitmap  get ...