HDU 5723 Abandoned country [最小生成树+dfs]

2 篇文章 0 订阅

题干

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

题解

做了这个题,感觉自己仍旧是ACM小白一枚
革命尚未成功,同志仍需努力!

/**
*整个题目解题关键是找出第58行的规律以简化计算,并且要注意数据类型
*/
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef struct{
  int l;int r;int len;
}Edge;
struct Node{
  int v;
  int len;
  Node(int _v,int _len):v(_v),len(_len){}
};
typedef long long ll;
const int MAX = 100010;
vector<Node>vet[MAX*10];
int T,n,m;
Edge edge[MAX*10];
int father[MAX];//并查集数组
void init_set(int n){
   for(int i=1;i<=n;i++){father[i] = i;}
}
int find(int x){
  if(x != father[x])father[x] = find(father[x]);
  return father[x];
}
void join(int x,int y){
  int fx = find(x);
  int fy = find(y);
  if(fx!=fy){
    father[fx] = fy;
  }
}
bool connect(int x,int y){
  return find(x) == find(y);
}
int cmp(Edge a,Edge b){
  return a.len < b.len;
}
ll sum[MAX];
ll dp[MAX];
//dfs深搜,复杂度为O(n)
void dfs(int root,int father){
   sum[root] = 1;
   for(int i=0;i<vet[root].size();i++){
      int son = vet[root][i].v;
      int len = vet[root][i].len;
      if(son==father)continue;//因为最小生成树是无环图,所以只要保证
      //在son这个结点处,不向父亲的方向搜索,那么dfs()就一定往son子树的方向搜索
      dfs(son,root);
      sum[root] += sum[son];
      //动态规划
      //一条边的贡献等于边权*边的使用次数
      //边的使用次数等于这条边左边的点数乘以右边的点数
      dp[root] += dp[son] + (sum[son]*(n-sum[son]))*len;
   }
}
int main(){
  //freopen("in.txt","r",stdin);
  //freopen("out.txt","w",stdout);
  scanf("%d",&T);
  while(T--){
     scanf("%d%d",&n,&m);
     init_set(n);
     for(int i=0;i<m;i++){
        scanf("%d%d%d",&edge[i].l,&edge[i].r,&edge[i].len);
     }
     sort(edge,edge+m,cmp);
     ll ans = 0;
     for(int i=0;i<MAX*10;i++)vet[i].clear();
     //kruskal算法,也就是贪心求最小生成树
     for(int i=0;i<m;i++){
        if(!connect(edge[i].l,edge[i].r)){
           join(edge[i].l,edge[i].r);
           ans += edge[i].len;
           vet[edge[i].l].push_back(Node(edge[i].r,edge[i].len));
           vet[edge[i].r].push_back(Node(edge[i].l,edge[i].len));
        }
     }
     memset(sum,0,sizeof(sum));
     memset(dp,0,sizeof(dp));
     dfs(1,0);
     //排列组合中,C(n,2)=n*(n-1)/2,也就是n个点中挑出俩点,有C(n,2)种挑法
     //一定注意数据类型是long long,用int保存不了
     double tmp = 1.0*((ll)n*(n-1)/2);
     printf("%I64d %.2f\n",ans,(double)dp[1]/tmp);
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值