51nod 1640 天气晴朗的魔法(并查集)
2016-04-16 09:44
375 查看
1640 天气晴朗的魔法
![](https://file.51nod.com/images/icon/ok.png)
题目来源: 原创
基准时间限制:1 秒 空间限制:131072 KB 分值: 20
难度:3级算法题
![](https://file.51nod.com/images/icon/star.png)
收藏
![](https://file.51nod.com/images/icon/plus.png)
关注
这样阴沉的天气持续下去,我们不免担心起他的健康。
51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动。
N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接起来,形成一个魔法阵。
魔法链是做法成功与否的关键。每一条魔法链都有一个魔力值V,魔法最终的效果取决于阵中所有魔法链的魔力值的和。
由于逆天改命的魔法过于暴力,所以我们要求阵中的魔法链的魔力值最大值尽可能的小,与此同时,魔力值之和要尽可能的大。
现在给定魔法师人数N,魔法链数目M。求此魔法阵的最大效果。
Input
Output
Input示例
Output示例
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
using namespace std;
#define esp 1e-8
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int inf = 2147483647;
const long long mod = 1000000007;
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中cin
int f[100005];
struct node
{
int a, b;
long long v;
}x[200005];
bool cmp(node a, node b)
{
return a.v < b.v;
}
int find(int x)
{
if (x != f[x])
f[x] = find(f[x]);
return f[x];
}
int main()
{
int n, m, i, j;
while (~scanf("%d%d", &n, &m))
{
for (i = 1; i <= n; ++i)
f[i] = i;
for (i = 0; i < m; ++i)
scanf("%d%d%lld", &x[i].a, &x[i].b, &x[i].v);
sort(x, x + m, cmp);
long long s = 0;
long long mmax = 0;
int y = 0;
for (i = 0; i < m; ++i)
{
int fa = find(x[i].a);
int fb = find(x[i].b);
if (fa != fb)
{
y++;
f[fa] = fb;
mmax = max(mmax, x[i].v);
}
if (y == n - 1)
break;
}
for (i = 1; i <= n; ++i)
f[i] = i;
y = 0;
for (i = m - 1; i >= 0; --i)
{
int fa = find(x[i].a);
int fb = find(x[i].b);
if (fa != fb && x[i].v <= mmax)
{
y++;
f[fa] = fb;
s += x[i].v;
}
if (y == n - 1)
break;
}
printf("%lld\n", s);
}
}
![](https://file.51nod.com/images/icon/ok.png)
题目来源: 原创
基准时间限制:1 秒 空间限制:131072 KB 分值: 20
难度:3级算法题
![](https://file.51nod.com/images/icon/star.png)
收藏
![](https://file.51nod.com/images/icon/plus.png)
关注
这样阴沉的天气持续下去,我们不免担心起他的健康。
51nod魔法学校近日开展了主题为“天气晴朗”的魔法交流活动。
N名魔法师按阵法站好,之后选取N - 1条魔法链将所有魔法师的魔力连接起来,形成一个魔法阵。
魔法链是做法成功与否的关键。每一条魔法链都有一个魔力值V,魔法最终的效果取决于阵中所有魔法链的魔力值的和。
由于逆天改命的魔法过于暴力,所以我们要求阵中的魔法链的魔力值最大值尽可能的小,与此同时,魔力值之和要尽可能的大。
现在给定魔法师人数N,魔法链数目M。求此魔法阵的最大效果。
Input
两个正整数N,M。(1 <= N <= 10^5, N <= M <= 2 * 10^5) 接下来M行,每一行有三个整数A, B, V。(1 <= A, B <= N, INT_MIN <= V <= INT_MAX) 保证输入数据合法。
Output
输出一个正整数R,表示符合条件的魔法阵的魔力值之和。
Input示例
4 6 1 2 3 1 3 1 1 4 7 2 3 4 2 4 5 3 4 6
Output示例
12
先用最下生成树找出那条魔法涟最大值最小的,然后再用最大生成树求魔法值都<= 该链即可。
#include <iostream>#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
using namespace std;
#define esp 1e-8
const double PI = acos(-1.0);
const double e = 2.718281828459;
const int inf = 2147483647;
const long long mod = 1000000007;
//freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中cin
int f[100005];
struct node
{
int a, b;
long long v;
}x[200005];
bool cmp(node a, node b)
{
return a.v < b.v;
}
int find(int x)
{
if (x != f[x])
f[x] = find(f[x]);
return f[x];
}
int main()
{
int n, m, i, j;
while (~scanf("%d%d", &n, &m))
{
for (i = 1; i <= n; ++i)
f[i] = i;
for (i = 0; i < m; ++i)
scanf("%d%d%lld", &x[i].a, &x[i].b, &x[i].v);
sort(x, x + m, cmp);
long long s = 0;
long long mmax = 0;
int y = 0;
for (i = 0; i < m; ++i)
{
int fa = find(x[i].a);
int fb = find(x[i].b);
if (fa != fb)
{
y++;
f[fa] = fb;
mmax = max(mmax, x[i].v);
}
if (y == n - 1)
break;
}
for (i = 1; i <= n; ++i)
f[i] = i;
y = 0;
for (i = m - 1; i >= 0; --i)
{
int fa = find(x[i].a);
int fb = find(x[i].b);
if (fa != fb && x[i].v <= mmax)
{
y++;
f[fa] = fb;
s += x[i].v;
}
if (y == n - 1)
break;
}
printf("%lld\n", s);
}
}
相关文章推荐
- Maximum Product of Word Lengths
- 文章标题
- 构建单页Web应用
- Linux学习之CentOS(一)--CentOS7环境搭建
- 将中文转码
- 截取NSString字符中一定范围的数据
- 解决Tomcat部署Maven异常:Deployment is out of date due to changes in the underlying project contents
- 浅谈ObjectAnimator设置动画
- 【转】360浏览器极速与兼容模式的解释
- sql_server角色成员身份权限
- SE4750 二维软解码在德州仪器Omap3平台的调试
- 用JS渲染的单页面应用其实性能还是比较差的
- 将字符串转换为data同时将中文转码
- 将字符串中的中文转为%格式
- 弹出系统播放器
- 将 expression 转换为数据类型 int 时发生算术溢出
- JVM调优之Tomcat启动参数配置及详解(一)
- 将 expression 转换为数据类型 int 时发生算术溢出
- 获取字符在字符串中的位置
- NSURLConnect的startImmediately如果设置为NO