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(即石头.剪刀.布),他们 ...
随机推荐
- 34款Firefox渗透测试插件
1:Firebug Firefox的 五星级强力推荐插件之一,不许要多解释 2:User Agent Switcher 改变客户端的User Agent的一款插件 3:Hackbar 攻城师必备工具, ...
- java在Win7 64位 获取客户端的IP,MAC,计算机名
package com.javaweb.util; import java.io.IOException; import java.io.InputStreamReader; import java. ...
- hadoop再次集群搭建(1)-安装系统
从8月份到现在12月份,中间有四个月的时间没有学习hadoop系统了.其实适应新的环境,到现在一切尘埃落定,就应该静下心来,好好学习一下hadoop以及我之前很想学习的mahout.个人对算法比较感兴 ...
- show table detail
create table #t(name varchar(255), rows bigint, reserved varchar(20),data varchar(20), index_size va ...
- 关于android中出现failed to read row 0,column -1错误
该错误出现的原因是Cursor.getColumnIndex()的参数列名不存在或者错误,这时返回值为-1.出现该错误
- SWT简介
--------------siwuxie095 SWT 简介: SWT(Standard Widget Toolkit) 也是基于一个对等体实 ...
- 解析IFC数据并转成json格式
{ "com.bim.ifc.ifc2x3.ifc2x3tc1.IfcBuilding (#104)-": [{ "objKey": "GlobalI ...
- java中下面这些引入都代表什么意思啊?
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; import java.uti ...
- Luogu 3530 [POI2012]FES-Festival
我是真的不会写差分约束啊呜呜呜…… BZOJ 2788被权限了. 首先对于第一个限制$x + 1 = y$,可以转化成$x + 1 \leq y \leq x + 1$, 所以连一条$(y, x, - ...
- Entity Framework Tutorial Basics(5):Create Entity Data Model
Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB datab ...