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 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...
随机推荐
- Axure RP 授权码
Axure RP 8.1.0.3372Licensee:KoshyKey:wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6x ...
- DataFrame衍生新特征操作
1.DataFrame中某一列的值衍生为新的特征 #将LBL1特征的值衍生为one-hot形式的新特征 piao=df_train_log.LBL1.value_counts().index #先构造 ...
- 四. Jmeter--JDBC 请求
一, SQLserver 1.下载 JDBC Driver (sqljdbc_6.0.8112.100_enu.exe) https://www.microsoft.com/en-us/downlo ...
- kippo蜜罐搭建
kippo蜜罐搭建 总结一下kippo蜜罐搭建的方法,centos系统.kippo-master.zip的安装包 (gcc,python-devel,pip,twisted==13.10,pycryp ...
- nginx与PHP的关系和交互方式【转】
nginx与PHP的关系. 对比, apache和PHP的关系, 将PHP安装成apache的一个功能模块, 导致的结果, 对外只有一个apache程序, PHP并不独立出现, 仅仅是apache的模 ...
- awk正则匹配nginx日志【原创】
查看网页访问代码不为200和30x所有行的内容 awk '{if($9!~/200|30*/) print $0}' /app/logs/http_access.log 或 awk '$9!~/200 ...
- Codeforces Round #441 (Div. 2)
Codeforces Round #441 (Div. 2) A. Trip For Meal 题目描述:给出\(3\)个点,以及任意两个点之间的距离,求从\(1\)个点出发,再走\(n-1\)个点的 ...
- u-boot启动第一阶段
目标板:2440开发板 u-boot启动的第一阶段是在文件start.S中完成的,这个过程对不同硬件平台的设置是不同的.下面进入start.S _start: b reset //跳转到reset / ...
- [ python ] 进程的操作
目录 (见右侧目录栏导航)- 1. 前言- 2. multiprocess模块- 2.1 multiprocess.Process模块 - 2.2 使用Process模块创建进程 - 2. ...
- Linux软件管理器(如何使用软件管理器来管理软件)2---安装及管理Linux应用程序
安装及管理Linux应用程序 Linux应用程序的组成1.普通的可执行程序文件,一般保存在/usr/bin目录中,普通用户即可执行.2.服务器程序.管理程序文件,一般保存在/usr/sbin目录中,需 ...