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(即石头.剪刀.布),他们 ...
随机推荐
- CentOS6.5 安装ORACLE 安装界面乱码解决方案
在终端运行 export LANG=EN_US 然后再执行安装程序
- maven 配置说明
1.坐标 1.1 每一jar文件都有一个唯一坐标.通过坐标可以精确确定是哪个jar 1.2 坐标组成 1.2.1 Group ID : 公司名.公司网址倒写 1.2.2 Artifact ID : 项 ...
- 3-3 zookeeper的作用体现
zookeeper比较重要的一个模式:选举模式,这也是高可用的一个体现.公司的董事长.副董事长.董事会常理员以及老总和副总,他们并不会乘坐同一班飞机,而是会分为两班或者三班飞机一起去,也就是我们所谓的 ...
- Opencv Laplacian(拉普拉斯算子)
#include <iostream>#include <opencv2/opencv.hpp>#include <math.h> using namespace ...
- [转]JQuery 如何选择带有多个class的元素
比如下面代码需要选择同时带有这几个class的元素,怎么写? 1 <div class="modal fade in"></div> A: 1. 依次过滤 ...
- Windows系统 安装 CMake
Windows系统 安装 CMake 我们的电脑系统:Windows 10 64位 安装的CMake 版本:cmake-3.6.1-win64-x64(目前最新) 下载 在CMake官网下载:cmak ...
- 如何将.jpg图片 转换成.eps 格式图片
在使用latex写作论文的时候,需要插入一些图片,但是往往有些图片不是eps格式的.虽然网上有如何插入jpg格式的图片方法,但是经过我实验后发现都不太管用.最后找到一个比较靠谱的方法,使用latx本身 ...
- Amazon S3 云服务
一.简介 Amazon Simple Storage Service (S3) 是一个公开的服务,Web 应用程序开发人员可以使用它存储数字资产,包括图片.视频.音乐和文档. S3 提供一个 REST ...
- bzoj4619 4619: [Wf2016]Swap Space
传送门 分析 首先不难想到我们要先处理容量变大的再处理容量变小的 对于第一种情况我们自然要选择x小的先格式化,因为这个样暂时存储所需空间较小,可以使得情况更优 而第二种情况y先考虑,因为这样对总空间的 ...
- python3-file的修改实现类似shell中sed的功能
# Auther: Aaron Fan '''思路:目的是为了修改yesterday这个文件,但是因为无法直接去修改这个文件,所以需要先把修改好的内容写入高yesterday.new这个文件中,然后再 ...