标签:
题目链接:
Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 327680/327680 K (Java/Others)
#include <bits/stdc++.h> using namespace std; const int N=9e6+4; typedef long long ll; const ll mod=1e9+7; const int inf=1e9; const double PI=acos(-1.0); int n,m,dis[3002][3002],p[3002],vis[3002],u,v,w,flag[3002][3002],dp[3002][3002]; struct Edge { int l,r,len; }; Edge edge[N]; int cmp(Edge x,Edge y) { return x.len<y.len; } int findset(int x) { if(x==p[x])return x; return p[x]=findset(p[x]); } int same(int x,int y) { int fx=findset(x),fy=findset(y); if(fx!=fy)p[fx]=fy; } vector<int>vc[3002]; int dfs(int num,int x,int father) { int l=vc[x].size(); int ans=inf; for(int i=0;i<l;i++) { int y=vc[x][i]; if(y!=father) { int temp=dfs(num,y,x);ans=min(ans,temp); dp[x][y]=dp[y][x]=min(dp[y][x],temp); } } if(num!=father) { ans=min(ans,dis[num][x]); } return ans; } int main() { while(1) { scanf("%d%d",&n,&m); if(n==0&&m==0)break; for(int i=0;i<n;i++) { vc[i].clear(); p[i]=i; for(int j=0;j<n;j++) { flag[i][j]=0; dis[i][j]=dp[i][j]=inf; } } for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); edge[i].l=u; edge[i].r=v; edge[i].len=w; dis[u][v]=w; dis[v][u]=w; } sort(edge,edge+m,cmp); double mans=0; for(int i=0;i<m;i++) { if(findset(edge[i].l)!=findset(edge[i].r)) { same(edge[i].l,edge[i].r); flag[edge[i].l][edge[i].r]=1; flag[edge[i].r][edge[i].l]=1; mans+=(double)edge[i].len; vc[edge[i].l].push_back(edge[i].r); vc[edge[i].r].push_back(edge[i].l); } } for(int i=0;i<n;i++) { dfs(i,i,-1); } int q; scanf("%d",&q); double sum=0; for(int i=0;i<q;i++) { scanf("%d%d%d",&u,&v,&w); if(!flag[u][v])sum+=mans; else { sum+=mans-dis[u][v]*1.0+min(dp[u][v],w)*1.0; } } printf("%.4lf\n",sum/(q*1.0)); } return 0; }
hdu-4126 Genghis Khan the Conqueror(最小生成树+树形dp)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5389384.html