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 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 <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 105;
const int MAXM = 200010;
const int N = 1005;
int n;
int mp[MAXN][MAXN],MP[MAXN][MAXN],dist[MAXN];
bool vis[MAXN];
int Prim(int mp[][MAXN])
{
int mi,now,ans=0;
memset(vis,false,sizeof(vis));
memset(dist,INF,sizeof(dist));
for (int i=0;i<=n;i++)
dist[i]=mp[i][0];
dist[0]=0;
vis[0]=true;
for (int i=0;i<n;i++)
{
mi=INF;
now=-1;
for (int j=0;j<=n;j++)
{
if (!vis[j]&&mi>dist[j])
{
now=j;
mi=dist[j];
}
}
if (now==-1) return -1;
vis[now]=true;
ans+=dist[now];
for (int j=0;j<=n;j++)
{
if (!vis[j]&&dist[j]>mp[now][j])
dist[j]=mp[now][j];
}
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j,u,v,w,cas=0,t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
memset(mp,INF,sizeof(mp));
memset(MP,INF,sizeof(MP));
while (1)
{
scanf("%d%d%d",&u,&v,&w);
if (u==0&&v==0&&w==0) break;
if (mp[u][v]>w) mp[u][v]=mp[v][u]=w;
if (MP[u][v]>-w) MP[u][v]=MP[v][u]=-w;
}
int ans=Prim(mp)-Prim(MP);
printf("Case %d: ",++cas);
if (ans%2==0) printf("%d\n",ans/2);
else printf("%d/2\n",ans);
}
return 0;
}