2016 Multi-University Training Contest 1 1001 hdu 5723 DFS+并查集最小生树
来源:互联网 发布:雅思听力技巧 知乎 编辑:程序博客网 时间:2024/06/06 01:03
链接:戳这里
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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 integer 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
思路:
并查集计算出最小生成树,算每条边的贡献,左右边分别有多少点
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;struct node{ int u,v,w; bool operator < (const node &a)const{ return w<a.w; }}s[1000100];struct edge{ int v,w,next;}e[2000100];int n,m,tot;int head[1000100],val[1000100],sz[1000100],fa[1000100];void init(){ for(int i=1;i<=n;i++) fa[i]=i; mst(sz,0); mst(head,-1); mst(val,0); tot=0;}int Find(int x){ if(x!=fa[x]) return fa[x]=Find(fa[x]); return fa[x];}void Add(int u,int v,int w){ e[tot].v=v; e[tot].next=head[u]; e[tot].w=w; head[u]=tot++;}void DFS(int u,int Fa){ sz[u]=1; for(int i=head[u];i!=-1;i=e[i].next){ int v=e[i].v; if(v==Fa) continue; val[v]=e[i].w; DFS(v,u); sz[u]+=sz[v]; }}int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); init(); for(int i=0;i<m;i++) scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].w); sort(s,s+m); ll an=0; for(int i=0;i<m;i++){ int X=Find(s[i].u); int Y=Find(s[i].v); if(X!=Y){ fa[X]=Y; an+=(ll)s[i].w; Add(s[i].u,s[i].v,s[i].w); Add(s[i].v,s[i].u,s[i].w); } } DFS(1,0); ll sum=0; for(int i=2;i<=n;i++) { sum+=(ll)val[i]*sz[i]*(n-sz[i]); } if(n==1) printf("0 0.00\n"); else printf("%I64d %.2f\n",an,sum*1.0/((ll)n*(n-1)/2)); } return 0;}
0 0
- 2016 Multi-University Training Contest 1 1001 hdu 5723 DFS+并查集最小生树
- 2016 Multi-University Training Contest 1-1001---HDU 5723 Abandoned country(DFS+最小生成树)
- 2016 Multi-University Training Contest 1 hdu 5723 Abandoned country【Kruskal+Dfs】好题
- hdu 5802 2016 Multi-University Training Contest 6(dfs)
- 【Hdu 5723】Abandoned country【2016 Multi-University Training Contest 1】
- 2016 Multi-University Training Contest 1 1001 Abandoned country (最小生成树+DFS)
- HDU 5723 Abandoned country(2016 Multi-University Training Contest 1的1001题)
- hdu 5723 Abandoned country(2016 Multi-University Training Contest 1——最小生成树+深搜)
- 2016 Multi-University Training Contest 2 1001 hdu 5734 暴力
- HDU 2016 Multi-University Training Contest 2 1001
- 2016 Multi-University Training Contest 2-1001---HDU 5734 Acperience
- (HDU 5733)2016 Multi-University Training Contest 1 tetrahedron(几何)
- 2016 Multi-University Training Contest 1 1002 hdu 5724 博弈
- 2016 Multi-University Training Contest 1-1004---HDU 5726 GCD
- 2016 Multi-University Training Contest 1 C Game(hdu 5725)
- HDU 5724 Chess (from: 2016 Multi-University Training Contest 1)
- hdu 5732 subway(2016 Multi-University Training Contest 1 1010)
- (HDU 5726)2016 Multi-University Training Contest 1 GCD(数学)
- caffe interface --- python
- 今日学习
- 职业发展
- 打击破解软件,终于有解决良方了!
- 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中的一些技巧