hud3371 Connect the Cities 简单最小生成树
//我看过Discuss说不能用克鲁斯卡尔因为有很多边
//但是只能用G++过,C++的确超时
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
int a, b, cost;
}c[];
int fa[]; void init(int n)
{
for (int i = ; i <= n; i++)
fa[i] = i;
} bool cmp(node x, node y)
{
return x.cost<y.cost;
} int find(int x)
{
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
} int main()
{
int n, k, m, ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d %d %d", &n, &k, &m);
init(n); //初始化
for (int i = ; i<k; i++)
scanf("%d %d %d", &c[i].a, &c[i].b, &c[i].cost);
for (int i = ; i <= m; i++)
{
int x, pos, pos1;
scanf("%d %d", &x, &pos);
for (int j = ; j<x; j++)
{
scanf("%d", &pos1);
c[k].a = pos, c[k].b = pos1, c[k].cost = ;
pos = pos1;
k++;
}
} sort(c, c + k, cmp);
int sum = ;
for (int i = ; i<k; i++)
{
int x = find(c[i].a);
int y = find(c[i].b);
if (x != y)
sum += c[i].cost, fa[x] = y;
} int count = ;
for (int i = ; i <= n; i++)
if (fa[i] == i)
count++; if (count != )
printf("-1\n");
else
printf("%d\n", sum);
}
return ;
}
hud3371 Connect the Cities 简单最小生成树的更多相关文章
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- HDU3371--Connect the Cities(最小生成树)
Problem Description In 2100, since the sea level rise, most of the cities disappear. Though some sur ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- 组件接口(API)设计指南[2]-类接口(class interface)
*返回文件夹阅读其它章节: http://blog.csdn.net/cuibo1123/article/details/39894477 类接口(class interface) 你能够參考MGTi ...
- java类加载机制的代码实例
package typeinfo; import java.util.Random; class Initable { static final int staticFinal = 47 ; stat ...
- centos笔记-安装特定版本的mysql
centos6的yum默认安装的mysql是5.1版, 如果要安装5.6.16 版,有三个办法 1.yum方式, 这个方式的好处是通过yum安装卸载都很方便,坏处是版本无法详细制定,比如官方版本yum ...
- diamond简介和使用
简介 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单.可靠.易用,目前淘宝内部绝大多数系统的配置,由diamond来进行统一管理. diamond为应用系统提供了获取配置的服务, ...
- HTML canvas
什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...
- react native与原生的交互
一.交互依赖的重要组件 react native 中如果想要调用ios 中相关的方法,必须依赖一个重要的组件nativemodules import { NativeModules } from ' ...
- POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 一步一步学Silverlight 2系列(7):全屏模式支持
一步一步学Silverlight 2系列(7):全屏模式支持 概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言V ...
- 「LuoguP1496」 火烧赤壁
Description 曹操平定北方以后,公元208年,率领大军南下,进攻刘表.他的人马还没有到荆州,刘表已经病死.他的儿子刘琮听到曹军声势浩大,吓破了胆,先派人求降了. 孙权任命周瑜为都督,拨给他三 ...
- 区间DP 青蛙的烦恼
池塘中有n片荷叶恰好围成了一个凸多边形,有一只小青蛙恰好站在1号荷叶上,小青蛙想通过最短的路程遍历所有的荷叶(经过一个荷叶一次且仅一次),小青蛙可以从一片荷叶上跳到另外任意一片荷叶上. 输入数据(fr ...