Lightoj 1029 - Civil and Evil Engineer

1029 - Civil and Evil Engineer

   PDF (English) Statistics Forum
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 << 61;
const double eps=1e-5;
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=0;
int fa[maxn];
int sum;
void init(){
    sum=0;
    for(int i=0;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=0;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=1;t<=T;t++){
        cin>>n;
        sz=0;
        while(cin>>x>>y>>w){
            if(x==0&&y==0&&w==0)break;
            nod[sz].s=x;
            nod[sz].e=y;
            nod[sz].w=w;
            sz++;
        }
        x=kur(1)+kur(0);
        int c=__gcd(x,2);
        if(c==2)printf("Case %d: %d\n",t,x/2);
        else printf("Case %d: %d/%d\n",t,x,2);
    }
    return 0;
}
时间: 2024-10-22 07:58:00

Lightoj 1029 - Civil and Evil Engineer的相关文章

LightOJ 1029 Civil and Evil Engineer最小生成树和最大生成树

F - Civil and Evil Engineer Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1029 Description A Civil Engineer is given a task to connect n houses with the main electric power station directly

LightOJ 1029-Civil and Evil Engineer(最小/大生成树)

Description 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 h

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 = 1003; struct

lightoj 1029 最小生成树 + 最大生成树

题意,给出若干条连接两个屋子间的路线的价格(保证一定都能联通),问联通所有屋子的最大代价和最小代价的平均值为多少. 分析,即求一次最大生成树,一次最小生成树 1 /* When all else is lost the future still remains. */ 2 #define rep(X,Y,Z) for(int X=(Y);X<(Z);X++) 3 #define drep(X,Y,Z) for(int X=(Y);X>=(Z);X--) 4 #define fi first 5

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1164 World is getting more evil and it's getting tougher to get into the Evil League of Evil. Since the legendary Bad Horse has retired, now you have to correctly answer the evil questions of Dr.

LightOJ 1341 - Aladdin and the Flying Carpet(算术基本定理啊)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter

LightOJ 1341 唯一分解定理

Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Appoint description:  System Crawler  (2016-07-08) Description It's said that Aladdin had to solve seven myst

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>