51nod 1640 天气晴朗的魔法 二分 + 克鲁斯卡算法(kruskal算法) 做复杂了

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640

一开始想的时候,看到要使得最大值最小,那这样肯定是二分这个最大值了,然后每一次都跑一次kruskal

这样的复杂度是O(E * 64),然后被卡TLE了

然后观察到kruskal的时候,如果最大边是val,那么比val大的是不要的了,然后整个数组也是有序的。

比如7、6、5、4、3、2、1等,这个也是可以lower_bound的,然后lower_bound后就能过,600ms

改了lower_bound后,我忘记删除那个if了,居然还是超时。TAT,这数据有点强。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
int n, m;
const int maxn = 2e5 + 20;
struct Edge {
    int u, v, w;
    bool operator < (const struct Edge & rhs) const {
        return w > rhs.w;
    }
}e[maxn];
int fa[maxn];
void init() {
    for (int i = 1; i <= n; ++i) fa[i] = i;
}
int tofind(int u) {
    if (fa[u] == u) return u;
    else return fa[u] = tofind(fa[u]);
}
void tomerge(int x, int y) {
    x = tofind(x);
    y = tofind(y);
    fa[y] = x;
}
LL nowAns;
bool check(LL val) {
    init();
    int sel = 0;
    nowAns = 0;
    struct Edge t;
    t.w = val;
    int pos = lower_bound(e + 1, e + 1 + m, t) - e;
    for (int i = pos; i <= m; ++i) {
//        if (e[i].w > val) continue;  卡TLE
        if (tofind(e[i].u) == tofind(e[i].v)) continue;
        tomerge(e[i].u, e[i].v);
        nowAns += e[i].w;
        sel++;
        if (sel == n - 1) {
            return true;
        }
    }
    return false;
}
void work() {
    scanf("%d%d", &n, &m);
    LL lo = 1e18L, hi = -1e18L;
    for (int i = 1; i <= m; ++i) {
        scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
        lo = min(lo, (LL)e[i].w);
        hi = max(hi, (LL)e[i].w);
    }
    sort(e + 1, e + 1 + m);
    while (lo <= hi) {
        LL mid = (lo + hi) >> 1;
        if (check(mid)) {
            hi = mid - 1;
        } else lo = mid + 1;
    }
    check(lo);
//    printf("%lld\n", nowAns);
    cout << nowAns << endl;
//    struct Edge t;
//    t.w = 3;
//    int pos = lower_bound(e + 1, e + 1 + m, t) - e;
//    cout << pos << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}

然后就上网看题解了,然后发现自己做法很复杂了。

感觉是题意弄得我们想复杂了,又魔法连的值,又总和,

正解是kruskal两次,第一次就能找出最大值最小,然后从大的再kruskal一次。

但是为什么还是700ms,有点坑。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
int n, m;
const int maxn = 2e5 + 20;
struct Node {
    int u, v, w;
    bool operator < (const struct Node & rhs) const {
        return w < rhs.w;
    }
}e[maxn];
bool cmp(struct Node a, struct Node b) {
    return a.w > b.w;
}
int fa[maxn];
int tofind(int u) {
    if (fa[u] == u) return u;
    else return fa[u] = tofind(fa[u]);
}
void tomerge(int x, int y) {
    x = tofind(x);
    y = tofind(y);
    fa[y] = x;
}
void init() {
    for (int i = 1; i <= n; ++i) fa[i] = i;
}
int result() {
    init();
    int sel = 0;
    for (int i = 1; i <= m; ++i) {
        if (tofind(e[i].u) == tofind(e[i].v)) continue;
        tomerge(e[i].u, e[i].v);
        sel++;
        if (sel == n - 1) return e[i].w;
    }
}
void work() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; ++i) {
        scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
    }
    sort(e + 1, e + 1 + m);
    int mx = result();
    sort(e + 1, e + 1 + m, cmp);
    init();
    LL ans = 0;
    int sel = 0;
    for (int i = 1; i <= m; ++i) {
        if (e[i].w > mx) continue;
        if (tofind(e[i].u) == tofind(e[i].v)) continue;
        tomerge(e[i].u, e[i].v);
        sel++;
        ans += e[i].w;
        if (sel == n - 1) break;
    }
    printf("%lld\n", ans);
//    cout << ans << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}

时间: 2024-08-04 20:29:29

51nod 1640 天气晴朗的魔法 二分 + 克鲁斯卡算法(kruskal算法) 做复杂了的相关文章

51nod 1640 天气晴朗的魔法 (最小生成树)

1640 天气晴朗的魔法 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 取消关注 这样阴沉的天气持续下去,我们不免担心起他的健康. 51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动. N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接起来,形成一个魔法阵. 魔法链是做法成功与否的关键.每一条魔法链都有一个魔力值V,魔法最终的效果取决于阵中所有魔法链的魔力值的和. 由于逆天改命的魔法过于暴力,所以我

51nod 1640 天气晴朗的魔法

题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 这样阴沉的天气持续下去,我们不免担心起他的健康. 51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动. N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接起来,形成一个魔法阵. 魔法链是做法成功与否的关键.每一条魔法链都有一个魔力值V,魔法最终的效果取决于阵中所有魔法链的魔力值的和. 由于逆天改命的魔法过于暴力,所以我们要求阵中的魔法链的魔力值最大值尽可能的小,与此

1640 天气晴朗的魔法

1640 天气晴朗的魔法 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 这样阴沉的天气持续下去,我们不免担心起他的健康. 51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动. N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接起来,形成一个魔法阵. 魔法链是做法成功与否的关键.每一条魔法链都有一个魔力值V,魔法最终的效果取决于阵中所有魔法链的魔力值的和. 由于逆天改命的魔法过于暴力,所以我们要求

天气晴朗的魔法 大+小生成树(最大值最小)/二分

天气晴朗的魔法 这样阴沉的天气持续下去,我们不免担心起他的健康. 51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动. N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接起来,形成一个魔法阵. 魔法链是做法成功与否的关键.每一条魔法链都有一个魔力值V,魔法最终的效果取决于阵中所有魔法链的魔力值的和. 由于逆天改命的魔法过于暴力,所以我们要求阵中的魔法链的魔力值最大值尽可能的小,与此同时,魔力值之和要尽可能的大. 现在给定魔法师人数N,魔法链数目M.求此魔法阵的最大效

51nod 1640 MST+二分

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640 1640 天气晴朗的魔法 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 这样阴沉的天气持续下去,我们不免担心起他的健康. 51nod魔法学校近日开展了主题为"天气晴朗"的魔法交流活动. N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接起来,形成一个魔法阵. 魔法链是

《机器学习实战》之二分K-均值聚类算法的python实现

<机器学习实战>之二分K-均值聚类算法的python实现 上面博文介绍了K-均值聚类算法及其用python实现,上篇博文中的两张截图,我们可以看到,由于K-均值聚类算法中由于初始质心的选取,会造成聚类的局部最优,并不是全局最优,因此,会造成聚类的效果并不理想,为克服K-均值算法收敛于局部最小值的问题,就有了二分K-均值算法. 二分K-均值聚类算法 二分K均值算法是基本K均值算法的直接扩充,其基本思想是:为了得到K个簇,首先将所有点的集合分裂成两个簇,然后从这些簇中选取一个继续分裂,迭代直到产生

HDU 2063:过山车(二分匹配,匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9745    Accepted Submission(s): 4294 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

POJ 3014:Asteroids(二分匹配,匈牙利算法)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14399   Accepted: 7836 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K as