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 ...
随机推荐
- [UnityShader3]溶解与重现效果
參考链接:http://www.cnblogs.com/Esfog/p/DissolveShader.html 效果图: 从颜色变化来说,有三种,一种是纹理颜色.一种是纹理与黑边的混合颜色,一种是透明 ...
- 11892 - ENimEN(博弈)
UVA 11892 - ENimEN 题目链接 题意:给定n堆石头.两人轮流取,每次仅仅能取1堆的1到多个.假设上一个人取了一堆没取完.那么下一个人必须继续取这堆.取到最后一个石头的赢,问谁赢 思路: ...
- Delphi之萝莉调教篇
本文纯属技术交流.如果各位看官想与小生一起探讨萝莉的问题的话...PM我吧 关于Delphi的萝莉调教技术,很久以前就有大牛做过了...其实技术早掌握了只是觉得太无聊~估计大家也都会于是就没有写~既然 ...
- SAP系统更新模块
SAP 系统中,一些单据保存到数据库用的是 update mudule function. 命名是ME_UPDATE_* (业务说明) 例:PR save module: ME_UPDATE_REQU ...
- DDD领域建模基本流程
整理一个精简的DDD领域建模基本流程,供大家在DDD领域建模实践中进行参考. 搜集用户故事(用户的原始需求) 整理用户故事,抽出用例(用例表达了用户对系统的需求,定义了系统的边界以及系统外部角色和系统 ...
- POJ1459 Power Network —— 最大流
题目链接:https://vjudge.net/problem/POJ-1459 Power Network Time Limit: 2000MS Memory Limit: 32768K Tot ...
- POJ2516 Minimum Cost —— 最小费用最大流
题目链接:https://vjudge.net/problem/POJ-2516 Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Tota ...
- ES6 解构赋值的常见用途,很强大
字符串 var [a,b,c,d,e] = "hello"; console.log(a); // h console.log(b); // e console.log(c); / ...
- PHP加密方式。 base!base!base!
PHP中的加密方式有如下几种 1. MD5加密 string md5 ( string $str [, bool $raw_output = false ] ) 参数 str -- 原始字符串. ...
- codeforces 672A A. Summer Camp(水题)
题目链接: A. Summer Camp time limit per test 1 second memory limit per test 256 megabytes input standard ...