HDU-5723-Abandoned country(kruskal+DFS)
来源:互联网 发布:易天行定位软件 编辑:程序博客网 时间:2024/04/27 16:17
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723
题意:无向图求最小生成树,以及两点之间距离的期望。
题解:kru求最小生成树,然后DFS出每条边所用到的次数(两边点数乘积)。
CODE:
#include <bits/stdc++.h>using namespace std;const int MAXM = 1e6+7;const int MAXN = 1e5+7;struct Edge{ int u,v,cost;} edge[MAXM];int head[MAXN],ind;int n,m;void add_edge(int u, int v, int cost){ edge[ind].u=u; edge[ind].v=v; edge[ind++].cost=cost;}struct node{ int u,v,next,cost;}e[MAXN<<1];int index;void add_(int u,int v,int cost){ e[index].u=u; e[index].v=v; e[index].cost=cost; e[index].next=head[u]; head[u]=index++;}bool cmp(Edge x, Edge y){ return x.cost<y.cost;}int set_[MAXN];int sum[MAXN];int set_find(int x){ if(set_[x]==-1)return x; return set_[x]=set_find(set_[x]);}double ans;int DFS(int u, int fa){ int temp=0,poi=0; for(int i=head[u]; i+1; i=e[i].next) { int v=e[i].v; if(v==fa)continue; poi=DFS(v,u); temp+=poi; ans=ans+1.0*poi*(n-poi)*e[i].cost; } return temp+1;}int main(){ int T; int a,b,c; scanf("%d",&T); while(T--) { index=ind=0; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for(int i=0; i<m; ++i) { scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); } sort(edge,edge+m,cmp); memset(set_,-1,sizeof(set_)); int cnt=0,x,y; long long temp=0; for(int i=0; i<m; ++i) { x=edge[i].u; y=edge[i].v; a=set_find(x); b=set_find(y); if(a!=b) { set_[b]=a; add_(x,y,edge[i].cost); add_(y,x,edge[i].cost); cnt++; temp+=edge[i].cost; if(cnt==n-1)break; } } ans=0; DFS(1,0); printf("%I64d %.2lf\n",temp,ans*2.0/((n-1)*1.0)/(n*1.0)); } return 0;}
0 0
- HDU 5723 Abandoned country(DFS+kruskal)
- HDU-5723-Abandoned country(kruskal+DFS)
- HDU 5723 - Abandoned country(最小生成树+dfs)
- 2016 Multi-University Training Contest 1 hdu 5723 Abandoned country【Kruskal+Dfs】好题
- HDU 5723 Abandoned country
- HDU 5723 Abandoned country
- HDU 5723 Abandoned country
- hdu 5723 Abandoned country
- HDU-5723 Abandoned country
- HDU 5723 Abandoned country
- HDU 5723 Abandoned country
- HDU 5723 - Abandoned country
- hdu 5723 Abandoned country
- hdu 5723 Abandoned country
- HDU 5723 Abandoned country(最小生成树Kruskal+树形DP)
- hdu 5723 Abandoned country 最小生成树+DFS
- HDU 5723 Abandoned country (最小生成树+dfs)
- HDU 5723 Abandoned country [最小生成树+dfs]
- #NOIP 2014#Day.2 T3 解方程
- javaweb学习总结(十)——HttpServletRequest对象(一)
- tableviewcell 单个选中状态(swift)
- sql
- // 去掉TabBar上边的线
- HDU-5723-Abandoned country(kruskal+DFS)
- 为什么中国的程序员总被称为码农?
- Unix Socket Tutorial Intro
- 国王的魔镜
- 理解transform
- IDEA实用插件
- android studio与git
- POJ2262
- RANSAC及其改进