lightoj 1029 生成树裸题

来源:互联网 发布:公安大数据宣传片 编辑:程序博客网 时间:2024/06/06 03:03

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 than 12000 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

几乎是裸题没啥好说的
令人在意的地方是哪个优先队列…
我一开始最大最小用反了…
很迷

#include<iostream>#include<cmath>#include<cstdio>#include<memory.h>#include<string>#include<vector>#include<algorithm>#include<queue>using namespace std;int tu[101][101][2];int fa[101];struct p{    int q, w, e;    bool operator < (const p&a)const {        return e > a.e;    }};int zhao(int x){    while (x != fa[x])    {        x = fa[x];    }    return x;}struct r{    int q, w, e;    bool operator < (const r&a)const {        return e < a.e;    }};int main(){    int T;    cin >> T;    int u = 0;    while (T--)    {        int n;        cin >> n;        for (int a = 0;a <= 100;a++)        {//0是最小1边是最大边            for (int b = 0;b <= 100;b++)            {                tu[a][b][0] = 10001;                tu[a][b][1] = 0;            }        }        int q, w, e;        while (1)        {            scanf("%d%d%d", &q, &w, &e);            if (q == w&&w == e&&q == 0)break;            tu[q][w][0] = min(tu[q][w][0], e);            tu[w][q][0] = tu[q][w][0];            tu[q][w][1] = max(tu[q][w][1], e);            tu[w][q][1] = tu[q][w][1];        }        for (int a = 0;a <= n;a++)fa[a] = a;        priority_queue<p> qq;        for (int a = 0;a <= n;a++)        {            for (int b = 0;b <= n;b++)            {                if (tu[a][b][0] == 10001)continue;                qq.push({ a, b, tu[a][b][0] });            }        }        int jishuxiao = 0;        while (!qq.empty())        {            p qw = qq.top();            qq.pop();            int qww = zhao(qw.q);            int wee = zhao(qw.w);            if (qww == wee)continue;            jishuxiao += qw.e;            fa[qww] = wee;        }        //cout << jishuxiao << endl;        priority_queue<r> rr;        for (int a = 0;a <= n;a++)fa[a] = a;        for (int a = 0;a <= n;a++)        {            for (int b = 0;b <= n;b++)            {                if (tu[a][b][1] == 0)continue;                rr.push({ a, b, tu[a][b][1] });            }        }        int jishuda = 0;        while (!rr.empty())        {            r qw = rr.top();            rr.pop();            int qww = zhao(qw.q);            int wee = zhao(qw.w);            if (qww == wee)continue;            jishuda += qw.e;            fa[qww] = wee;        }        //cout << jishuda << endl;        int jishu = jishuda + jishuxiao;        if (jishu % 2 == 0)printf("Case %d: %d\n", ++u, jishu/2);        else printf("Case %d: %d/2\n", ++u, jishu);    }    return 0;}
0 0
原创粉丝点击