【2016 Multi-University Training Contest 1】【hdu5723】Abandoned Countries
来源:互联网 发布:java高并发编程实战 编辑:程序博客网 时间:2024/06/16 06:07
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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 integersn,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 .
For each test case, the first line contains two integers
Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
Sample Input
14 61 2 12 3 23 4 34 1 41 3 52 4 6
Sample Output
6 3.33
Author
HIT
Source
2016 Multi-University Training Contest 1
Recommend
wange2014 | We have carefully selected several similar problems for you: 5733 5732 5731 5730 5729
多校联合第一场第一题。我读了题WZJRJ28基本秒想出正解了——先找出最小生成树再树形DP计算最小期望(其实就是期望,因为可以证明边权值唯一的情况下最小生成树唯一)——可惜他写的时候我很多细枝末节没跟他交代清楚,结果跪了15发,这第一场栽这里了。
考完了自己写了写看,果然还是太生疏了……Kruskal的并查集写错,邻接表写错,double long long各种错……调试了半天总算过了(最后还是换回了vector) T A T 。
多校联合第一场第一题。我读了题WZJRJ28基本秒想出正解了——先找出最小生成树再树形DP计算最小期望(其实就是期望,因为可以证明边权值唯一的情况下最小生成树唯一)——可惜他写的时候我很多细枝末节没跟他交代清楚,结果跪了15发,这第一场栽这里了。
考完了自己写了写看,果然还是太生疏了……Kruskal的并查集写错,邻接表写错,double long long各种错……调试了半天总算过了(最后还是换回了vector) T A T 。
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<iostream>#include<vector>using namespace std;int m,n;int wx[1000010],wy[1000010],f[100010],sum[100010];vector<int> p[100010];int ufs(int x){return x==f[x]?x:(f[x]=ufs(f[x]));}long long ans;double cost;void dfs(int v,int pre,double key){sum[v]=1;for(unsigned int i=0;i<p[v].size();i++){if(wx[p[v][i]]==pre||wy[p[v][i]]==pre)continue;if(wx[p[v][i]]==v){dfs(wy[p[v][i]],v,p[v][i]);sum[v]+=sum[wy[p[v][i]]];}else{dfs(wx[p[v][i]],v,p[v][i]);sum[v]+=sum[wx[p[v][i]]];}}if(v!=1)cost+=key*(sum[v])*(n-sum[v]);}int main(){//freopen("1001.in","r",stdin);int T;scanf("%d",&T);int tempa,tempb,tempw;while(T--){scanf("%d%d",&n,&m);memset(wx,0,sizeof(wx));memset(wy,0,sizeof(wy));for(int i=1;i<=m;i++){scanf("%d%d%d",&tempa,&tempb,&tempw);wx[tempw]=tempa;wy[tempw]=tempb;}for(int i=1;i<=n;i++)f[i]=i;for(int i=1;i<=n;i++)p[i].clear();ans=0;for(int i=1;i<=1000000;i++){if(!wx[i])continue;if(ufs(wx[i])==ufs(wy[i]))continue;f[f[wy[i]]]=f[wx[i]];ans+=((long long)i);p[wx[i]].push_back(i);p[wy[i]].push_back(i);}cost=0;dfs(1,0,0);double temp=((long long)(n-1)*n/2);printf("%I64d ",ans);printf("%.2lf\n",cost/temp);}return 0;}
0 0
- 【2016 Multi-University Training Contest 1】【hdu5723】Abandoned Countries
- 2016 Multi-University Training Contest 1Abandoned country
- 2016 Multi-University Training Contest 1 1001.Abandoned country
- 2016 Multi-University Training Contest 1 Abandoned country
- 2016 Multi-University Training Contest 1 1001 Abandoned country
- 【Hdu 5723】Abandoned country【2016 Multi-University Training Contest 1】
- 【HDU5723 2016 Multi-University Training Contest 1A】【最小生成树 + 简单算贡献】 Abandoned country 最小生成树的链长之和
- HDU5723 2016 Multi-University Training Contest 1 (最小生成树+dfs)
- HDU5723 2016 Multi-University Training Contest 1 (最小生成树+dfs)
- 2016 Multi-University Training Contest 1 hdu 5723 Abandoned country【Kruskal+Dfs】好题
- 2016 Multi-University Training Contest 1 1001 Abandoned country (最小生成树+DFS)
- 2016暑假多校合练第一场Multi-University Training Contest 1 1001 Abandoned country
- HDU 5723 Abandoned country(2016 Multi-University Training Contest 1的1001题)
- 2016 Multi-University Training Contest 1-1001---HDU 5723 Abandoned country(DFS+最小生成树)
- hdu 5723 Abandoned country(2016 Multi-University Training Contest 1——最小生成树+深搜)
- 2016 Multi-University Training Contest 1 1001 Abandoned country(最小生成树)
- (HDU 5723)2016 Multi-University Training Contest 1 Abandoned country(图论)
- 2016 Multi-University Training Contest 1
- Java 所有异常的详解
- 合并两个排序的链表
- time_t tm timeval 和 时间字符串的转换
- 反转链表
- Win32调用大漠插件dm.dll教程
- 【2016 Multi-University Training Contest 1】【hdu5723】Abandoned Countries
- Java基本数据类型转化总结
- 数学书籍下载地址
- JAVA遍历二叉树
- POJ2480——Longge's problem(数论,欧拉函数d)
- Java类结
- pip
- 链表中倒数第k个结点
- ubuntu终端下面用startup.sh脚本启动tomcat报错