Light OJ 1029- Civil and Evil Engineer (图论-最小生成树)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1029
题目大意:一个发电站,给n座房子供电, 任意房子之间有电线直接或者间接相连接, 现在想知道需要连接这些房子花费的平均电线长度。平均电线长度 = (最长电线长度 + 最短电线长度)/ 2;
解题思路:裸的最小生成树
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = ;
struct edge
{
int s, t, l;
edge() {}
edge(int s, int t, int l):s(s), t(t), l(l) {}
};
vector<edge>vec;
int par[N], Rank[N];
int n; void init()
{
for(int i=; i<=n; ++ i)
par[i] = i, Rank[i] = ;
} int Find(int x)
{
if(par[x] == x)
return x;
else
return par[x] = Find(par[x]);
} void unite(int x, int y)
{
x = Find(x), y = Find(y);
if(x == y)
return ; if(Rank[x] < Rank[y])
par[x] = y;
else
{
par[y] = x;
if(Rank[x] == Rank[y])
Rank[x] ++;
}
} bool same(int x, int y)
{
return Find(x) == Find(y);
} bool cmp(const edge &a, const edge &b)
{
return a.l < b.l;
} bool Cmp(const edge &a, const edge &b)
{
return a.l > b.l;
} int kruskal(int m)
{
init();
if(m == )
sort(vec.begin(), vec.end(), cmp);
else
sort(vec.begin(), vec.end(), Cmp);
int res = ;
for(int i=; i<vec.size(); ++ i)
{
edge e = vec[i];
if(!same(e.s, e.t))
{
unite(e.s, e.t);
res += e.l;
}
}
return res;
} void solve(int cases)
{
scanf("%d", &n);
vec.clear();
while()
{
int s, t, l;
scanf("%d%d%d", &s, &t, &l);
if((s || t || l) == false)
break;
vec.push_back(edge(s, t, l));
} int ans = ;
ans += kruskal();
ans += kruskal();
if(ans & )
printf("Case %d: %d/%d\n", cases, ans, );
else
printf("Case %d: %d\n", cases, ans/); } int main()
{
int t;
scanf("%d", &t);
for(int i=; i<=t; ++ i)
solve(i);
return ;
}
Light OJ 1029- Civil and Evil Engineer (图论-最小生成树)的更多相关文章
- Lightoj 1029 - Civil and Evil Engineer
1029 - Civil and Evil Engineer PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limi ...
- Civil and Evil Engineer(普林姆)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=198#problem/E 水题一道,题意就是让求一遍最小生成树与最大生成树,但我 ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
- Jan's light oj 01--二分搜索篇
碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...
随机推荐
- vs2012中添加lib,.h文件方法(原)
项目.属性.C/C++.附加包含目录:填写附加头文件(*.h)所在目录 分号间隔多项项目.属性.链接器.附加库目录:填写附加依赖库(*.lib)所在目录 分号间隔多项项目.属性.链接器(点前面的+展开 ...
- SQLSERVER如何查看索引缺失
SQLSERVER如何查看索引缺失 当大家发现数据库查询性能很慢的时候,大家都会想到加索引来优化数据库查询性能, 但是面对一个复杂的SQL语句,找到一个优化的索引组合对人脑来讲,真的不是一件很简单的事 ...
- C#编写WIN32系统托盘程序
基本功能概述: 程序运行后驻留系统托盘,左键呼出,右键退出.后续可加右键菜单. 注册系统案件WIN+F10,呼出程序. 重写系统消息,最小化和关闭按钮隐藏程序 using System; using ...
- ORACLE插入DATE类型字段
1 怎样在ORACLE中输入DATE类型的字段 insert into table_name (date_column) values(to_date('2006-06-04','yyyy-mm-dd ...
- 关于ckeditor ajax提交到后台 问题
ckeditor 提交时 如果有带有html时是提交不了的 解决办法就是 你在提交的时候 将ckeditor获取的只编码(encodeURI) 然后在传到后台提交的时候 在解码 就ok了 ckname ...
- Android 调用系统联系人界面的添加联系人,添加已有联系人,编辑和修改。
一.添加联系人 Intent addIntent = new Intent(Intent.ACTION_INSERT,Uri.withAppendedPath(Uri.parse("cont ...
- Session操作
存储API localStorage和sessionStorage通常被当做普通的JavaScript对象使用:通过设置属性来存储字符串值,查询该属性来读取该值.除此之外,这两个对象还提供了更加正式的 ...
- ZeroC Ice 暂记
摘自: http://weibo.com/p/1001603869896789339575 原文地址: http://www.oschina.net/question/865233_242146 吴治 ...
- css secrets----multiple borders
原始文档: https://www.zybuluo.com/freeethy/note/193574 box-shadow solution 只能实现solid border box-shadow表现 ...
- 100怎么变成100.00 || undefined在数字环境下是:NaN || null在数字环境下是0 || 数组的toString()方法把每个元素变成字符串,拼在一起以逗号隔开 || 空数组转换成字符串后是什么?
100怎么变成100.00?