HDU 5723 Abandoned country [最小生成树+dfs]
2016-07-22 19:33
302 查看
题干
Problem DescriptionAn 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; }
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- Prim(普里姆)算法求最小生成树的思想及C语言实例讲解
- 详解图的应用(最小生成树、拓扑排序、关键路径、最短路径)
- 最小生成树算法之Prim算法
- 使用C语言实现最小生成树求解的简单方法
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- 最小生成树算法——Prim和Kruskal算法的实现
- Hadoop FS Shell
- DFS使用方法总结
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- C 语言实现迷宫 DFS算法
- Data Structure - Week 15
- 一幅图弄清DFT与DTFT,DFS的关系
- HDFS---Namenode
- HDFS ---- Services startup
- POJ1523 SPF dfs
- 练习四1004