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 + ;
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 = ; 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 = ;
nowAns = ;
struct Edge t;
t.w = val;
int pos = lower_bound(e + , e + + 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 - ) {
return true;
}
}
return false;
}
void work() {
scanf("%d%d", &n, &m);
LL lo = 1e18L, hi = -1e18L;
for (int i = ; 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 + , e + + m);
while (lo <= hi) {
LL mid = (lo + hi) >> ;
if (check(mid)) {
hi = mid - ;
} else lo = mid + ;
}
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 ;
}

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

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

正解是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 + ;
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 = ; i <= n; ++i) fa[i] = i;
}
int result() {
init();
int sel = ;
for (int i = ; i <= m; ++i) {
if (tofind(e[i].u) == tofind(e[i].v)) continue;
tomerge(e[i].u, e[i].v);
sel++;
if (sel == n - ) return e[i].w;
}
}
void work() {
scanf("%d%d", &n, &m);
for (int i = ; i <= m; ++i) {
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
}
sort(e + , e + + m);
int mx = result();
sort(e + , e + + m, cmp);
init();
LL ans = ;
int sel = ;
for (int i = ; 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 - ) 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 ;
}

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

  1. 51Nod - 1640 天气晴朗的魔法 大+小生成树(最大值最小)/二分

    天气晴朗的魔法 这样阴沉的天气持续下去,我们不免担心起他的健康.   51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动.   N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的 ...

  2. 51nod 1640 天气晴朗的魔法

    题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 这样阴沉的天气持续下去,我们不免担心起他的健康.   51nod魔法学校近日开展了主题为“天气晴朗 ...

  3. 51nod 1640 天气晴朗的魔法 最小生成树

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640 题解: 先求最小生成树,记录最大边. 然后求最大生成树 ...

  4. 51nod——1640 天气晴朗的魔法 有边权限制的最大生成树

    好好读题嗷:“所以我们要求阵中的魔法链的魔力值最大值尽可能的小,与此同时,魔力值之和要尽可能的大.” 第一条件是生成树的最大边权更小,第二条件是在最大边权的限制下搞一个最大生成树. 至于最大生成树,如 ...

  5. 51 Nod 1640 天气晴朗的魔法( Kruskall )

    #include <bits/stdc++.h> typedef long long LL; using namespace std; ; struct node{ LL u,v,w; n ...

  6. 51nod 天气晴朗的魔法 - (Kruskall最小生成树)

    题目: 基准时间限制:1 秒 空间限制:131072 KB  51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动.   N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接 ...

  7. 51nod 1640 MST+二分

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640 1640 天气晴朗的魔法 题目来源: 原创 基准时间限制:1 秒 ...

  8. 51nod-1640--天气晴朗的魔法(简单最小生成树)

    1640 天气晴朗的魔法 题目来源: 原创 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 这样阴沉的天气持续下去,我们不免担心起他的健康. 51nod魔法学校近日 ...

  9. javascript数据结构与算法---检索算法(二分查找法、计算重复次数)

    javascript数据结构与算法---检索算法(二分查找法.计算重复次数) /*只需要查找元素是否存在数组,可以先将数组排序,再使用二分查找法*/ function qSort(arr){ if ( ...

随机推荐

  1. 重学DSP:对于卷积的理解

    最近,我发现自己对于一个事情,如果不给自己一个说服自己的理由,就会出现不能理解,不能记住,以至于不会使用或者“盲目”应用的情况. 但是,我学的这个学科就是应当建立在对信号作用过程的理解上面的. 下面, ...

  2. hadoop 添加,删除节点

    http://www.cnblogs.com/tommyli/p/3418273.html

  3. ICE协议下NAT穿越的实现(STUN&TURN)

    正文: 一. 首先来简单讲讲什么是NAT? 原来这是因为IPV4引起的,我们上网很可能会处在一个NAT设备(无线路由器之类)之后.NAT设备会在IP封包通过设备时修改源/目的IP地址. 对于家用路由器 ...

  4. Swing项目编译成exe,并且打包成安装文件(二)

    前面我们讲到了将Swing项目编译成双击可执行的文件exe,这篇我就教大家怎么把exe打包成需要在电脑安装的那种,首先需要一个工具,Inno Setup 编译器, 下载地址,我这个是汉化版的,双击打开 ...

  5. 我的CSDN博客

    从csdn搬过来的: csdn地址:http://blog.csdn.net/WR_technology

  6. nginx开发_ngx_palloc源码解析

    功能简介 ngx_pool_t是nginx开发中最经常使用到的内存容器.对动态内存的封装,由框架进行创建与释放,模块开发过程中仅需要进行内存申请,不需要关注何时释放.常见的pool对象有: 1. ng ...

  7. IP地址库解析——读取IP地址获得实际地理位置信息的java源码实现

    说明:IP地址库来自QQwry.dat数据库文件,通过解析地址库当中的ip,已经细化最后获取的信息:获取ip地址对应的:国家 / 省 / 市 / 运营商ISP信息. 解析主要用到三个类: (1) IP ...

  8. 创建Sitemap文件供搜索引擎使用

    以下内容转载自 http://www.cnblogs.com/webtrados/archive/2009/12/29/1635305.html 如何创建Sitemap文件 Sitemap的格式有XM ...

  9. UVa 1658 Admiral (最小费用流)

    题意:给定一个图,求1-n的两条不相交的路线,并且权值和最小. 析:最小费用流,把每个结点都拆成两个点,中间连一条容量为1的边,然后一个作为入点,另一个是出点.最后跑两次最小费用流就行了. 代码如下: ...

  10. 《剑指offer》面试题1:为类CMyString添加赋值运算符函数——C++拷贝构造函数与赋值函数

    题中已给出CMyString的类定义,要求写赋值运算符函数. #include<iostream> #include<cstring> using namespace std; ...