Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 877    Accepted Submission(s): 236

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
题目大意:有n(n<100000)个地点面m(m<1000000)条路,修每条路都有花费w,求最小花费使每个地点能够互相到达并求出人一两点的花费期望;
因为n很大所以用kruskal求最小生成树求出最小花费,然后dfs搜索回溯的办法找到所有情况每条路用过的次数并求出总花费,用总花费除以所有可能发生的次数(n*(n-1)/2)就是我们要求的期望。
其中用到vector容器进行dfs;
以下是代码:

#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
#define LL long long
#define N 100010
#define M 1000010
vector<pair<int,int> > v[N];//定义一个pair型的主要是因为要在v[].second中储存路径权值
struct node
{
int a,b,w;
} edge[M];//储存每条边
int father[N],vis[N];
int n,m;
long long ans;//记录总权值;
bool cmp(node x,node y){
return x.w<y.w;
}
int finds(int x){
return father[x]==x?x:father[x]=finds(father[x]);
}
long long dfs(int x){//dfs递归搜索
vis[x]=;                   //标记顶点避免死循环
int i;
long long nb=,cb=;
int h=v[x].size();
for(i=; i<h; i++)              
{
int b=v[x][i].first;          
if(!vis[b])
{
nb=dfs(b);
cb+=nb;
ans+=nb*(n-nb)*v[x][i].second;
}
}
return +cb;
}
int main()
{
int t,i;
long long sum;
cin>>t;
while(t--)
{
for(i=; i<=N; i++)
v[i].clear();
memset(vis,,sizeof(vis));
ans=;
sum=;
int flag=;
scanf("%d%d",&n,&m);
if(n==||m==)
{
printf("0 0.00\n");
continue;
}
for(i=; i<=n; i++)
{
father[i]=i;
}
for(i=; i<m; i++)
{
scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].w);
}
sort(edge,edge+m,cmp);
for(i=; i<m; i++)
{
int fx=finds(edge[i].a);
int fy=finds(edge[i].b);
if(fx!=fy)
{
flag++;
father[fx]=fy;
sum+=edge[i].w;
v[edge[i].a].push_back(make_pair(edge[i].b,edge[i].w));
v[edge[i].b].push_back(make_pair(edge[i].a,edge[i].w));
}
if(flag==n-)
break;
}
for(i=; i<=n; i++)
{
if(v[i].size()==)
break;
}
long long ko=dfs(i);
double y=1.0*n*(n-)/;
printf("%I64d %.2lf\n",sum,(double)ans/y);
}
return ;
}

多校HDU5723 最小生成树+dfs回溯的更多相关文章

  1. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  2. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  3. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  7. P1074 靶形数独 dfs回溯法

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...

  8. 剪格子---(dfs回溯)

    如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...

  9. 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

随机推荐

  1. PowerDesigner如何导出表到word的方法

    from:https://jingyan.baidu.com/article/295430f1c385970c7f005056.html PowerDesigner如何导出表到word的方法 听语音 ...

  2. cocos2d-x学习日志(18) --程序是怎样開始执行与结束?

    问题的由来 怎么样使用 Cocos2d-x 高速开发游戏.方法非常easy,你能够看看其自带的例程,或者从网上搜索教程,执行起第一个HelloWorld,然后在 HelloWorld 里面写相关逻辑代 ...

  3. activity通过流程实例id动态获取流程图并展示在jsp页面上

    提供的Service方法如下: Java /** * 获取当前任务流程图 * * @param processInstanceId * @return */ @Override public Inpu ...

  4. Minimal Steiner Tree ACM

    上图论课的时候无意之间看到了这个,然后花了几天的时间学习了下,接下来做一个总结. 一般斯坦纳树问题是指(来自百度百科): 斯坦纳树问题是组合优化问题,与最小生成树相似,是最短网络的一种.最小生成树是在 ...

  5. JSF -> 导航(Navigation)

    在使用jsf框架时,肯定会用到faces-config.xml. 而其中就会出现很多的Navigation项. 其实这些Navigation就是一些页面跳转的东西. 以下内容来自http://blog ...

  6. 爬虫实战【6】Ajax内容解析-今日头条图集

    Ajax技术 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). Ajax并不是新的编程语言,而是一种使用现有标准的新方法,当然 ...

  7. 1400 序列分解(dfs)

    1400 序列分解 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 小刀和大刀是双胞胎兄弟.今天他们玩一个有意思的游戏. 大刀给小刀准备了一个长度为n的整数序列.小 ...

  8. java 多种判断key是否在map中存在的方法

    java 中有时候会遇到判断传过来的map里是否包含了指定的key,我目前只发现两种办法,如果有其他方法欢迎补充 我添加上去: HashMap map = new HashMap(); map.put ...

  9. 前端基础 & 初识JS(JavaScript)

    JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中),后将其改名ScriptEase(客 ...

  10. argparse 模块 在终端执行脚本文件

    1.案例 #1.案例: import argparse #首先导入模块 parser = argparse.ArgumentParser() #创建一个解析对象 parser.add_argument ...