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 ...
随机推荐
- Python+selenium(多表单、多窗口切换)
多表单切换 案例:在Frame.html文件种定位搜狗搜索页面,进行搜索操作 Frame.html <html> <head> <title>Frame_test& ...
- HDU 3062 简单的2-SAT问题
在2-SAT,最让我纠结的还是添加有向线段的函数了 void add_clause(int i,int a,int j,int b){ int m=2*i+a; int n=2*j+b; ...
- [luoguP3258] [JLOI2014]松鼠的新家(lca + 树上差分)
传送门 需要把一条路径上除了终点外的所有数都 + 1, 比如,给路径 s - t 上的权值 + 1,可以先求 x = lca(s,t) 类似数列上差分的思路,可以给 s 和 f[t] 的权值 + 1, ...
- 【BZOJ3939】Cow Hopscotch(动态开点线段树)
题意: 就像人类喜欢跳格子游戏一样,FJ的奶牛们发明了一种新的跳格子游戏.虽然这种接近一吨的笨拙的动物玩跳格子游戏几乎总是不愉快地结束,但是这并没有阻止奶牛们在每天下午参加跳格子游戏 游戏在一个R* ...
- P1420 最长连号
洛谷——P1420 最长连号 题目描述 输入n个正整数,(1<=n<=10000),要求输出最长的连号的长度.(连号指从小到大连续自然数) 输入输出格式 输入格式: 第一行,一个数n; 第 ...
- 最短路——Dijkstra算法
模板 水模板ing #include <cstdio> #include <cstring> #include <algorithm> #include <i ...
- ArcEngine中IFeatureClass.Search(filter, Recycling)方法中Recycling参数的理解
转自 ArcEngine中IFeatureClass.Search(filter, Recycling)方法中Recycling参数的理解 ArcGIS Engine中总调用IFeatureCla ...
- maven打包时的三方包的选择顺序
在一个项目有多个模块引用多个版本的某个插件(或者叫三方包.jar包等)时,如何解决版本冲突问题?最终选用某个版本还是选择几个版本? maven在遇到上面的情况时,会智能处理版本冲突,最终选择一个版本, ...
- USB多重系統 - 開機碟工具 – WinSetupFromUSB
WinSetupFromUSB下載與安裝 讓USB磁碟擁有多重開機的功能,WinSetupFromUSB有著提軟體和硬體的高相容性. [官方網頁]:http://www.winsetupfromusb ...
- iOS国际化:NSLocalizedString的使用
因为iOS和XCode版本号更新得太快的原因,导致网上非常多文章都失去了时效性,或许再过两三个月我这篇文章也将走上这条路,但起码能够让现阶段看到的人对iOS的国际化有个比較清楚的认识. NSLocal ...