Lightoj 1029 - Civil and Evil Engineer
Time Limit: 2 second(s) | Memory Limit: 32 MB |
A Civil Engineer is given a task to connect n houses with the main electric power station directly or indirectly. The Govt has given him permission to connect exactly n wires to connect all of them. Each of the wires connects either two houses, or a house and the power station. The costs for connecting each of the wires are given.
Since the Civil Engineer is clever enough and tries to make some profit, he made a plan. His plan is to find the best possible connection scheme and the worst possible connection scheme. Then he will report the average of the costs.
Now you are given the task to check whether the Civil Engineer is evil or not. That's why you want to calculate the average before he reports to the Govt.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of houses. You can assume that the houses are numbered from 1 to n and the power station is numbered 0. Each of the next lines will contain three integers in the form u v w (0 ≤ u, v ≤ n, 0 < w ≤ 10000, u ≠ v) meaning that you can connect u and v with a wire and the cost will be w. A line containing three zeroes denotes the end of the case. You may safely assume that the data is given such that it will always be possible to connect all of them. You may also assume that there will not be more than12000 lines for a case.
Output
For each case, print the case number and the average as described. If the average is not an integer then print it in p/q form. Where p is the numerator of the result and q is the denominator of the result; p and q are relatively-prime. Otherwise print the integer average.
Sample Input |
Output for Sample Input |
3 1 0 1 10 0 1 20 0 0 0 3 0 1 99 0 2 10 1 2 30 2 3 30 0 0 0 2 0 1 10 0 2 5 0 0 0 |
Case 1: 15 Case 2: 229/2 Case 3: 15 |
求最小生成树,和最大生成树。
/* ***********************************************
Author :guanjun
Created Time :2016/7/8 19:36:30
File Name :1029.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq; struct node{
int s,e;
int w;
}nod[maxn];
bool cmp1(node a,node b){
return a.w<b.w;
}
bool cmp2(node a,node b){
return a.w>b.w;
}
int n;
int sz=;
int fa[maxn];
int sum;
void init(){
sum=;
for(int i=;i<maxn;i++)fa[i]=i;
}
int findfa(int x){
if(x==fa[x])return x;
return fa[x]=findfa(fa[x]);
}
int kur(int judge){
init();
if(judge)sort(nod,nod+sz,cmp1);
else sort(nod,nod+sz,cmp2);
for(int i=;i<sz;i++){
int a=findfa(nod[i].s);
int b=findfa(nod[i].e);
if(a!=b){
fa[a]=b;
sum+=nod[i].w;
}
}
return sum; }
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T,x,y,w;
cin>>T;
for(int t=;t<=T;t++){
cin>>n;
sz=;
while(cin>>x>>y>>w){
if(x==&&y==&&w==)break;
nod[sz].s=x;
nod[sz].e=y;
nod[sz].w=w;
sz++;
}
x=kur()+kur();
int c=__gcd(x,);
if(c==)printf("Case %d: %d\n",t,x/);
else printf("Case %d: %d/%d\n",t,x,);
}
return ;
}
Lightoj 1029 - Civil and Evil Engineer的更多相关文章
- Civil and Evil Engineer(普林姆)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=198#problem/E 水题一道,题意就是让求一遍最小生成树与最大生成树,但我 ...
- Light OJ 1029- Civil and Evil Engineer (图论-最小生成树)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1029 题目大意:一个发电站,给n座房子供电, 任意房子之间有电线直接或者间接相 ...
- LightOJ 1029 【最小生成树】
思路: 利用克鲁斯卡尔算法,最小生成树把边从小到大排序,然后Union: 最大生成树就是把边从大到小排序,然后Union: #include<bits/stdc++.h> using na ...
- lightoj刷题日记
提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...
- LightOJ 1341 唯一分解定理
Aladdin and the Flying Carpet Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld &a ...
- LightOJ 1197 Help Hanzo(区间素数筛选)
E - Help Hanzo Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】
Help Hanzo (LightOJ - 1197) [简单数论][筛区间质数] 标签: 入门讲座题解 数论 题目描述 Amakusa, the evil spiritual leader has ...
- Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】
Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...
随机推荐
- Leetcode 233.数字1的个数
数字1的个数 给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数. 示例: 输入: 13 输出: 6 解释: 数字 1 出现在以下数字中: 1, 10, 11, 12, 13 . ...
- JavaScript变量声明提升
JavaScript代码在被解析引擎执行前,会被“编译”把变量声明等放在合适的作用域中,如果不了解这一点,会让人产生很多疑惑. 文章:详解js变量声明提升
- linux shell symbolic link & soft link, symbol link, link
linux shell symbolic link symbolic link https://en.wikipedia.org/wiki/Ln_(Unix) https://stackoverflo ...
- [POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)
传送门 引子: 有一个问题,是对于一个图上的所有点,用不相交的路径把他们覆盖,使得每个点有且仅属于一条路径,且这个路径数量尽量小. 对于这个问题可以把直接有边相连的两点 x —> y,建一个二分 ...
- csu 1600: Twenty-four point
传送门 1600: Twenty-four point Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 490 Solved: 78[Submit][S ...
- python学习之-- redis模块操作 LIST
redis 模块操作之 List List 操作,在内存中按照一个name对应一个List来存储. lpush(name,values):在name对应的list中添加元素,每个新的元素都添加到列表的 ...
- python学习之 - XML
xml模块定义:实现不同语言或程序之间进行数据交换的协议.格式如下:通过<>节点来区别数据结构如:<load-on-startup(这个是标签) test="value&q ...
- 洛谷——P1451 求细胞数量
P1451 求细胞数量 题目描述 一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数.(1<=m,n<=10 ...
- Java实验——输出二维数组连续二维子数组的最大和
该算法思路,根据我博客里面一维子数组求和的思路,可以用一个新的二维数组对该二维区域的数组进行求和,例如新的二维数组的第5个位置,就代表从1到5斜对角线的块状区域的和,即1,2,4,5这4个数的和,x个 ...
- windows 平台使用wireshark命令行抓包
Windows网络流量大,或则需要长时间抓包时,wireshark图形界面使用起来比较麻烦 wireshark 内置 dumpcap命令 Capture interface: -i <inte ...