题目大意:有n个网站,由m条线路相连,每条线路都有一定的花费,找出连接所有线路的最小花费。

  最小生成树问题(Minimal Spanning Tree, MST),使用Kruskal算法解决。

 #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> ii;
#define MAXN 1000100 int p[MAXN]; int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n;
bool first = true;
while (scanf("%d", &n) != EOF)
{
int u, v, w;
int old_cost = ;
for (int i = ; i < n-; i++)
{
scanf("%d%d%d", &u, &v, &w);
old_cost += w;
}
int k;
scanf("%d", &k);
vector< pair<int, ii> > EdgeList;
for (int i = ; i < k; i++)
{
scanf("%d%d%d", &u, &v, &w);
EdgeList.push_back(make_pair(w, make_pair(u, v)));
}
int m;
scanf("%d", &m);
for (int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
EdgeList.push_back(make_pair(w, make_pair(u, v)));
}
sort(EdgeList.begin(), EdgeList.end());
for (int i = ; i <= n; i++)
p[i] = i;
int new_cost = ;
for (int i = ; i < EdgeList.size(); i++)
{
w = EdgeList[i].first;
u = EdgeList[i].second.first;
v = EdgeList[i].second.second;
int pu = find(u);
int pv = find(v);
if (pu != pv)
{
new_cost += w;
p[pv] = pu;
}
}
if (first) first = false;
else printf("\n");
printf("%d\n%d\n", old_cost, new_cost);
}
return ;
}

UVa 908 - Re-connecting Computer Sites的更多相关文章

  1. UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)

    UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...

  2. uva 12096 - The SetStack Computer(集合栈)

    例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...

  3. uva 12096 The SetStack Computer

    点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...

  4. UVA 11766 Racing Car Computer --DP

    题意:电脑记录了某一时刻每个赛车的前面和后面个有多少辆车(多个车并排时在别的车那只算一辆),问最少有多少个不合理的数据. 分析:看到n<=1000时,就尽量往DP上想吧. 每输入一组数据a,b, ...

  5. UVa 1647 (递推) Computer Transformation

    题意: 有一个01串,每一步都会将所有的0变为10,将所有的1变为01,串最开始为1. 求第n步之后,00的个数 分析: 刚开始想的时候还是比较乱的,我还纠结了一下000中算是有1个00还是2个00 ...

  6. UVa 12096 The SetStack Computer【STL】

    题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...

  7. uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)

    题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...

  8. poj3694 缩点边双连通分量

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8669   Accepted: 3175 Descripti ...

  9. Network-POJ3694并查集+LCA

    Network Time Limit: 5000MS   Memory Limit: 65536K       Description A network administrator manages ...

随机推荐

  1. CentOS 6.5配置mysql

    启动mysql service mysqld start 给root账号设置密码 mysqladmin -u root password '

  2. PAT (Advanced Level) 1074. Reversing Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  3. oracle中nvl函数

    最近在修改项目中一个统计的bug,统计出的钱数不对,因为不是自己开发的模块,经过分析流程找到了统计的sql. sum(f_msmoney)+sum(f_fkmoney) as total, 上面这段是 ...

  4. 百度网盘API的操作--PCS 百度个人云存储 上传 ,下载文件

    来自http://blog.csdn.net/u014492257/article/details/39856403 另外需要所有API使用方法的请访问本人上传的资源(需要3个下载分的)链接: htt ...

  5. C语言写的俄罗斯方块

    源:C语言写的俄罗斯方块 2014年最后一天, 任天堂将风靡全球30年的经典游戏<<俄罗斯方块>>下架. 作为全球最畅销的游戏, 其移植版本遍布各个平台. 下面这个是我去年在5 ...

  6. GPRS优点介绍及GPRS上网相关知识(转)

    源:http://blog.chinaunix.net/uid-20745340-id-1878732.html 单片机微控制器以其体积小.功耗低.使用方便等特点,广泛应用于各种工业.民用的嵌入式系统 ...

  7. DBNull.value

    判断某一个datarow的cell是否有值需要进行两步判断 1.dr["field_name"]!=DBNull.value&&dr["field_nam ...

  8. CodeForces 614B Gena's Code

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  9. Java谜题心得

    1,二进制浮点数的运算是对实际算数的一种近似运算. 2,IEEE 754浮点算术保留了一个特殊的值用来表示一个不是数字的数量[IEEE 754].这个值就是NaN(“不是一个数字(Not a Numb ...

  10. SDWEBImage和collectionView的组合,以及collectionView的随意间距设置

    #import "ViewController.h" #import <ImageIO/ImageIO.h> #import "UIImageView+Web ...