spoj 368
额 最小生成树 ........
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int u,v,len;
void f(int i, int j, int k)
{
u = i, v = j;
len = k;
}
bool operator < (const node &p) const
{
return len < p.len;
}
};
node cc[300020];
int f[1010];
int find(int x)
{
return f[x] == x ? x : f[x] = find(f[x]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int p,n,m;
scanf("%d%d%d",&p,&n,&m);
for(int i = 1; i <= n; i++)
f[i] = i;
for(int i = 0; i < m; i++)
{
int x,y,len;
scanf("%d%d%d",&x,&y,&len);
cc[i].f(x, y, len);
}
sort(cc, cc+m);
int ans = 0;
for(int i = 0; i < m; i++)
{
int x = find(cc[i].u), y = find(cc[i].v);
if(x != y)
{
ans += cc[i].len;
f[x] = y;
}
}
printf("%d\n",ans*p);
}
return 0;
}
spoj 368的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- winform版简易http服务器
传人url运行(url以/结尾,例如:http://localhost:8080/web/ 监听这个url// 在浏览器 中输入 http://localhost:8080/web/?name=tes ...
- Ubuntu Linux 分区简易教程
关于Linux系统下的“分区”问题,对于新手来说一直是很头疼的.我来简单写一下,它的“分区”方法,规则. 声明:我为了让没有接触过Linux系统的人,理解更加简单.所以在言语表述上不是很规范,专业.我 ...
- 链接器工具错误 LNK2011
问题描述: 使用visual studio 2015编译apr-iconv失败,提示"链接器工具错误 LNK2011:未链接预编译对象:映像可能不能运行"错误. 原因分析: MSD ...
- Swift扩展(Extension)
在现有类和结构体的类型基础上,扩展新的功能. 语法: extension SomeType{ // new functionality to add to SomeType goes here } A ...
- JavaScript学习笔记(10)——JavaScript语法之操作DOM
1.页面输出用document.write()方法,但是不可以在window.onload中用,否则整个html页面将被覆盖. 2.通过javascript获取对象后,改变对象中的html内容:doc ...
- sql2000下如何新建并使用dbml
默认新建的dbml只是支持sql2005及其以上版本. 但是现在是sql2000怎么办?我要是想要用linq to sql 的? 解决方案如下: 1首先打开cmd,在其中cd到sqlmetal.exe ...
- GitHub 基本操作流程
GitHub是最先进的分布式版本控制工具,下面是我学习中总结的操作流程,仅供参考 ----------------------------------------------------------- ...
- eclispe 出现超内纯错误
刚开始以为只要修改tomcat的最大最小内存就可以,结果还是报错,后来才懂需要在eclipse.ini文件中修改 -Xms256m-Xmx512m的值改大些,增加虚拟机运行的内存空间 刚开始最小值只有 ...
- CodeForces 679B(Bear and Tower of Cubes)
题意:Limak要垒一座由立方体垒成的塔.现有无穷多个不同棱长(a>=1)的立方体.要求:1.塔的体积为X(X<=m).2.在小于X的前提下,每次都选体积最大的砖块.3.在砖块数最多的前提 ...
- Poj OpenJudge 百练 1573 Robot Motion
1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot M ...