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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black-hole6

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值