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

題幹

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;
}
發佈了40 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章