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 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...
随机推荐
- OpenStack的架构详解
OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于,帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也为大云.小云提供可扩展的.灵活的 ...
- Java序列化的机制和原理 转
转 http://developer.51cto.com/art/200908/147650.htm Java序列化的机制和原理 本文讲解了Java序列化的机制和原理.从文中你可以了解如何序列化一个对 ...
- 20145316许心远《Java程序设计》第4周学习总结
20145316许心远<Java程序设计>第4周学习总结 教材学习内容总结 6.继承与多态 1.继承共同行为 * 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需 ...
- 使用sql语句创建 表空间 和用户,以及如何彻底删除表空间和用户,使用命令导出和导出数据库
创建表空间有很多种方式,在安装好oracle 11g 后在网站上 https://localhost:1158/em 手动创建也可以,但是没有sql直接生成方便,下面介绍下如何用sql语句直接生成表空 ...
- 企业级hbase HA配置
1 HBase介绍HBase是一个分布式的.面向列的开源数据库,就像Bigtable利用了Google文件系统(File System)所提供的分布式数据存储一样,HBase在Hadoop之上提供了类 ...
- 关于Android RenderScript 的详细说明和一些实用文档
http://www.cnblogs.com/TerryBlog/archive/2012/03/02/2377251.html RenderScript 是一种低级的高性能编程语言,用于3D渲染和处 ...
- tomcat结合nginx或apache做负载均衡及session绑定
1.tomcat结合nginx做负载均衡,session绑定 nginx:192.168.223.136 tomcat:192.168.223.146:8081,192.168.223.146:8 ...
- 三20135320赵瀚青LINUX内核分析第二周学习笔记
赵瀚青原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.计算机的三个法宝 存储程 ...
- ECU
ECU(Electronic Control Unit)电子控制单元,又称“行车电脑”.“车载电脑”等.从用途上讲则是汽车专用微机控制器,也叫汽车专用单片机.它和普通的单片机一样,由微处理器(CPU) ...
- mysql增量备份脚本
#!/bin/sh ############################### # 此脚本用来增量备份 # 此文件名:mysqldailybackup.sh # # Author: zhangro ...