多校HDU5723 最小生成树+dfs回溯
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 877 Accepted Submission(s): 236
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 <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回溯的更多相关文章
- 素数环(dfs+回溯)
题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...
- NOJ 1074 Hey Judge(DFS回溯)
Problem 1074: Hey Judge Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- P1074 靶形数独 dfs回溯法
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...
- 剪格子---(dfs回溯)
如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...
- 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯
算法提高 8皇后·改 时间限制:1.0s 内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...
随机推荐
- nyoj677 谍战
本题能够说是最小割入门级题目. 假设能想到是最小割问题.那么建图思路便是水到渠成的事了. 加入一个源点S和汇点T: 把S与每一个间谍相连.容量为无穷大: 把城市N(即飞机场的位置)与汇点T相连.容量为 ...
- TextView 图片居右
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- NoSQL-MongoDB with python
前言: MongoDB,文档存储型数据库(document store).NoSQL数据库中,它独占鳌头,碾压其他的NoSQL数据库. 使用C++开发的,性能仅次C.与redis一样,开源.高扩展.高 ...
- Nuske vs Phantom Thnook
Nuske vs Phantom Thnook Time limit : 4sec / Memory limit : 256MB Score : 700 points Problem Statemen ...
- sqlserver 脚本和批处理指令小结
一.脚本基础 1.USE语句 设置当前数据库. 2.声明变量 语法:DECLARE @变量名 变量类型 在声明变量后,给变量赋值之 ...
- windowsphone8.1学习笔记之磁贴
磁贴,wp系统独有.个人觉得还是蛮给力的. 好了废话不多说,我们来创建自己的辅助磁贴.创建磁贴是通过SecondaryTile对象来的,所以先说说该对象的一些重要属性. Arguments属性:传值用 ...
- iOS CGAffineTransform你了解多少?
CGAffineTransform介绍 概述 CGAffineTransform是一个用于处理形变的类,其可以改变控件的平移.缩放.旋转等,其坐标系统采用的是二维坐标系,即向右为x轴正方向,向下为y轴 ...
- IOS开发复习笔记(4)-TableView
总结几个TableView常用的代码 1.初始化方面 static string CellIndetifier=@"cellIndetifier"; -(NSInteger)num ...
- php7/etc/php-fpm.d 配置
php7/etc/php-fpm.d/www.conf nginx php-fpm 高并发优化 - 日记本的回忆 - 博客园 https://www.cnblogs.com/cocoliu/p/856 ...
- js验证表单大全2
屏蔽右键 很酷 oncontextmenu="return false" ondragstart="return false"onselectstart=&q ...