标签:
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 4487 Accepted Submission(s): 1131 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
题意 :
国家有M条废弃的路和N个点,现在要重新修,求出连通每一个点需要的最短路径是多少,并求在连通的路任意选两个点走,它的最小期望。
解法:
1 #include <bits/stdc++.h> 2 #define maxn 100005 3 using namespace std; 4 double res; 5 6 vector< pair<int,int> >Edge1[maxn]; 7 8 struct Edge 9 { 10 int from; 11 int to; 12 int length; 13 friend bool operator < (const Edge &e1 , const Edge &e2) 14 { 15 return e1.length > e2.length;//最小值优先 16 } 17 }; 18 19 20 int father[maxn]; //用来做并查集 21 int nodeNum,edgeNum; //顶点数、边数 22 long long MST; //最小生成树边权值和 23 24 priority_queue<Edge> myQ; //优先队列 25 26 void storeMap() //存储岛的桥构成的图 27 { 28 while(!myQ.empty()) 29 myQ.pop(); //清空队列 30 int from,to,length; 31 for(int i = 0 ; i < edgeNum ; i++) //kruskal算法对于无向图也只需建一条边即可 32 { 33 scanf("%d%d%d",&from,&to,&length); 34 Edge e; 35 e.from = from; 36 e.to = to; 37 e.length = length; 38 myQ.push(e); 39 } 40 } 41 42 43 int findx(int x) //查找父节点 44 { 45 if(x == father[x]) 46 return father[x]; 47 return father[x] = findx(father[x]); 48 } 49 50 51 bool judge() //判断是否是一棵最小生成树 ,这里得注意起点和终点 52 { 53 int f = findx(1); 54 for(int i = 2 ; i <= nodeNum ; i++) 55 { 56 if(f != findx(i)) 57 return false; 58 } 59 return true; 60 } 61 62 63 void init()//初始化函数 64 { 65 for(int i = 0 ; i <= nodeNum ; i++) 66 { 67 father[i] = i; 68 Edge1[i].clear(); 69 } 70 return; 71 }//特意把 maxn 改成 nodeNum 并且把这个模块从底下的函数中独立出来,没想到时间一点也没少,反倒增加了,很是迷茫 72 73 74 int kruskal() //kruskal算法 75 { 76 MST = 0; 77 int num = 0; //记录MST的边数 78 while(!myQ.empty() && num != nodeNum-1) 79 { 80 Edge e = myQ.top(); 81 myQ.pop(); 82 int fx = findx(e.from); 83 int fy = findx(e.to); 84 if(fx != fy) 85 { 86 father[fx] = fy; 87 MST += e.length; 91 Edge1[ e.from ].push_back(make_pair(e.to , e.length)); 92 Edge1[ e.to ].push_back(make_pair(e.from , e.length)); 93 num++; 94 } 95 } 96 return num; 97 } 98 99 100 int dfs(int x , int f) 101 { 102 int cnt = 0;//该条路遍历过次数 103 for(int i = 0 ; i < Edge1[x].size() ; i++) 104 { 105 int v = Edge1[x][i].first; 106 if(v == f) 107 continue; 108 int fcnt = dfs( v , x ); 109 cnt += fcnt; 110 res = res + 1.0 * fcnt * ( nodeNum - fcnt) * Edge1[x][i].second;//该条路的贡献 111 } 112 return cnt + 1; 113 } 114 115 int main() 116 { 117 //freopen("sample.in" , "r" , stdin); 118 //freopen("sample1.out" , "w" , stdout); 119 int t; 120 scanf("%d",&t); 121 while(t--) 122 { 123 res = 0; 124 scanf("%d%d",&nodeNum , &edgeNum); 125 storeMap(); 126 init(); 127 kruskal(); 128 dfs(1 , 0); 129 res = res * 2.0 / (nodeNum *1.0) /(nodeNum - 1.0); 130 printf("%I64d %.2lf\n",MST,res ); 131 } 132 return 0; 133 }
ACM学习之路___HDU 5723(kruskal + dfs)
标签:
原文地址:http://www.cnblogs.com/x-1204729564/p/5792870.html