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 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...
随机推荐
- PHP对象5: define / const /static
define定义全局常量: define('PATH', '/data/home/www'); const也是定义常量, 一般用于类中, 饰成员属性,不可以修饰方法,如下: class Test{ c ...
- offset宏的讲解【转】
转自:http://blog.csdn.net/tigerjibo/article/details/8299584 1.offset宏讲解 #define offsetof(TYPE, MEMBER) ...
- linux常用函数简单介绍
mmap函数简介: mmap函数是unix/linux下的系统调用,来看<Unix Netword programming>卷二12.2节对mmap的介绍: The mmap functi ...
- Freemaker如何遍历key为non-string类型的map?
(一) 前置知识 Freemaker默认配置下会使用SimpleHash去包装后台传递的hashmap,下段摘抄自官方reference 同样,当你传递进去一个hashmap实例时,会替换为一个sim ...
- [转载]ACE的陷阱
转自: http://blog.csdn.net/fullsail/article/details/2915685 坦白说,使用这个标题无非是希望能够吸引你的眼球,这篇文章的目的仅仅是为了揭示一些AC ...
- Python连接Access数据库
前言 今天想要用Python访问Access数据库,折腾了半天,特记录一下 背景 最近想将一些文件记录下来,存入数据库,为此拿LabVIEW写了一个版本,记录环境配置为: LabVIWE:2015 A ...
- ECharts图表tooltip显示时超出canvas图层解决方法
我们在做ECharts图表的时候可能会遇到tooltip显示时超出了canvas图层范围,并且被其它z-index较高的div容器遮盖,这是悬浮展示信息就看不全,我们根据官网文档的配置项查询发现con ...
- 20165301陈潭飞2017-2018-2 20165301 实验三《Java面向对象程序设计》实验报告
2017-2018-2 20165301 实验三<Java面向对象程序设计>实验报告 一.敏捷开发与XP实践-1 实验要求: 在IDEA中使用工具(Code->Reformate C ...
- Appium+python 一个简单的登录测试实例
# coding=utf-8 from appium import webdriver import time import unittest import os import HTMLTestRun ...
- Linux 用户篇——用户管理命令之id、whoami、su、chage
一.浅谈id.whoami.su.chage 本篇是续写上一篇<Linux 用户篇——用户管理命令之useradd.passwd.userdel.usermod>. (1)id命令 命令格 ...