zoj 2966 Build The Electric System 最小生成树
Escape Time II
Time Limit: 20 Sec Memory Limit: 256 MB
题目连接
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2966
Description
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
HINT
题意
给你个无向边带权图,让你输出最小生成树
题解:
Kruskal or prim
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int u[maxn],v[maxn],w[maxn],r[maxn];
int pa[maxn],n,m;
bool cmp(int i,int j)
{
return w[i]<w[j];
}
int fi(int x)
{
return pa[x]==x?x:pa[x]=fi(pa[x]);
}
int kruskal()
{
int ans=;
for(int i=;i<n;i++)
pa[i]=i;
for(int i=;i<m;i++)
r[i]=i;
sort(r,r+m,cmp);
for(int i=;i<m;i++)
{
int e=r[i];
int x=fi(u[e]);
int y=fi(v[e]);
if(x!=y)
ans+=w[e],pa[x]=y;
}
return ans;
}
int main()
{
//test;
int t=read();
while(t--)
{
n=read(),m=read();
for(int i=;i<m;i++)
{
int a=read(),b=read(),c=read();
u[i]=a,v[i]=b,w[i]=c;
}
printf("%d\n",kruskal());
}
}
zoj 2966 Build The Electric System 最小生成树的更多相关文章
- zoj 2966 Build The Electric System(最小生成树)
Build The Electric System Time Limit: 2 Seconds Memory Limit: 65536 KB In last winter, there wa ...
- 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 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...
随机推荐
- python基础===string模块常量
In [8]: import string In [9]: dir(string) In [10]: string.ascii_letters Out[10]: 'abcdefghijklmnopqr ...
- mysql的一些规范
我司的mysql规范,值得牢记 一.禁止内容 表字段类型 不推荐使用enum,set,blob,text等类型 表中所有字段都不能为空,需要设置not null属性,可以给默认值 表的主键列值禁止被更 ...
- $NTT$(快速数论变换)
- 概念引入 - 阶 对于$p \in N_+$且$(a, \ p) = 1$,满足$a^r \equiv 1 (mod \ p)$的最小的非负$r$为$a$模$p$意义下的阶,记作$\delta_p ...
- 链接 DB App.config 解析
<?xml version="1.0" encoding="utf-8"?><configuration> <startup> ...
- ASP.NET Core 2.0 MVC 发布部署--------- Ubuntun 16.04 X64 具体操作
.Net Core 部署到Ubuntu 16.04 中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务 ...
- Delphi根据字符串实例化对象
我们可以通过ClassRegistry单元的TClassRegistry类很轻松的根据字符串创建出对象. 下面是该类几个主要函数的说明: // 获取TClassRegistry自身的单例引用class ...
- MySql学习笔记——存储函数
在学习完存储过程后,今天主要回顾一下mysql中的存储函数的知识. 函数与存储过程的区别 首先,存储函数也是过程式对象之一,与存储过程相似.它们都是由SQL和过程式语句组成的代码片断,并且可以从应用程 ...
- MongoDB权威指南--笔记
mongodb并不具备一些在关系型数据库中很普遍的功能,如连接和复杂的多行事务. 集合-->文档-->id id在文档所属的集合中是唯一的. db.help()查看数据库级别的帮助,db. ...
- 用tomcat配置https自签名证书,解决 ios7.1以上系统, 苹果inHouse发布
用tomcat配置https自签名证书,解决 ios7.1以上系统苹果inHouse发布不能下载安装的问题教程,话说,我其实最讨厌配置某某环境了,因为某一个小环节一旦出错,你的所有工作往往会功亏一篑, ...
- 在Ubuntu上安装Arena
安装JDK 首先安装JDK对吧,下面以jdk-7u67-linux-i586.tar.gz为例 在官网上下载JDK,具体依照你的机器而定. 解压掉 tar -zxvf jdk-7u67-linux-i ...