Build The Electric System


Time Limit: 2 Seconds      Memory Limit: 65536 KB

In last winter, there was a big snow storm in South China. The electric system was damaged seriously. Lots of power lines were broken and lots of villages lost contact with the main power grid. The government wants to reconstruct the electric system as soon as possible. So, as a professional programmer, you are asked to write a program to calculate the minimum cost to reconstruct the power lines to make sure there's at least one way between every two villages.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.

In each test case, the first line contains two positive integers N and E (2 <= N <= 500, N <= E <= N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There follow E lines, and each of them contains three integers, ABK (0 <= AB < N, 0 <= K < 1000). A and B respectively means the index of the starting village and ending village of the power line. If K is 0, it means this line still works fine after the snow storm. If K is a positive integer, it means this line will cost K to reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself.

Output

For each test case in the input, there's only one line that contains the minimum cost to recover the electric system to make sure that there's at least one way between every two villages.

Sample Input

1
3 3
0 1 5
0 2 0
1 2 9

Sample Output

5

Author: ZHOU, Ran
Source: The 5th Zhejiang Provincial Collegiate Programming Contest

#include <iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
struct node
{
int x,y,w;
node(int a,int b,int c){x=a;y=b;w=c;}
};
int t,n,e;
int team[];
struct cmp
{
bool operator()(node a,node b)
{
return a.w>b.w;
}
};
int findteam(int k)
{
if (k!=team[k]) return team[k]=findteam(team[k]);
else return k;
}
int main()
{
while(~scanf("%d",&t))
{
for(;t>;t--)
{
scanf("%d%d",&n,&e);
priority_queue<node,vector<node>,cmp> Q;
for(int i=;i<n;i++) team[i]=i;
for(int i=;i<=e;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
Q.push(node(x,y,z));
}
int sum=;
while(!Q.empty())
{
node u=Q.top();
Q.pop();
int fx=findteam(u.x);
int fy=findteam(u.y);
if (fx!=fy)
{
sum+=u.w;
team[fx]=fy;
}
}
printf("%d\n",sum);
}
}
return ;
}

zoj 2966 Build The Electric System(最小生成树)的更多相关文章

  1. zoj 2966 Build The Electric System 最小生成树

    Escape Time II Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showP ...

  2. zoj 2966 Build The Electric System

    就是套了个prim算法就ac了 #include <stdio.h> #include <string.h> #define MaxInt 0x3f3f3f3f #define ...

  3. B - Build The Electric System 求强连通的最小和//lxm

    有n个城市,有m条线路,每条线路a,b,len表示a到b的线路需要花费len的费用维修,要求能将所有城市联通的最小维修花费 按照排序排一下然后利用并查集解决 #include <iostream ...

  4. Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG]

    Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG] <ignore_js_op> 程序员文本编辑器 Sublime Tex ...

  5. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  6. HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...

  7. HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树

    点击打开链接题目链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  8. hdu-4081 Qin Shi Huang's National Road System(最小生成树+bfs)

    题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: ...

  9. HDU 4081 Qin Shi Huang's National Road System 最小生成树

    分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...

随机推荐

  1. 714. Best Time to Buy and Sell Stock with Transaction Fee

    问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...

  2. 20145316许心远《Java学习笔记》第三周总结

    20145316许心远<Java程序设计>第3周学习总结 教材学习内容总结 一.定义类: 类定义时使用class关键字 如果要将x绑定到新建的对象上,可以使用"="制定 ...

  3. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  4. Linux Java环境搭建

    -------------------------网络配置-------------------------------1.配置DNS:vi /etc/resolv.confnameserver 19 ...

  5. JS正则表达式从入门到入土(7)—— 分组

    分组 在使用正则的时候,有时候会想要匹配一串字符串连续出现多次的情况,比如:我想匹配字符串Byron连续出现3次的情况. 有些人会直接写: Byron{3} 但是,这种情况仅仅会匹配Byro加上三个n ...

  6. PentestBox简明使用教程

    介绍 PentestBox:渗透测试盒子 顾名思义,这是一个渗透工具包,但是不同于绝大多数国内xx工具包的是,这里集成的大都是Linux下的工具,Kali Linux上面的常用的很多工具这里面也都集成 ...

  7. centos安装xdebug 和 phpstorm+Xdebug断点调试PHP

    转载地址:http://www.2cto.com/os/201304/206058.html   CentOS下安装xdebug   在CentOS 6.x 的系统中,是集成xdebug 的,   y ...

  8. (转)C#自制Web 服务器开发:用C#开发自己的Web服务器

    当输入:127.0.0.1:5050 GET / HTTP/1.1 Accept: text/html, application/xhtml+xml, */* Accept-Language: zh- ...

  9. [翻译]理解CSS模块方法

    在这个前端发展日新月异的世界,能够找到有所影响的概念相当困难,而将其准确无误的传达,让人们愿意尝试,更是难上加难. 拿CSS来看,在我们写CSS时,工具侧最大的变化,也就是CSS处理器的使用,如:可能 ...

  10. Nagios监控mysql主从复制

    因为公司的nagios用了很久监控项目很多,也在zabbix迁移中,也就先临时用nagios监控mysql主从了 mysql> show slave status\G 查看其输出,即可判定主从复 ...