N - 嘤嘤嘤 (并查集+枚举)
Our lovely KK has a difficult Social problem.
A big earthquake happened in his area.
N(2≤N≤2000)N(2≤N≤2000) cities have been implicated. All the roads between them are destroyed.
Now KK was commissioned to rebuild these roads.
However, after investigation,KK found that some roads are too damaged to rebuild.
Therefore, there are only M(0≤M≤15000)M(0≤M≤15000) roads can be rebuilt.
KK needs to make sure that there is a way between any two cities, and KK wants to rebuild roads as few as possible.
With rebuilding minimal number of roads, he also wants to minimize the difference between the price of the most expensive road been built and the cheapest one.
Input
The first line of the input file contains an integer T(1≤T≤10)T(1≤T≤10), which indicates the number of test cases.
For each test case,The first line includes two integers N(2≤N≤2000)N(2≤N≤2000),M(0≤M≤15000)M(0≤M≤15000).
The next MM lines include three integers a,b,c(a≠b,1≤c≤2∗109)a,b,c(a≠b,1≤c≤2∗109),indicating there is a undirected edge between aa and bb,the cost is cc.
Output
For each test case, output the smallest difference between the price of the most expensive road and the cheapest one.If there is no legal solution, then output -1.
Sample Input
2
5 10
1 2 9384
1 3 887
1 4 2778
1 5 6916
2 3 7794
2 4 8336
2 5 5387
3 4 493
3 5 6650
4 5 1422
2 0
Sample Output
1686
-1
题解:这道题的时间给的是6s,排完序后所以可以枚举每一种修路的情况,但是如果把所有的情况都存在数组里会溢出,就只把最贵的和最便宜的放数组然后做差,然后找到最小的。
此题坑点有主要有二:
1,存所有情况数组溢出
2,定义那个较大的数一定要尽量大(在比较求差值最小的时候)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int pre[100005];
struct node
{
int x,y;
long long int val;
}road[200005];
int find(int x)
{
if(x==pre[x])
return x;
else
{
return pre[x]=find(pre[x]);
}
}
bool merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return true;
}
else
{
return false;
}
}
bool cmp(node x,node y)
{
return x.val<y.val;
}
int main()
{
int n;
cin>>n;
while(n--)
{
int m,s;
cin>>m>>s;
for(int j=1;j<=m;j++)
{
pre[j]=j;
}
for(int t=1;t<=s;t++)
{
scanf("%d%d%d",&road[t].x,&road[t].y,&road[t].val);
}
sort(road+1,road+s+1,cmp);
long long int cnt=0;
int a[3000005];
int crt=0;
int l=0;
for(int t=1;t<=s;t++)
{
for(int j=t;j<=s;j++)
{
if(merge(road[j].x,road[j].y))
{
if(cnt%(m-1)==m-2||(cnt%(m-1)==0))
{
a[crt]=road[j].val;
crt++;
}
cnt++;
}
if(cnt%(m-1)==0)
{
for(int k=1;k<=m;k++)
{
pre[k]=k;
}
break;
}
}
}
long long int Min=999999999;
int k=0;
if(crt%2==1){
for(int t=0;t<crt-1;t=t+2)
{
if(Min>a[t+1]-a[t])
{
Min=a[t+1]-a[t];
}
}
}
else
{
for(int t=0;t<crt;t=t+2)
{
if(Min>a[t+1]-a[t])
{
Min=a[t+1]-a[t];
}
}
}
if(cnt>=m-1)
{
printf("%lld\n",Min);
}
else
{
printf("-1\n");
}
}
return 0;
}
N - 嘤嘤嘤 (并查集+枚举)的更多相关文章
- poj2912(种类并查集+枚举)
题目:http://poj.org/problem?id=2912 题意:n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000),接下来m行形如x, y, ch ...
- 2019.01.22 zoj3583 Simple Path(并查集+枚举)
传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). ...
- 1050. [HAOI2006]旅行【并查集+枚举】
Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求 一条路径,使得路径上最 ...
- BZOJ 1050 旅行comf 并查集+枚举下界
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1050 题目大意: 给你一个无向图,N(N<=500)个顶点, M(M<=5 ...
- POJ - 2912 Rochambeau (带权并查集+枚举)
题意:有N个人被分为了三组,其中有一个人是开了挂的.同组的人的关系是‘=’,不同组的人关系是‘<’或'>',但是开了挂的人可以给出自己和他人任意的关系.现在要根据M条关系找出这个开了挂的人 ...
- hdu 1598 find the most comfortable road(并查集+枚举)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- POJ2912 Rochambeau —— 种类并查集 + 枚举
题目链接:http://poj.org/problem?id=2912 Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submi ...
- USACO环绕岛屿Surround the Islands 并查集 枚举暴力
题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...
- poj2912(带权并查集+枚举)
题目链接:http://poj.org/problem?id=2912 题意:给n个人,m组关系,玩石头剪刀布的游戏,n个人中除一个人judge以外,其他人属于3个group(即石头.剪刀.布),他们 ...
随机推荐
- 为Docker镜像添加SSH服务
一.基于commit命令创建 1. 首先下载镜像 $ docker run -it ubuntu:16.04 /bin/bash 2. 安装SSH服务 #更新apt缓存 root@5ef1d31632 ...
- 企业级搜索引擎Solr 第三章 索引数据(Indexing Data)
虽然本书中假设你要建索引的内容都是有着良好结构的,比如数据库表,XML文件,CSV,但在现实中我们要保存很混乱的数据,或是二进制文件,如PDF,Microsoft Office,甚至是图片和音乐文件. ...
- Java线程池拒绝策略
Java线程池拒绝策略 相关资料: 线程池的RejectedExecutionHandler(拒绝策略):http://blog.csdn.net/jgteng/article/details/544 ...
- MSER
1.注释很全的分析:http://blog.csdn.net/zhaocj/article/details/40742191 2.opencv采用的mser实现方法 * 1. the gray ima ...
- 机器人自主移动的秘密:实际应用中,SLAM究竟是如何实现的?(二)
博客转载自:https://www.leiphone.com/news/201612/FRzmoEI8Iud6CmT2.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...
- p4213 【模板】杜教筛(Sum)
传送门 分析 我们知道 $\varphi * 1 = id$ $\mu * 1 = e$ 杜教筛即可 代码 #include<iostream> #include<cstdio> ...
- Luogu 3704 [SDOI2017]数字表格
列一下式子: $\prod_{i = 1}^{n}\prod_{j = 1}^{m}fib_{gcd(i, j)}$ 很套路的变成这样: $\prod_{d = 1}^{min(n, m)}fib_{ ...
- 24.Windows任意文件读取漏洞
漏洞概述: 近日,国外安全研究员SandboxEscaper又一次在推特上公布了新的Windows 0 day漏洞细节及PoC.这是2018年8月开始该研究员公布的第三个windows 0 day漏洞 ...
- LeetCode第35题:搜索插入位置
题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6 ...
- PHP网站在Linux服务器上安全设置方案
本文总结了PHP网站在Linux服务器上一些安全设置(ps:还有一些设置给忘了),在<lnmp一键安装包>大多数参数已经包含,如果有什么更多的设置,大家一起讨论学习 PHP安全配置 1. ...