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 ...
随机推荐
- 面向对象五大原则_1.单一职责原则&2.里氏替换原则
单一职责原则:Single Responsibility Principle (SRP) 一个类.仅仅有一个引起它变化的原因.应该仅仅有一个职责.每个职责都是变化的一个轴线.假设一个类有一个以上的职责 ...
- nyoj 题目10 skiing —— 南阳oj
题目信息例如以下: skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区 ...
- LINQ体验(18)——LINQ to SQL语句之视图和继承支持
视图 我们使用视图和使用数据表类似,仅仅需将视图从"server资源管理器/数据库资源管理器"拖动到O/R 设计器上,自己主动能够创建基于这些视图的实体类.我们能够同操作数据表一样 ...
- GSON的简单示例
https://github.com/google/gson package com.example.wolf; import com.google.gson.JsonArray; import co ...
- 程序中引入库文件的头文件 编译时并不需要显示的用gcc去链接他的库文件 why?
拿一个苹果系统下的c文件为例: testArr.c #include <stdio.h> int main() { , , , , }; printf(]); } 当我们编译的时候 一般 ...
- redis13-----配置文件
==配置文件全解=== ==基本配置 daemonize no 是否以后台进程启动 databases 创建database的数量(默认选中的是database ) #刷新快照到硬盘中,必须满足两者要 ...
- MapReduce Join的使用
一.Map端Join 可连接两个都非常大的数据集之间可使用map端连接,数据在到达map端之前就执行连接操作. 需满足: 两个要连接的数据集都先划分成相同数量的分区,相同的key要保证在同一分区中(每 ...
- bzoj3462: DZY Loves Math II
状态很差脑子不清醒了,柿子一直在推错.... ... 不难发现这个题实际上是一个完全背包 问题在于n太大了,相应的有质数的数量不会超过7个 假设要求sigema(1~plen)i pi*ci=n 的方 ...
- 织梦CMS被挂马特征汇总
一.织梦CMS被挂马特征汇总 2013织梦CMS被挂马特征汇总.最近很多朋友反应后台多了几个系统管理员用户:service.spider等,而且自己之前的管理员用户登陆时候会提示用户名不存在.还有朋友 ...
- BestCoder4 1002 Miaomiao's Geometry (hdu 4932) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 题目意思:给出 n 个点你,需要找出最长的线段来覆盖所有的点.这个最长线段需要满足两个条件:(1 ...