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. Ubuntu 16.04 安装Navicat Premium

    一.介绍 Navicat premium是一款数据库管理工具,是一个可多重连线资料库的管理工具,它可以让你以单一程式同时连线到 MySQL.SQLite.Oracle 及 PostgreSQL 资料库 ...

  2. Linux 日志分析工具(logwatch)安装及使用

    Linux 日志分析工具(logwatch)安装及使用 日志是非常重要的系统文件,管理员每天的重要工作就是分析和查看服务器的日志,判断服务器的健康状态.但是日志管理又是一项非常枯燥的工作,如果需要管理 ...

  3. zookeeper和淘宝dubbo的关系

    Dubbo建议使用Zookeeper作为服务的注册中心. 1.   Zookeeper的作用:         zookeeper用来注册服务和进行负载均衡,哪一个服务由哪一个机器来提供必需让调用者知 ...

  4. 在python3下对数据分块(8x8大小)使用OpenCV的离散余弦变换DCT

    在MATLAB中有blkproc (blockproc)对数据处理, 在python下没找到对应的Function, 这里利用numpy 的split(hsplit和vsplit) 对数据分块处理成8 ...

  5. CSS Link(链接)

    CSS Link(链接) 不同的链接可以有不同的样式. 一.链接样式 链接的样式,可以用任何CSS属性(如颜色,字体,背景等). 特别的链接,可以有不同的样式,这取决于他们是什么状态. 这四个链接状态 ...

  6. mouseleave mouseout时候悬浮框不应该消失的时候消失了 css 解决办法

    要实现的效果和代码思路 简单来说就是 用一个div包着喇叭和悬浮框 悬浮事件写在这个div上 鼠标悬浮到div上的时候 悬浮框出现 最终要做成鼠标从小喇叭移动到下面的框上的时候 下面框是不会消失的. ...

  7. 试编hello world

    这里是一些vim的使用方法: 这时不知道怎么编译了  看了上面的知识 也问了志伟,我就知道了.是要“./hello”就可以了 自己敲了代码,今后也会多试运行,编译,得尽快安装虚拟机了.

  8. MR案例:Reduce-Join

    问题描述:两种类型输入文件:address(地址)和company(公司)进行一对多的关联查询,得到地址名(例如:Beijing)与公司名(例如:Beijing JD.Beijing Red Star ...

  9. 树梅派配置ad-hoc网络

    树梅派配置ad-hoc网络 更新与安装 1.更改源/etc/apt/source.list: http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian ...

  10. Linux环境下的CPU消耗分析

    在Linux系统中, CPU 主要用于中断,内核以及用户进程的任务处理,优先级为 中断 > 内核 > 用户进程.在CPU消耗分析中,我们还经常遇到下面几个概念. 上下文切换         ...