HDU 5624 KK's Reconstruction

KK‘s Reconstruction

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 360    Accepted Submission(s): 151

Problem Description

Our lovely KK has a difficult Social problem.
A big earthquake happened in his area.
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) 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), which indicates the number of test cases.

For each test case,The first line includes two integers N(2≤N≤2000),M(0≤M≤15000).

The next M lines include three integers a,b,c(a≠b,1≤c≤2?109),indicating there is a undirected edge between a and b,the cost is c.

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


所修的路中最长的一段和最小的一段的差值最小为多少 遍历求解

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int f[2009];
int n,m,t;
struct Node
{
    ll u;
    ll v;
    ll w;
    friend bool operator<(const Node&a,const Node &b)
    {
        return a.w<b.w;
    }
}e[15009];
int find(int x)
{
    return f[x]==x?x:find(f[x]);
}
ll kruskal()
{
    ll ans=INF,minn,maxn;
    int q;
    for(int i=0;i<m;i++)
    {
        minn=e[i].w,maxn=e[i].w;
        q=1;
        for(int k=1;k<=n;k++)
            f[k]=k;
        for(int j=i;j<m;j++)
        {
            int u=find(e[j].u);
            int v=find(e[j].v);
            if(u!=v)
            {
                maxn=max(maxn,e[j].w);
                if(u<v) f[u]=v;
                else f[v]=u;
                q++;
                if(q==n) break;
            }
        }
        if(q==n) ans=min(ans,maxn-minn);
    }
    if(ans==INF) return -1;
    return ans;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            scanf("%lld%lld%lld",&e[i].u,&e[i].v,&e[i].w);
        }
        sort(e,e+m);
        printf("%lld\n",kruskal());
    }
    return 0;
}

HDU 5624 KK's Reconstruction

时间: 2024-10-13 04:56:17

HDU 5624 KK's Reconstruction的相关文章

HDU 5624 KK&#39;s Reconstruction(最小生成树)

题目链接:点击打开链接 题意:n个城市, m条可以修建的路, 修每条路有一个费用, 要求修建路将n个城市全部联通,并且最大费用减去最小费用最小. 思路:枚举最小边, 然后重新求一遍最小生成树,复杂度m^2, 出的数据水了, 左边BC水过了.. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #inc

hdu 5623 KK&#39;s Number(dp)

问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10?4??)个数,每次KK都会先拿数.每次可以拿任意多个数,直到NN个数被拿完.每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大.在这样的情况下,最终KK的得分减去对手的得分会是多少? 输入描述 第一行一个数T\left( 1\leq T\leq 10\right)T(1≤T≤10),表示数据组

hdu 5621 KK&#39;s Point(数学,推理题)

题解: 在圆上点三个点时,除圆上三个交点外,圆内没有交点:在圆上点四个点时,除圆上四个交点外,圆内出现了一个交点,因此,在N个点中每四个点便可以在圆内产生一个交点,因此N个点在圆内形成的点的个数为CN4,总的交点数就是CN4+N 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstrin

hdu 5620 KK&#39;s Steel(推理)

Problem Description Our lovely KK has a difficult mathematical problem:he has a N(1≤N≤1018) meters steel,he will cut it into steels as many as possible,and he doesn't want any two of them be the same length or any three of them can form a triangle. I

HDU 5620 KK&#39;s Steel

想了一下发现是斐波那契数列.....水题 #include <stdio.h> #include <algorithm> #include <string.h> #include <queue> #include <stack> #include <map> #include <vector> using namespace std; const int maxn=1000; long long n; long long

HDU 1885 Key Task

bfs. 一把某种颜色的锁开 所有这个颜色的门. 状态检查压缩一下  vis[][][2^4]; 跟HDU 1429 类似.至于颜色判断我用了 map: #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostre

hdu 5025 BFS + 优化 广东区域赛网赛

http://acm.hdu.edu.cn/showproblem.php?pid=5025 TLE了好几次 写的时候,问题在于, 1.钥匙怎么处理 参考了他人的写法,vis[key_num][i][j],相当于将图多维化,这样就可以判重了,否则只是按照普通的BFS,钥匙数不同的时候,可以重复,这个代码难易表达出来 另外此处一个很好的优化,当cost_now<cost[key_now][x_now][y_now]的时候,才q.push(Node)  这个剪枝很好啊 2.蛇怎么处理 没蛇的话,第一

hdu 5625

Clarke and chemistry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 63    Accepted Submission(s): 33 Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke

hdu 1023 Train Problem II

卡特兰数的应用 :卡特兰数参考kuangbing总结http://www.cnblogs.com/kuangbin/archive/2012/03/21/2410516.html. 大数的运算 hdu题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1023 1 #include<algorithm> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<iostream