0
点赞
收藏
分享

微信扫一扫

HDU 5723 Abandoned country


Problem Description


n(n≤100000) villages which are numbered from 1 to  n. Since abandoned for a long time, the roads need to be re-built. There are  m(m≤1000000) roads to be re-built, the length of each road is  wi(wi≤1000000). Guaranteed that any two  wi


 



Input


T(T≤10) which indicates the number of test cases. 

For each test case, the first line contains two integers  n,m indicate the number of villages and the number of roads to be re-built. Next  m lines, each line have three number  i,j,wi, the length of a road connecting the village  i and the village  j is  wi.


 



Output


output the minimum cost and minimum Expectations with two decimal places. They separated by a space.


 



Sample Input


1
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6

 



Sample Output


6 3.33


 



因为边都是不同的,所以最小生成树唯一,然后就是一遍dfs算出每条边的使用次数即可

#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x7FFFFFFF;
int T, n, m, fa[N];
int ft[N], nt[N], u[N], v[N], sz;
LL ans1, cnt[N];
double ans2;

struct point
{
int x, y, z;
void read() { scanf("%d%d%d", &x, &y, &z); }
bool operator<(const point& a) const { return z < a.z; }
}a[N];

int get(int x)
{
return x == fa[x] ? x : fa[x] = get(fa[x]);
}

void insert(int x, int y, int z)
{
u[sz] = y; nt[sz] = ft[x]; v[sz] = z; ft[x] = sz++;
u[sz] = x; nt[sz] = ft[y]; v[sz] = z; ft[y] = sz++;
}

void dfs(int x, int fa)
{
cnt[x] = 1;
for (int i = ft[x]; i != -1; i = nt[i])
{
if (u[i] == fa) continue;
dfs(u[i], x);
cnt[x] += cnt[u[i]];
ans2 += 2 * cnt[u[i]] * (n - cnt[u[i]])*v[i];
}
}

int main()
{
scanf("%d", &T);
while (T--)
{
ans1 = ans2 = sz = 0;
scanf("%d%d", &n, &m);
rep(i, 1, m) a[i].read();
sort(a + 1, a + m + 1);
rep(i, 1, n) fa[i] = i, ft[i] = -1;
rep(i, 1, m)
{
int fx = get(a[i].x), fy = get(a[i].y);
if (fx == fy) continue;
fa[fx] = fy; ans1 += a[i].z;
insert(a[i].x, a[i].y, a[i].z);
}
dfs(1, 1);
printf("%lld %.2lf\n", ans1, ans2 / n / (n - 1));
}
return 0;
}



举报

相关推荐

0 条评论