zoj 2966 Build The Electric System(最小生成树)
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, A, B, K (0 <= A, B < 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(最小生成树)的更多相关文章
- zoj 2966 Build The Electric System 最小生成树
Escape Time II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showP ...
- zoj 2966 Build The Electric System
就是套了个prim算法就ac了 #include <stdio.h> #include <string.h> #define MaxInt 0x3f3f3f3f #define ...
- B - Build The Electric System 求强连通的最小和//lxm
有n个城市,有m条线路,每条线路a,b,len表示a到b的线路需要花费len的费用维修,要求能将所有城市联通的最小维修花费 按照排序排一下然后利用并查集解决 #include <iostream ...
- 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 ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- 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: ...
- HDU 4081 Qin Shi Huang's National Road System 最小生成树
点击打开链接题目链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- 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: ...
- HDU 4081 Qin Shi Huang's National Road System 最小生成树
分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...
随机推荐
- selenium+python学习总结
学习了一个月的selenium+python,终于学有所成,下面以一个简单的项目来总结学习所得. 1. 项目结构 在项目结构中,大家要注意到:每一个源文件夹中都要有一个__init__ ...
- Web前端页面的浏览器兼容性测试心得(一)搭建测试用本地静态服务器
自从实习以来,我体验了由一个人编程再到多人协作编程的转换.同时也接触了很多新鲜事物,学到了很多东西.工作时,常常需要多个人协同测试一张H5页面(PC.手机.平板等跨设备测试),也会经常把页面给PM或老 ...
- mysql批量修改列类型-生成语句
SELECT CONCAT( 'alter table ', table_name, ' MODIFY COLUMN ', column_name, ' float DEFAULT NULL;' ) ...
- 日志处理(三) logback 手动加载(转)
本文转自:http://www.2cto.com/kf/201302/191149.html 一共两个java文件,第一个是例子,第二个是配置文件加载类; LogbackTest.java /* * ...
- Linux远程管理器xshell和xftp使用教程,以及遇到关闭Xshell后项目也停止的解决方法
1.xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议. 2.是一个基于 MS windows 平台的功能强大的 ...
- kerberos master-slave搭建
1. 安装kerberos server yum install krb5-server krb5-libs krb5-auth-dialog client yum install krb5-work ...
- 20145307第十周JAVA学习报告
教材学习内容总结 Java的网络编程 1.计算机网络概述 (1)路由器和交换机组成了核心的计算机网络,计算机只是这个网络上的节点以及控制等,通过光纤.网线等连接将设备连接起来,从而形成了一张巨大的计算 ...
- 20145313Java第五次实验
实验内容 网络编程TCP代码的结对完成,一人服务器,一人客户端,进行数据传输. 结伴对象:20145313卢鑫 实验步骤 本次实验中,需要两台电脑互联.一台电脑开启无线网,充当客户端,另一台连入局域网 ...
- linux 下各个头文件的作用[典]
linux 下各个头文件的作用 2.6.30.4的头文件的位置和2.6.25.8的不一样,除去内核源码下的include目录外, 在arch/arm/mach-s3c2410/和arch/arm/ ...
- Quick Launcher FAQ
Q: Which category can be previewed and selected in Finder? A: All the categories can be selected in ...