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 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...
随机推荐
- 通过存储过程创建SQL作业
USE dbNameGO/****** Object: StoredProcedure [dbo].[usp_Createjob] Script Date: 03/26/2014 14:36:30 * ...
- git 常用的撤销操作
git 开发时的使用流程,从同事那里学习的, 第一步,先建立一个新的分支,可以在GITLAB或通过命令git checkout -b newbranch 或通过IDE插件 第二步,让项目切换到该分支, ...
- 关于Linq翻译Inner join ,Left join (本文为转载)
我們先來一段最基本的 LINQ to SQL 使用類似 T-SQL 的 INNER JOIN 資料查詢語法: from c in Categories from o in c.Products sel ...
- 为什么@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
Spring Boot会自动根据jar包的依赖来自动配置项目,例如当你项目下面有HSQLDB的依赖,Spring Boot会自动创建默认的内存数据库的数据源DataSource, 但我们使用Mybat ...
- logstash运输器以及kibana的更多操作
为了达到不会因为ELK中的某一项组件因为故障而导致整个ELK工作出问题,于是 将logstash收集到的数据存入到消息队列中如redis,rabbitMQ,activeMQ或者kafka,这里以red ...
- ThinkPHP5执行流程分析
1.入口文件(tp5\public\index.php) 作用: 1)定义目录常量. 2)加载框架引导目录. 2.框架引导目录(tp5\thinkphp\start.php) 作用: 1)引导基础文件 ...
- 20145303刘俊谦 《Java程序设计》第2周学习总结
20145303刘俊谦 <Java程序设计>第2周学习总结 教材学习内容总结 1.对于标识符的一些名称规范(觉得挺重要而且容易混淆!定义就不写了,列一些例子): 包名: xxxyyyzzz ...
- 配置linux内核输出所有的log信息
答案:修改内核文件include/linux/printk.h (内核版本为4.9) 修改前 #define CONSOLE_LOGLEVEL_DEFAULT 7 /* anything MORE ...
- android emulator 安装中文输入法
android emulator 模拟器内置没有中文输入法,有些情况下我们需要输入正文就比较麻烦. 在模拟器的浏览器中下载输入法然后安装,会提示系统不兼容的情况. 这是由于Android应用多基于AR ...
- e.target和e.currentTarget区别
直接上代码: body里: <div id="father"> father <div id="son"> son </div&g ...