hdu5723 Abandoned country

来源:互联网 发布:知乎的提问不能删除 编辑:程序博客网 时间:2024/06/07 22:31

Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 938    Accepted Submission(s): 251


Problem Description
An abandoned country has n(n100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000). Guaranteed that any two wi are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk.
 

Input
The first line contains an integer T(T10) 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
14 61 2 12 3 23 4 34 1 41 3 52 4 6
 

Sample Output
6 3.33
 

Author
HIT
 

Source
2016 Multi-University Training Contest 1
 


先用Kruskal生成最小树,然后数每一条边通过几次,每条边通过的次数乘以权重的累加和再除以C(n,2)即为 the minimum expectations length



每条边通过次数的计算方法如图


边a左边3个点,右边4个点,那么边a就会通过3 * 4 = 12次


#include <cstdio>#include <iostream>#include <algorithm>#include <cstdlib>#include <cstring>#include <cmath>#include <vector>#include <queue>typedef long long ll;using namespace std;const int maxn = 100000, maxm = 1000000;struct node {int u, v, w;};vector<node> edges;vector<node> tree[maxn + 5];bool vis[maxn + 5];int p[maxn + 5], m;ll res, ans, n;bool cmp(node x, node y) {return x.w < y.w;}int find(int x) {return p[x] == x ? x : p[x] = find(p[x]);}void Kruskal() {ans = 0;for (int i = 1; i <= n; i++) { p[i] = i; }sort(edges.begin(), edges.end(), cmp);for (int i = 0; i < m; i++) {node tn = edges[i];int x = find(tn.u), y = find(tn.v);if (x != y) {p[x] = y;ans += tn.w;tree[tn.u].push_back(tn);swap(tn.u, tn.v);tree[tn.u].push_back(tn);}}}ll dfs(int x) {vis[x] = true;ll cnt = 1, temp;for (vector<node>::iterator i = tree[x].begin(); i != tree[x].end(); i++) {if (!vis[(*i).v]) {temp = dfs((*i).v); //求一个分支上子节点个数res += temp * (n - temp) * (*i).w;cnt += temp; //当前结点子节点的总数}}return cnt;}int main(){int t;cin >> t;while (t--) {scanf("%I64d%d", &n, &m);edges.clear();for (int i = 0; i < n + 3; i++) {tree[i].clear();}for (int i = 1; i <= m; i++) {node tn;scanf("%d%d%d", &tn.u, &tn.v, &tn.w);edges.push_back(tn);}Kruskal();memset(vis, false, sizeof(vis));res = 0;ll cn2 = (n * (n - 1)) / 2;dfs(1);printf("%I64d %.2f\n", ans, 1.0 * res / cn2);}return 0;}


0 0
原创粉丝点击