博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多校HDU5723 最小生成树+dfs回溯
阅读量:5345 次
发布时间:2019-06-15

本文共 2448 字,大约阅读时间需要 8 分钟。

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(n100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000). 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(T10) 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 
#include
#include
#include
#include
#include
using namespace std;#define LL long long#define N 100010#define M 1000010vector
> 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
>t; while(t--) { for(i=0; i<=N; i++) v[i].clear(); memset(vis,0,sizeof(vis)); ans=0; sum=0; int flag=0; scanf("%d%d",&n,&m); if(n==0||m==0) { printf("0 0.00\n"); continue; } for(i=1; i<=n; i++) { father[i]=i; } for(i=0; i

 

转载于:https://www.cnblogs.com/yuanbo123/p/5687595.html

你可能感兴趣的文章
Git核心技术:在Ubuntu下部署Gitolite服务端
查看>>
平面波展开法总结
查看>>
建造者模式
查看>>
ArraySort--冒泡排序、选择排序、插入排序工具类demo
查看>>
composer 安装laravel
查看>>
8-EasyNetQ之Send & Receive
查看>>
Android反编译教程
查看>>
List<string> 去重复 并且出现次数最多的排前面
查看>>
js日志管理-log4javascript学习小结
查看>>
Android之布局androidmanifest.xml 资源清单 概述
查看>>
How to Find Research Problems
查看>>
Linux用户管理
查看>>
数据库第1,2,3范式学习
查看>>
《Linux内核设计与实现》第四章学习笔记
查看>>
使用iperf测试网络性能
查看>>
图片的显示隐藏(两张图片,默认的时候显示第一张,点击的时候显示另一张)...
查看>>
Docker 安装MySQL5.7(三)
查看>>
python 模块 来了 (调包侠 修炼手册一)
查看>>
关于CSS的使用方式
查看>>
本地MongoDB服务开启与连接本地以及远程服务器MongoDB服务
查看>>