HDU 5624 KK's Reconstruction(最小生成树)

题目链接:点击打开链接

题意:n个城市, m条可以修建的路, 修每条路有一个费用, 要求修建路将n个城市全部联通,并且最大费用减去最小费用最小。

思路:枚举最小边, 然后重新求一遍最小生成树,复杂度m^2, 出的数据水了, 左边BC水过了。。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 1000000000 + 7;
const ll INF = (ll)2100000000;
const int maxn = 2000 + 10;
const int maxm = 15000 + 10;
int T,n,m,p[maxn];
struct node {
    ll a, b, c;
    bool operator < (const node& rhs) const {
        return c < rhs.c;
    }
}a[maxm];
int _find(int x) { return p[x] == x ? x : p[x] = _find(p[x]); }
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++) {
            scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c);
        }
        sort(a,a+m);
        ll ans = INF;
        for(int i=1;i<=n;i++) p[i] = i;
         for(int i=0;i<m;i++) {
            ll minv = a[i].c, maxv = a[i].c;
            ll cnt = 1;
            for(int j=1;j<=n;j++) p[j] = j;
            for(int j=i;j<m;j++) {
                int x = _find(a[j].a);
                int y = _find(a[j].b);
                if(x != y) {
                    p[x] = y;
                    maxv = a[j].c;
                    ++cnt;
                }
                if(cnt == n) break;
            }
            if(cnt == n) ans = min(ans, maxv - minv);
        }
        if(ans == INF) printf("-1\n");
        else printf("%I64d\n",ans);
    }
    return 0;
}

HDU 5624 KK's Reconstruction(最小生成树)

时间: 2024-10-13 07:14:34

HDU 5624 KK's Reconstruction(最小生成树)的相关文章

HDU 5624 KK&#39;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

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 oj1102 Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13995    Accepted Submission(s): 5324 Problem Description There are N villages, which are numbered from 1 to N, and you should

HDU 1863 畅通工程 (最小生成树)

Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现请你编写程序,计算出全省畅通需要的最低成本. Input 测试输入包含若干测试用例.每个测试用例的第1行给出评估的道路条数 N.村庄数目M ( < 100 ):随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间

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 1162 Eddy&#39;s picture 最小生成树入门题 Prim+Kruskal两种算法AC

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7428    Accepted Submission(s): 3770 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

hdu 1162 Eddy&#39;s picture(最小生成树算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6866    Accepted Submission(s): 3469 Problem Description Eddy begins to like p

HDU 1875 畅通工程再续 (最小生成树)

畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14835    Accepted Submission(s): 4591 Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题