ACM~java~web~密码学

Abandoned country

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

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
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

Author
HIT

，而后，在最小花费的方案上计算一个期望，啥子期望腻，就是国王要随机选择两个城市，问这两个城市之间距离的期望，既任意两个城市都有可能被选择到，那么期望等于所有路程和/路径个数，最小花费方案不用说肯定是最小生成树了，既然是最小生成树，那么路径个数= n*(n-1)/2，路程长度咋求啊，不能把所有路径都求出来加和啊，那么我们想到，可以算每条路用的次数，我们发现，一条路用的次数等于这条路两侧城市个数之积；

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <queue>
const int maxm=1000000+1000,maxn=100000+100;
using namespace std;
struct node
{
int x,y;
long long num;
}d[maxm];
bool cmp(node a,node b)
{
return a.num<b.num;
}
int par[maxn];
int rank2[maxn];
void init1(int n)
{
for(int i=0;i<=n;i++)
{
par[i]=i;
rank2[i]=0;
}
}
int find_(int x)
{
if(par[x]==x)
return x;
else
{
return par[x]=find_(par[x]);
}
}
void add(int x,int y)
{
x=find_(x);
y=find_(y);
if(x==y)
{
return;
}
if(rank2[x]<rank2[y])
{
par[x]=y;
}
else{
par[y]=x;
if(rank2[x]==rank2[y])
rank2[x]++;
}
}

struct Edge
{
int v;
long long w;
Edge(){;}
Edge(int vv,long long ww)
{
v=vv;
w=ww;
}
};
int leaf[maxn];
vector<Edge>G[maxm];
void init2(int n)
{
for(int i=0;i<=n;i++)
{
G[i].clear();
leaf[i]=1;
}
}
void add_edge(int u,int v,long long w)
{
G[u].push_back(Edge(v,w));
G[v].push_back(Edge(u,w));
}
void dfs(int u,int pre)
{
if(G[u].size()==1)
leaf[u]=1;
int num=G[u].size();
for(int i=0;i<num;i++)
{
int v=G[u][i].v;
if(v==pre)
continue;
dfs(v,u);
leaf[u]+=leaf[v];
}
}
int vis[maxn];
int N;
double bfs(int s)
{
memset(vis,0,sizeof(vis));
queue<int>que;
que.push(s);
double  ans=0.0,fm,temp;
fm=1.0*N*(N-1)/2.0;
//cout<<"fm"<<fm<<endl;
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]=1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].v;
if(!vis[v])
{
que.push(v);
temp=1.0*min(leaf[u],leaf[v]);
ans=ans+(((1.0*N-temp)*temp)*(1.0*G[u][i].w))/fm;
//cout<<ans<<endl;
}
}
}
return ans;
}
int main()
{
int t;
int n,m;
long long mintree;
scanf("%d",&t);
while(t--)
{

scanf("%d%d",&n,&m);
init2(n);
init1(n);
N=n;
mintree=0;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&d[i].x,&d[i].y,&d[i].num);
}
sort(d,d+m,cmp);
for(int i=0;i<m;i++)
{
if(find_(d[i].x)!=find_(d[i].y))
{
mintree+=d[i].num;
}
}
dfs(1,1);
double ans2=bfs(1);
printf("%lld %.2lf\n",mintree,ans2);
}
return 0;
}

HDU 4756:Install Air Conditioning (最小生成树最佳替换边，最小生成树+树形dp)

2017-10-22 19:55:59

hdu5723(树形dp+最小生成树)

2016-07-20 11:29:24

树形DP整理小结

2016-08-24 15:38:21

华中科大2011状态压缩DP和树形DP

2018年05月15日 905KB 下载

树形 DP 总结

2016-08-27 14:12:16

树形DP例题整理

2017-08-17 17:40:56

树形动态规划（树形DP）入门问题—初探 & 训练

2015-04-29 22:40:03

树形dp 小结

2016-11-17 22:32:40

树形dp_树形动态规划_讲解PPT

2013年05月03日 130KB 下载

树形DP总结，持续更新

2016-01-03 18:09:58

不良信息举报

hdu5723 最小生成树 树形dp