Abandoned country
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.
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.
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e6+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,p[maxn],vis[maxn],cnt;
double ans[];
vector<pii>edge[maxn];
struct node
{
int a,b,l;
}q[maxn];
bool cmp(const node&x,const node&y)
{
return x.l<y.l;
}
int all(int x){return p[x]==x?x:p[x]=all(p[x]);}
int dfs(int now)
{
int son=,son_son;
vis[now]=;
for(auto x:edge[now])
{
if(!vis[x.fi]){
son_son=dfs(x.fi);
son+=son_son;
ans[]+=1.0*son_son*(n-son_son)*x.se;
}
}
return son;
}
int main()
{
int i,j,k,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
rep(i,,n)p[i]=i,edge[i].clear();
memset(vis,,sizeof(vis));
ans[]=ans[]=;cnt=;
rep(i,,m-)scanf("%d%d%d",&q[i].a,&q[i].b,&q[i].l);
sort(q,q+m,cmp);
rep(i,,m-)
{
int u=all(q[i].a),v=all(q[i].b);
if(u!=v)
{
p[u]=v;
ans[]+=q[i].l;
edge[q[i].a].pb({q[i].b,q[i].l}),edge[q[i].b].pb({q[i].a,q[i].l});
cnt++;
if(cnt==n-)break;
}
}
dfs();
double y=1.0*(n-)*n/;
printf("%.0f %.2f\n",ans[],ans[]/y);
}
//system ("pause");
return ;
}
Abandoned country的更多相关文章
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- HDU 5723 Abandoned country (最小生成树 + dfs)
Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU5723 Abandoned country (最小生成树+深搜回溯法)
Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since aban ...
- HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...
- HDU 5723:Abandoned country(最小生成树+算期望)
http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Problem Description An abandoned ...
随机推荐
- 让上下两个DIV块之间有一定距离或没有距离
1.若想上下DIV块之间距离,只需设定:在CSS里设置DIV标签各属性参数为0div{margin:0;border:0;padding:0;}这里就设置了DIV标签CSS属性相当于初始化了DIV标签 ...
- 《LYFvs2013转vs2010》
<LYFvs2013转vs2010>1.修改解决方案文件(**.sln) 将-------------------------------------------------------- ...
- javaWEB总结(1):第一个servlet程序
1.新建一个javaWeb工程,工程的目录如下 2.在com.dao.chu的包下新建一个HelloServlet.java类 package com.dao.chu; import java.io. ...
- C# 连接SQL Server数据库的几种方式--server+data source等方式
如何使用Connection对象连接数据库? 对于不同的.NET数据提供者,ADO.NET采用不同的Connection对象连接数据库.这些Connection对象为我们屏蔽了具体的实现细节,并提供了 ...
- 其他应用和技巧-用try和catch来让程序更友好
---------------------------- <script type="text/javascript"> //进行 ...
- hdu_5768_Lucky7(中国剩余定理+容斥)
题目链接:hdu_5768_Lucky7 题意: 给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数 乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥 ...
- servlet第1讲初识
- Linux非root用户程序使用小于1024端口
在Linux下,默认端口1024下的是要在root下才能使用的,在其他用户下,如果尝试使用将会报错.在有的时候,我们可能考虑程序运行在root帐户下,可能会给Linux系统带来安全风险.那如何能够让非 ...
- Qt Quick里的图形效果:阴影(Drop Shadow)
Qt Quick提供了两种阴影效果: DropShow,阴影.这个元素会根据源图像,产生一个彩色的.模糊的新图像,把这个新图像放在源图像后面,给人一种源图像从背景上凸出来的效果. InnerShado ...
- Sublime console installation instructions install Package Control
instructions: import urllib2,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1 ...