HDU 5723 Abandoned country (最小生成树+树形dp)
来源:互联网 发布:雅思听力技巧 知乎 编辑:程序博客网 时间:2024/05/16 11:13
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 46 Accepted Submission(s): 19
Total Submission(s): 46 Accepted Submission(s): 19
Problem Description
An abandoned country has 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 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 integerT(T≤10) which indicates the number of test cases.
For each test case, the first line contains two integersn,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 .
For each test case, the first line contains two integers
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
6 3.33
2016 Multi-University Training Contest 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723
题目大意:给一个图,求一个最小生成树,输出权值和这个最小生成树任意两点间距离的平均值
题目分析:签到题,和hdu 2376一样,只不过这题要在求出来的MST上求解,sort可能会T,因为是3990ms过的,或者因为权值不一样,直接枚举权值做2500ms过,各种爆int
#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int const MAXN = 1e5 + 5;int const MAXM = 1e6 + 5;int n, m;struct EDGE{ int to, nxt; ll w;}e[MAXM << 1];int head[MAXN], cnt, fa[MAXN];int num[MAXN];ll sum;struct EDGE2{ int u, v;}e2[MAXM];void Add(int u, int v, ll w){ e[cnt].to = v; e[cnt].w = w; e[cnt].nxt = head[u]; head[u] = cnt ++; e[cnt].to = u; e[cnt].w = w; e[cnt].nxt = head[v]; head[v] = cnt ++;}void Init(){ for(int i = 0; i <= n; i++) fa[i] = i;}int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]);}void Union(int a, int b){ int r1 = Find(a); int r2 = Find(b); if(r1 != r2) fa[r2] = r1;}void DFS(int u, int fa) { num[u] = 1; for(int i = head[u]; i != -1; i = e[i].nxt) { int v = e[i].to; ll w = e[i].w; if(v != fa) { DFS(v, u); num[u] += num[v]; sum += (ll)num[v] * (ll)(n - num[v]) * w; } } } int main(){ int T; scanf("%d", &T); while(T --) { scanf("%d %d", &n, &m); Init(); cnt = 0; sum = 0; memset(head, -1, sizeof(head)); memset(e2, 0, sizeof(e2)); memset(num, 0, sizeof(num)); int ma = 0; for(int i = 0; i < m; i++) { int u, v, w; scanf("%d %d %d", &u, &v, &w); ma = max(ma, w); e2[w].u = u; e2[w].v = v; } ll ans = 0; int pnum = 0; for(int i = 0; i <= ma; i++) { if(e2[i].u && e2[i].v) { int uu = e2[i].u; int vv = e2[i].v; if(Find(uu) != Find(vv)) { pnum ++; ans += (ll) i; Union(uu, vv); Add(uu, vv, i); if(pnum >= n - 1) break; } } } DFS(1, -1); ll tmp = (ll) n * (ll) (n - 1) / 2ll; printf("%I64d %.2f\n", ans, (double) sum / (double) tmp); }}
2 0
- HDU 5723 Abandoned country (最小生成树+树形dp)
- HDU 5723 Abandoned country(最小生成树 + 树形DP)
- HDU 5723 Abandoned country(最小生成树Kruskal+树形DP)
- HDU 5723-A - Abandoned country-最小生成树+树dp
- HDU 5723 Abandoned country 最小生成树
- hdu 5723 Abandoned country 最小生成树+DFS
- HDU 5723 Abandoned country 最小生成树,树上期望
- HDU 5723 Abandoned country (最小生成树+期望)
- HDU 5723 Abandoned country (最小生成树+dfs)
- HDU 5723 Abandoned country [最小生成树+dfs]
- HDU 5723 Abandoned country (最小生成树 + 期望值)
- HDU 5723 Abandoned country(最小生成树+DFS)
- HDU 5723 - Abandoned country(最小生成树+dfs)
- hdu 5723 Abandoned country(最小生成树)
- 2016HDU多校联赛-HDU-5723-Abandoned country(最小生成树 邻接表DFS)
- 最小生成树+树的平均距离-HDU-5723-Abandoned country
- HDU 5723 Abandoned country【2016多校联合】(最小生成树+深搜)
- 2016 Multi-University Training Contest 1-1001---HDU 5723 Abandoned country(DFS+最小生成树)
- 职业发展
- 打击破解软件,终于有解决良方了!
- PBR贴图转换1——对PBR的误解
- 2016 Multi-University Training Contest 1 1001 hdu 5723 DFS+并查集最小生树
- 上code之前的test plan
- HDU 5723 Abandoned country (最小生成树+树形dp)
- C++四大类型转换
- POJ 3461 Oulipo KMP模板题
- Android之MVP入门使用(一)
- 总结关于登陆Linux的三种方式
- JAVA api 远程hbase出错 org.apache.hadoop.hbase.client.RetriesExhaustedException
- 【shaderToy】中openGL转到unity中的一些技巧
- 写shell脚本调用php文件
- 20160711