题目大意:有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. linux下CPU信息查询

    1.查看逻辑CPU个数: #cat /proc/cpuinfo |grep "processor"|sort -u|wc -l24 2.由于有超线程技术有必要查看一下物理CPU个数 ...

  2. Ubuntu14.04下SP_Flash_Tool_exe_Linux无法烧录

    1,用命令lsusb查看usb信息. 2,vim 20-mm-blacklist-mtk.rules 输入下面内容: ATTRS{idVendor}=="0e8d",ENV{ID_ ...

  3. zf-关于分页必写的代码

    1 存储过程 ALTER PROCEDURE [dbo].[getStatForXXGKWeb] ), ), ), @page int, -- 必写的 @pageRows int,-- 必写的 @al ...

  4. CodeForces 139C Literature Lesson(模拟)

    这个题,读懂了就是水,读不懂就没办法下手,论英语阅读的重要性...只有五种形式,第一种万能型aaaa,是另外3种的特殊情况,第二种克莱里林四行打油诗aabb形式,第三种是交替的abab形式,第四种是封 ...

  5. C#对象序列化笔记

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  6. C# devExpress BandedGridView属性 备忘

    BandedGridView属性备忘 StringBuilder sb = new StringBuilder(); DevExpress.XtraGrid.Views.BandedGrid.Band ...

  7. css3的apprearance属性(转)

    appearance使用方法: .elmClass{ -webkit-appearance: value; -moz-appearance: value; appearance: value; } 接 ...

  8. 一个java 开源神经网络引擎 joone

    https://sourceforge.net/projects/joone/files/?source=navbar joone

  9. Keil 二进制数输入宏

    源:http://hi.baidu.com/skystalker/item/e7679cd79c6f751220e250c1?qq-pf-to=pcqq.c2c 在C语言中有十进制,十六进制,八进制; ...

  10. javascript 函数 add(1)(2)(3)(4)实现无限极累加 —— 一步一步原理解析

    问题:我们有一个需求,用js 实现一个无限极累加的函数, 形如 add(1) //=> 1; add(1)(2)  //=> 2; add(1)(2)(3) //=>  6; add ...